I've finished a script that'll make a DIV scroll out from the side of the page when a button/link is clicked, but I'm having a little trouble compacting that script.
It's rather long, so I decided to put in a couple of for statements.
Here's the original function:
function slideb() {
clearTimeout(mTimer1);
if (menu.xpos > -110) {
menu.xpos--;
if (menu.xpos < -8) {
menu.xpos--;
if (menu.xpos < -20) {
menu.xpos--;
if (menu.xpos < -30) {
menu.xpos--;
if (menu.xpos < -40) {
menu.xpos--;
if (menu.xpos < -50) {
menu.xpos--;
if (menu.xpos < -60) {
menu.xpos--;
if (menu.xpos < -70)
menu.xpos--;
}
}
}
}
}
}
menu.left = menu.xpos;
setTimeout(mTimer2,1)
}
else
clearTimeout(mTimer2)
}
Now, everything about that worked perfectly, but that's an extremely lengthy function for doing something so simple.
This is how I tried to shorten it:
function slideb() {
if (menu.xpos > -110) {
for (i = 0;i > -100;i -= 1) {
if (menu.xpos <= i)
menu.xpos--;
menu.left = menu.xpos;
setTimeout(mTimer2,1);
}
}
else
clearTimeout(mTimer2);
}
It seems like it would work, but it doesn't. It doesn't reload the position of the menu DIV fast enough.
Here's the whole file so you can see what I'm talking about:
sliding.html
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Is() {
var agent = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion);
this.minor = parseFloat(navigator.appVersion);
this.ns = ((agent.indexOf('mozilla') != -1) && ((agent.indexOf('spoofer') == -1) && (agent.indexOf('compatible') == -1)));
this.ns2 = (this.ns && (this.major == 3));
this.ns3 = (this.ns && (this.major == 3));
this.ns4b = (this.ns && (this.minor < 4.04));
this.ns4 = (this.ns && (this.major >= 4));
this.ie = (agent.indexOf("msie"
!= -1);
this.ie3 = (this.ie && (this.major == 2));
this.ie4 = (this.ie && (this.major >= 4));
this.op3 = (agent.indexOf("opera"
!= -1);
this.win = (agent.indexOf("win"
!= -1);
this.mac = (agent.indexOf("mac"
!= -1);
this.unix = (agent.indexOf("x11"
!= -1);
}
function init() {
var is = new Is();
if (is.ns4) menu = document.menuDiv
if (is.ie4) menu = menuDiv.style
menu.xpos = parseInt(menu.left)
}
mTimer1 = "slide()";
mTimer2 = "slideb()";
// Original idea for the script, but has been
// revised in slideb()
function slide() {
clearTimeout(mTimer2);
if (menu.xpos < -2) {
menu.xpos++;
if (menu.xpos > -98) {
menu.xpos++;
if (menu.xpos > -70) {
menu.xpos += 2;
if (menu.xpos > -40)
menu.xpos += 3;
}
}
menu.left = menu.xpos
setTimeout(mTimer1,1)
}
else
clearTimeout(mTimer1)
}
// function that works
/* function slideb() {
clearTimeout(mTimer1);
if (menu.xpos > -110) {
menu.xpos--;
if (menu.xpos < -8) {
menu.xpos--;
if (menu.xpos < -20) {
menu.xpos--;
if (menu.xpos < -30) {
menu.xpos--;
if (menu.xpos < -40) {
menu.xpos--;
if (menu.xpos < -50) {
menu.xpos--;
if (menu.xpos < -60) {
menu.xpos--;
if (menu.xpos < -70)
menu.xpos--;
}
}
}
}
}
}
menu.left = menu.xpos;
setTimeout(mTimer2,1)
}
else
clearTimeout(mTimer2)
} */
// function that I want to work
function slideb() {
if (menu.xpos > -110) {
for (i = 0;i > -100;i -= 1) {
if (menu.xpos <= i)
menu.xpos--;
menu.left = menu.xpos;
setTimeout(mTimer2,1);
}
}
else
clearTimeout(mTimer2);
}
//-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" onLoad="init()">
<DIV STYLE="position: absolute;left: 400px;"><A HREF="javascript:slide()">slide</A> - <A HREF="javascript:slideb()">back</A></DIV>
<DIV ID="menuDiv" STYLE="position:absolute; left:-110px; top:0px; width:150px; height:600px;clip:rect(0,120,600,0);layer-background-color: #000000;background: #000000;border: #FFFFFF;">
</DIV>
</BODY>
</HTML>
Now, what would be appreciated is the function slideb() written.
It's rather long, so I decided to put in a couple of for statements.
Here's the original function:
function slideb() {
clearTimeout(mTimer1);
if (menu.xpos > -110) {
menu.xpos--;
if (menu.xpos < -8) {
menu.xpos--;
if (menu.xpos < -20) {
menu.xpos--;
if (menu.xpos < -30) {
menu.xpos--;
if (menu.xpos < -40) {
menu.xpos--;
if (menu.xpos < -50) {
menu.xpos--;
if (menu.xpos < -60) {
menu.xpos--;
if (menu.xpos < -70)
menu.xpos--;
}
}
}
}
}
}
menu.left = menu.xpos;
setTimeout(mTimer2,1)
}
else
clearTimeout(mTimer2)
}
Now, everything about that worked perfectly, but that's an extremely lengthy function for doing something so simple.
This is how I tried to shorten it:
function slideb() {
if (menu.xpos > -110) {
for (i = 0;i > -100;i -= 1) {
if (menu.xpos <= i)
menu.xpos--;
menu.left = menu.xpos;
setTimeout(mTimer2,1);
}
}
else
clearTimeout(mTimer2);
}
It seems like it would work, but it doesn't. It doesn't reload the position of the menu DIV fast enough.
Here's the whole file so you can see what I'm talking about:
sliding.html
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Is() {
var agent = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion);
this.minor = parseFloat(navigator.appVersion);
this.ns = ((agent.indexOf('mozilla') != -1) && ((agent.indexOf('spoofer') == -1) && (agent.indexOf('compatible') == -1)));
this.ns2 = (this.ns && (this.major == 3));
this.ns3 = (this.ns && (this.major == 3));
this.ns4b = (this.ns && (this.minor < 4.04));
this.ns4 = (this.ns && (this.major >= 4));
this.ie = (agent.indexOf("msie"
this.ie3 = (this.ie && (this.major == 2));
this.ie4 = (this.ie && (this.major >= 4));
this.op3 = (agent.indexOf("opera"
this.win = (agent.indexOf("win"
this.mac = (agent.indexOf("mac"
this.unix = (agent.indexOf("x11"
}
function init() {
var is = new Is();
if (is.ns4) menu = document.menuDiv
if (is.ie4) menu = menuDiv.style
menu.xpos = parseInt(menu.left)
}
mTimer1 = "slide()";
mTimer2 = "slideb()";
// Original idea for the script, but has been
// revised in slideb()
function slide() {
clearTimeout(mTimer2);
if (menu.xpos < -2) {
menu.xpos++;
if (menu.xpos > -98) {
menu.xpos++;
if (menu.xpos > -70) {
menu.xpos += 2;
if (menu.xpos > -40)
menu.xpos += 3;
}
}
menu.left = menu.xpos
setTimeout(mTimer1,1)
}
else
clearTimeout(mTimer1)
}
// function that works
/* function slideb() {
clearTimeout(mTimer1);
if (menu.xpos > -110) {
menu.xpos--;
if (menu.xpos < -8) {
menu.xpos--;
if (menu.xpos < -20) {
menu.xpos--;
if (menu.xpos < -30) {
menu.xpos--;
if (menu.xpos < -40) {
menu.xpos--;
if (menu.xpos < -50) {
menu.xpos--;
if (menu.xpos < -60) {
menu.xpos--;
if (menu.xpos < -70)
menu.xpos--;
}
}
}
}
}
}
menu.left = menu.xpos;
setTimeout(mTimer2,1)
}
else
clearTimeout(mTimer2)
} */
// function that I want to work
function slideb() {
if (menu.xpos > -110) {
for (i = 0;i > -100;i -= 1) {
if (menu.xpos <= i)
menu.xpos--;
menu.left = menu.xpos;
setTimeout(mTimer2,1);
}
}
else
clearTimeout(mTimer2);
}
//-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" onLoad="init()">
<DIV STYLE="position: absolute;left: 400px;"><A HREF="javascript:slide()">slide</A> - <A HREF="javascript:slideb()">back</A></DIV>
<DIV ID="menuDiv" STYLE="position:absolute; left:-110px; top:0px; width:150px; height:600px;clip:rect(0,120,600,0);layer-background-color: #000000;background: #000000;border: #FFFFFF;">
</DIV>
</BODY>
</HTML>
Now, what would be appreciated is the function slideb() written.