问答详情
源自:4-1 JS多物体动画

请问哪里不对呢?

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

<script>

window.onload=function(){

var x=document.getElementById("1st");

x.onmouseover=function(){

startmove(0)

}

x.onmouseout=function(){

startmove(-480)

}

}

var timmer=null;

function startmove(target){

clearInterval(timmer)

var x=document.getElementById("1st");

setInterval(function(){

var speed=0;

speed=(target-x.offsetLeft)/10

speed=speed>0?Math.ceil(speed):Math.floor(speed);

if(x.offsetLeft==target){

clearInterval(timmer)

}else{

x.style.left=x.offsetLeft+speed+"px";

}

},30)

}

</script>

</head>


<body>

<div id="1st" style="position: absolute; width: 500px; height: 100px; left: -480px; top: 262px;background:#00F"></div>

</body>

</html>


提问者:qq_Desperado_19 2016-09-05 19:25

个回答

  • 绝望之巅
    2016-09-05 20:00:02
    已采纳

    定时器那里, timmer = setInterval(