Advanced Computer

Javascript

Assignment 6    -   Move a picture Left to Right

 

LEFTRIGHT.HTML

<html>

                <head>

                <title>Hello World</title>

 

</head>

 

<body>

 

<div id='lyr' img = "conductor.gif" style='position:absolute; top:40px; left:20px;

width:80px; height:80px; background-color:purple;

color:white; z-index:10; border:3px solid black'>

<IMG SRC="conductor.gif"></div>

 

<script type= "text/javascript" src= "leftright.js">

</script>

 

<a href = "javascript://" onmouseover = "left()"

onmouseout = "stop()">Left</a>

 

<a href = "javascript://" onmouseover = "right()"

onmouseout = "stop()">Right</a>

 

</body>

               

</html>

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


LEFTRIGHT.JS

 

// variable for timer

var timer;

 

function left()

{

                var obj = document.getElementById("lyr");

 

                obj.style.left = (parseInt(obj.style.left)-1)+"px";

 

                timer = window.setTimeout("left()", 50);

}

 

function right()

{

                var obj = document.getElementById("lyr");

 

                obj.style.left = (parseInt(obj.style.left)+1)+"px";

 

                timer = window.setTimeout("right()", 50);

}

 

function stop()

{             

                clearTimeout(timer);

}