javascript运动功能-分享到
<script> //窗体载入,为div控件绑定事件 window.onload = function () { var div1 = document.getElementById('div1'); //mouseover div1.onmouseover = function () { startMove(0); } //mouseout div1.onmouseout = function () { startMove(-100); } } //定义定时器 var timer = null; //淡入和淡出 function startMove(targetPoint) { clearInterval(timer); var div1 = document.getElementById('div1'); timer = setInterval(function () { var ispeed = 0; if (div1.offsetLeft < targetPoint) { ispeed = 10; } else { ispeed = -10; } //控件位置 if (div1.offsetLeft == targetPoint) { clearInterval(timer); } else { div1.style.left = div1.offsetLeft + ispeed + "px"; } },30); } </script>
请关注微信公众号“跟我学前端”,谢谢大家!