懒人一只
2016-03-11 15:10
<div class="hide" id="hide"><div class="hover" id="hover"></div></div> <style> * {margin: 0; padding: 0; } .hide {height: 500px; width: 500px; background-color: red; position: absolute; left: -500px; } .hover {height: 50px; width: 50px; background-color: blue; position: relative; top: 225px; left: 500px; } </style> <script> var hide=document.getElementById('hide'); var hover=document.getElementById('hover'); var timer=null; var startMove=function() { clearInterval(timer); timer=setInterval(function(){ if(hide.offsetLeft<0){ hide.style.left=hide.offsetLeft+10+'px' }else{ clearInterval(timer) }},30 ) }; var endMove=function() { timer=setInterval(function(){ if(hide.offsetLeft>-500){ hide.style.left=hide.offsetLeft-10+'px' }else{ clearInterval(timer) }},30 ) }; hover.onmouseover=function() { startMove(); }; hover.onmouseout=function() { endMove(); }; </script>
hide.offsetLeft<0,hide.offsetLeft>-500
onmouseover和onmouseout绑定在hide元素上
hide.offsetLeft>-500
JS动画效果
113925 学习 · 1443 问题
相似问题