...

View Full Version : Making a div move to point A then point B (code isn't working).



DayOldPorridge
11-16-2008, 07:44 PM
Here is the code I have right now. It won't let me move the div back to a certain point unless it's to the left, or beyond the div's specified 'left:' attribute. I can't seem to make it stop before it hits that point; the script just doesn't function if I try doing that.



var the_timeout;
var direction = "right";

function moveDiv()
{
var new_left;

var the_style = getStyleObject("myDiv");
if (the_style)
{

var current_left = parseInt(the_style.left);
if (direction == "right")
{
new_left = current_left + 5;
}
else
{
new_left = current_left - 5;
}

if (new_left > 300)
{
direction = "left";
window.document.bee.src = "bee_left.gif";
}

if (new_left < 30)
{
the_timeout = setTimeout('moveDiv();',10); return true;
}

if (document.layers)
{
the_style.left = new_left;
}
else
{
the_style.left = new_left + "px";
}

the_timeout = setTimeout('moveDiv();',10);
}
}

vwphillips
11-16-2008, 10:19 PM
<!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" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
var the_timeout;
var direction = "right";

function moveDiv(){
var new_left;
var obj = document.getElementById("myDiv");
if (obj){
var current_left = parseInt(obj.style.left);
if (direction == "right"){
new_left = current_left + 5;
}
else{
new_left = current_left - 5;
}
if (new_left > 300){
direction = "left";
// window.document.bee.src = "bee_left.gif";
}
if (new_left < 30){
direction = "right";
}
obj.style.left = new_left + "px";
the_timeout = setTimeout(function(){ moveDiv(); },10);
}
}
/*]]>*/
</script></head>

<body onload="moveDiv();">
<div id="myDiv" style="position:absolute;left:110px;top:10px;width:20px;height:20px;background-Color:red; " ></div>
</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum