为了账号安全,请及时绑定邮箱和手机立即绑定

第一次写的JS动画,不知道为什么动不了

第一次写的JS动画,不知道为什么动不了

Mrshoushou 2016-10-24 22:02:06
<!DOCTYPE html><html><head> <title>JS Flash</title> <meta charset="utf-8"> <style type="text/css"> body,div,span{ margin: 0; padding: 0;}#div1{ width: 200px; height: 200px; background: red; position: relative; left: -200px; top:0; }#div1 span{ width: 20px; height: 50px; background: blue; position: absolute; left: 200px; top: 75px;} </style> <script type="text/javascript"> window.onload=function(){  var oDiv=document.getElementById('div1');  oDiv.onmouseover=function(){  startMove();  }  oDiv.onmouseover = function(){  startMove1();  } } var timer=null; function startMove(){  clearInterval(timer);  setInterval(function(){  var oDiv=document.getElementById('div1');  if(oDiv.offsetLeft == 0){  clearInterval(timer);  }  else{  oDiv.style.left=oDiv.offsetLeft+10+'px';  }  },30) } function startMove1(){  clearInterval(timer);  setInterval(function(){  var oDiv=document.getElementById('div1');  if(oDiv.offsetLeft == -200){  clearInterval(timer);  }  else{  oDiv.style.left=oDiv.offsetLeft-10+'px';  }  },30) } </script></head><body><div id="div1"> <span id="share">分享</span></div></body></html>
查看完整描述

3 回答

已采纳
?
李晓健

TA贡献1036条经验 获得超461个赞


oDiv.onmouseover=function(){
    startMove();
}
oDiv.onmouseover = function(){
    startMove1();
}
//因为后面一个onmouseover 会覆盖前面一个 onmouseover ,所以你就看不到效果


查看完整回答
1 反对 回复 2016-10-24
  • Mrshoushou
    Mrshoushou
    谢谢,谢谢。。。。真是太感谢了,继续努力奋斗在前端自学路上
  • Mrshoushou
    Mrshoushou
    大神,我把鼠标移出事件改成了onmouseout 后,测试的确能动了,但是在抽风,前后前后的跳动。。。。求解
  • 李晓健
    李晓健
    因为你的 timmer没有用呀,你只是定义了,没有赋值,所以定时是没有取消,却一直在累加,就会出现很多的定时器同时执行,就会出现抽风的情况。 大概应该这是样的 timer = setInterval(function(){xxxxx},30)
点击展开后面2
  • 3 回答
  • 0 关注
  • 2064 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信