开发者

Need help with animation on JavaScript without jQuery

I have got a div object for example (Simple Image), and I need my picture to move to right and left and backward without any frame and border. My picture will collide with end (right side of my page) and move backward to left and then action with right side too. This will be only on JavaScript.

Edit:

<html>
<head>
<script> 
function dvig () { 
    var xx = 50; 
    var corner_left=200 +'px'; 
    var corner_right=100 +'px'; 
    var move; var move2; 
    var dv=document.getElementById("adam"); 
    if(dv.style.left<=corner_right) { 
        move=1; 
        move2=1; 
    } 
    if(dv.style.left>=corner_left) {
        move2=0; 
        move=0; 
    } 
    if(move=1) { 
        dv.style.left=parseInt(document.getElementById("adam").style.left)+xx; 
    } 
    if (move2=0) { 
        move=0; 
        dv.style.left=parseInt(document.getElementById("adam").style.left)-xx; 
    } 
} 
function chustro() { 
    setInterval("dvig()", 100)开发者_如何学C; 
} 
</script> 
</head>
<body onload="chustro()">
    <img id="adam" style="position: absolute; top: 100px; left: 100px; width: 40px; height: 40px; background-color: red">
</body>
</html>


Your going to have to setup a iterative process that runs every X milliseconds. Each time its run you have to update the div's position a bit and check if it hits a side and reverse direction.

function doWork{
    // move current direction a little

    // check if past the left edge and change directions

    // check if past the right edge and change direction
}

setInterval(doWork, 10);

Update:

Based on your code, check this example: http://jsfiddle.net/HT9A4/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜