/    Sign up×
Community /Pin to ProfileBookmark

‘Refreshing’

Hello!

I would like to know if there a way to get the code below working as I intended (i.e. the div’s smoothly going up in stead of in one time with a one-second (100 * 10 milliseconds = 1 second) delay)

[code]<?xml version=”1.0″ encoding=”iso-8859-1″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” />

<head>
<title>airJX.com</title>

<script language=”JavaScript” type=”text/JavaScript”>

var Netscape = (window.Event) ? true : false;
var IExplore = (document.all) ? true : false;

function moveDiv(iLeft,iTop) {

if(Netscape) {
document.layers[‘div1’].left = parseInt(document.layers[‘div1’].left) + iLeft;
document.layers[‘div1’].top = parseInt(document.layers[‘div1’].top) + iTop;
document.layers[‘div2’].left = parseInt(document.layers[‘div2’].left) + iLeft;
document.layers[‘div2’].top = parseInt(document.layers[‘div2’].top) + iTop;
document.layers[‘div3’].left = parseInt(document.layers[‘div3’].left) + iLeft;
document.layers[‘div3’].top = parseInt(document.layers[‘div3’].top) + iTop;
}

if(IExplore) {
document.all.div1.style.left = parseInt(document.all.div1.style.left) + iLeft;
document.all.div1.style.top = parseInt(document.all.div1.style.top) + iTop;
document.all.div2.style.left = parseInt(document.all.div2.style.left) + iLeft;
document.all.div2.style.top = parseInt(document.all.div2.style.top) + iTop;
document.all.div3.style.left = parseInt(document.all.div3.style.left) + iLeft;
document.all.div3.style.top = parseInt(document.all.div3.style.top) + iTop;
}
}

function moveUp() {

for (a = 0; a <= 100; a++) {
pause(10);
moveDiv(0,-1);
}

}

function pause(iDelay) {

Temp = new Date();
iOld = Temp.getMilliseconds() + 1000 * Temp.getSeconds() + 60000 * Temp.getMinutes() + 3600000 * Temp.getHours();
iNew = 0;

while (iOld + iDelay > iNew) {
Temp = new Date();
iNew = Temp.getMilliseconds() + 1000 * Temp.getSeconds() + 60000 * Temp.getMinutes() + 3600000 * Temp.getHours();
}
}
</script>
</head>

<body>

<a onmousedown=”javascript:moveUp()”>Click this!</a>

<div id=”div1″ style=”position: absolute; left: 100px; top: 100px; height: 100px; width: 300px; background-color: 00FFFF;”>
Greetings!
</div>

<div id=”div2″ style=”position: absolute; left: 100px; top: 250px; height: 100px; width: 300px; background-color: 00FFFF;”>
Hello!
</div>

<div id=”div3″ style=”position: absolute; left: 100px; top: 400px; height: 100px; width: 300px; background-color: 00FFFF;”>
Bye now!
</div>

</body>
</html>[/code]

I think there should be somesort of refresh-action in the moveUp-function…

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMay 30.2004 — Hi!

Actually, your code is designed for IE4+ and NS4.x (document.layers does not exist in later versions). Anyway, you didn't implement stuff necessary for NS4 to run the function.

This example works in the browsers specified above:[code=php]<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" />
<head>
<title>airJX.com</title>
<script language="JavaScript" type="text/JavaScript">
var blah;
var Netscape4 = (document.layers) ? true : false;
var IExplore = (document.all) ? true : false;

function moveDiv(iLeft,iTop) {
if(Netscape4) {
document.layers['div1'].left = parseInt(document.layers['div1'].left) + iLeft;
document.layers['div1'].top = parseInt(document.layers['div1'].top) + iTop;
document.layers['div2'].left = parseInt(document.layers['div2'].left) + iLeft;
document.layers['div2'].top = parseInt(document.layers['div2'].top) + iTop;
document.layers['div3'].left = parseInt(document.layers['div3'].left) + iLeft;
document.layers['div3'].top = parseInt(document.layers['div3'].top) + iTop;
}
if(IExplore) {
document.all.div1.style.left = parseInt(document.all.div1.style.left) + iLeft;
document.all.div1.style.top = parseInt(document.all.div1.style.top) + iTop;
document.all.div2.style.left = parseInt(document.all.div2.style.left) + iLeft;
document.all.div2.style.top = parseInt(document.all.div2.style.top) + iTop;
document.all.div3.style.left = parseInt(document.all.div3.style.left) + iLeft;
document.all.div3.style.top = parseInt(document.all.div3.style.top) + iTop;
}
}
function moveUp() {
moveDiv(0,-1)
blah=setTimeout('moveUp()',100)
}
</script>
</head>
<body>
<a href="#" onmousedown="moveUp()">Move up!</a>
<br>
<a href="#" onmousedown="clearTimeout(blah)">Stop!</a>
<div id="div1" style="position: absolute; left: 100px; top: 100px; height: 100px; width: 300px; background-color: 00FFFF;">
Greetings!
</div>
<div id="div2" style="position: absolute; left: 100px; top: 250px; height: 100px; width: 300px; background-color: 00FFFF;">
Hello!
</div>
<div id="div3" style="position: absolute; left: 100px; top: 400px; height: 100px; width: 300px; background-color: 00FFFF;">
Bye now!
</div>
</body>
</html>[/code]
I would advise you to modify it to make it work in modern NS versions and Mozilla too, which are by far more important than NS4.x

Cheers - Pit
Copy linkTweet thisAlerts:
@airJXauthorMay 30.2004 — This code works well, thanks.

But what should I use for NS5+ instead?
Copy linkTweet thisAlerts:
@PittimannMay 30.2004 — Hi!

This should work with modern NS versions as well:[code=php]<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" />
<head>
<title>airJX.com</title>
<script language="JavaScript" type="text/JavaScript">
var blah;
var dom=(!document.all&&document.getElementById);
var Netscape4 = (document.layers) ? true : false;
var IExplore = (document.all) ? true : false;

function moveDiv(iLeft,iTop) {
if(dom) {
document.getElementById('div1').style.left = parseInt(document.getElementById('div1').style.left) + iLeft+'px';
document.getElementById('div1').style.top = parseInt(document.getElementById('div1').style.top) + iTop+'px';
document.getElementById('div2').style.left = parseInt(document.getElementById('div2').style.left) + iLeft+'px';
document.getElementById('div2').style.top = parseInt(document.getElementById('div2').style.top) + iTop+'px';
document.getElementById('div3').style.left = parseInt(document.getElementById('div3').style.left) + iLeft+'px';
document.getElementById('div3').style.top = parseInt(document.getElementById('div3').style.top) + iTop+'px';
}
if(Netscape4) {
document.layers['div1'].left = parseInt(document.layers['div1'].left) + iLeft;
document.layers['div1'].top = parseInt(document.layers['div1'].top) + iTop;
document.layers['div2'].left = parseInt(document.layers['div2'].left) + iLeft;
document.layers['div2'].top = parseInt(document.layers['div2'].top) + iTop;
document.layers['div3'].left = parseInt(document.layers['div3'].left) + iLeft;
document.layers['div3'].top = parseInt(document.layers['div3'].top) + iTop;
}
if(IExplore) {
document.all.div1.style.left = parseInt(document.all.div1.style.left) + iLeft;
document.all.div1.style.top = parseInt(document.all.div1.style.top) + iTop;
document.all.div2.style.left = parseInt(document.all.div2.style.left) + iLeft;
document.all.div2.style.top = parseInt(document.all.div2.style.top) + iTop;
document.all.div3.style.left = parseInt(document.all.div3.style.left) + iLeft;
document.all.div3.style.top = parseInt(document.all.div3.style.top) + iTop;
}
}
function moveUp() {
moveDiv(0,-1)
blah=setTimeout('moveUp()',100)
}
function moveDown() {
moveDiv(0,1)
blah=setTimeout('moveDown()',100)
}
</script>
</head>
<body>
<a href="#" onmousedown="if(blah)clearTimeout(blah);moveUp();">Move up!</a>
<br>
<a href="#" onmousedown="clearTimeout(blah)">Stop!</a>
<br>
<a href="#" onmousedown="if(blah)clearTimeout(blah);moveDown();">Move down!</a>
<div id="div1" style="position: absolute; left: 100px; top: 100px; height: 100px; width: 300px; background-color: #00FFFF;">
Greetings!
</div>
<div id="div2" style="position: absolute; left: 100px; top: 250px; height: 100px; width: 300px; background-color: #00FFFF;">
Hello!
</div>
<div id="div3" style="position: absolute; left: 100px; top: 400px; height: 100px; width: 300px; background-color: #00FFFF;">
Bye now!
</div>
</body>
</html>[/code]
I also added a "move down" function.

Cheers - Pit
Copy linkTweet thisAlerts:
@airJXauthorMay 30.2004 — The first 'dom'-method works for IE as well. So I changed the code to the following:

(I tested the code and it works for IExplore, Netscape and Mozilla)
&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" /&gt;
&lt;head&gt;
&lt;title&gt;airJX.com&lt;/title&gt;
&lt;script language="JavaScript" type="text/JavaScript"&gt;

<i> </i> var iTimeout;

<i> </i> function moveDiv(iLeft,iTop) {
<i> </i> document.getElementById('div1').style.left = parseInt(document.getElementById('div1').style.left) + iLeft + 'px';
<i> </i> document.getElementById('div1').style.top = parseInt(document.getElementById('div1').style.top) + iTop + 'px';
<i> </i> document.getElementById('div2').style.left = parseInt(document.getElementById('div2').style.left) + iLeft + 'px';
<i> </i> document.getElementById('div2').style.top = parseInt(document.getElementById('div2').style.top) + iTop + 'px';
<i> </i> document.getElementById('div3').style.left = parseInt(document.getElementById('div3').style.left) + iLeft + 'px';
<i> </i> document.getElementById('div3').style.top = parseInt(document.getElementById('div3').style.top) + iTop + 'px';
<i> </i> }

<i> </i> function moveUp() {
<i> </i> moveDiv(0,-1)
<i> </i> iTimeout = setTimeout('moveUp()',100)
<i> </i> }

<i> </i> function moveDown() {
<i> </i> moveDiv(0,1)
<i> </i> iTimeout = setTimeout('moveDown()',100)
<i> </i> }
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;a href="#" onmousedown="if(iTimeout)clearTimeout(iTimeout);moveUp();"&gt;Move up!&lt;/a&gt;
&lt;br&gt;
&lt;a href="#" onmousedown="clearTimeout(iTimeout)"&gt;Stop!&lt;/a&gt;
&lt;br&gt;
&lt;a href="#" onmousedown="if(iTimeout)clearTimeout(iTimeout);moveDown();"&gt;Move down!&lt;/a&gt;

&lt;div id="div1" style="position: absolute; left: 100px; top: 100px; height: 100px; width: 300px; background-color: #00FFFF;"&gt;
Greetings!
&lt;/div&gt;

&lt;div id="div2" style="position: absolute; left: 100px; top: 250px; height: 100px; width: 300px; background-color: #00FFFF;"&gt;
Hello!
&lt;/div&gt;

&lt;div id="div3" style="position: absolute; left: 100px; top: 400px; height: 100px; width: 300px; background-color: #00FFFF;"&gt;
Bye now!
&lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;
Thanx a lot for your help!
Copy linkTweet thisAlerts:
@PittimannMay 30.2004 — Hi!

You're welcome!

I deliberately left in the old IE stuff to maintain compatibility with IE4 (which doesn't "understand" getElementById).

Cheers - Pit
×

Success!

Help @airJX spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...