来源:2-1 JS速度动画

gzw19931217

2014-09-24 17:28


请大神帮我看下这个代码为什么动不起来

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js动画制作</title>
</head>
<style>
body,div,span{
    margin:0;
    padding:0;
    
}
#div1{
    height: 200px;
    width: 200px;
    background-color: #333;
    position: relative;
    left: -200px;
    top: 0;
}
#div1 span{
    height:50px;
    width:20px;
    background-color:#666;
    position: absolute;
    left:200px;
    top:75px;
}
</style>
<script>
window.onload = function(){
    var oDiv = document.getElementById('div1');
    oDiv.onmouseover = function(){
        startMove(0);
        }
    oDiv.onmouseout = function(){
        startMove(-200);
        }
}
var timer = null;
function startMove(iTarget){
    clearInterval(timer);
    var oDiv = document.getElementById('div1');
    timer = setInterval(function(){
        var speed=0;
        if(oDiv.offsetLeft<iTaget){speed = 10;}
        else{speed = -10;}
        if(oDiv.offsetLeft == iTarget){
            clearInterval(timer);
            }
        else{
        oDiv.style.left = oDiv.offsetLeft+ speed +'px';
            }
        },30)    
}
</script>

<body>

<div id="div1"><span id="share">分享</span></div>
</body>
</html>


写回答 关注

2回答

  • gzw19931217
    2014-09-26 16:28:09

    额,粗心了,多谢

  • 火红
    2014-09-25 11:39:57

     if(oDiv.offsetLeft<iTaget){speed = 10;}    iTarget  少了个r

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113916 学习 · 1502 问题

查看课程

相似问题