时间:2021-07-01 10:21:17 帮助过:4人阅读
//鼠标移到元素上元素右移,鼠标离开元素回去。
var timer="";
function Move(locat) {//移动终点位置
var ob=document.getElementById('box1');
clearInterval(timer);
timer=setInterval(function () {
var speed=(locat-ob.offsetLeft)/10;//speed的大小和移动距离成正比,分母控制缓冲的快慢,即比例系数K,可调整
speed=speed>0?Math.ceil(speed):Math.floor(speed);//凡是缓冲运动速度一定要取整!!!向右运动时坐标向上取整,向左运动时坐标向下取整
if (ob.offsetLeft==locat) {//当前位置到达指定终点,关闭定时器
clearInterval(timer);
} else {
ob.style.left=ob.offsetLeft+speed+'px';
}
}, 30)
}在下面的HTML文档里调用上面的JS函数。还用上次的那个div为例:
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#box1{
width: 200px;
height: 200px;
background-color: red;
position: absolute;
left: 0;
}
</style><div id="box1"></div>
<script type="text/javascript">
window.onload=function(){
var ob=document.getElementById('box1');
ob.onmouseover=function(){
Move(200);
}
ob.onmouseout=function(){
Move(0);
}
}
</script>以上就是js动画学习(二)的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!