Js缓冲运动代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.wrapper{
width: 400px;
height: 80px;
cursor: pointer;
background: rgb(0, 204, 255);
position: absolute;
left: -400px;
top: 20px;
}
.wrapper span{
width: 50px;
height: 80px;
background: #008c8c;
position: absolute;
right: -50px;
top: 0px;
}
</style>
</head>
<body>
<div class="wrapper">
<span></span>
</div>
<script>
var timer = null;
var oDiv = document.getElementsByClassName('wrapper')[0];
oDiv.onmouseover = function(){
console.log("over");
startMove(this,0);
}
oDiv.onmouseleave = function(){
console.log("leave");
startMove(this,-400)
}
function getStyle(dom,attr){
if(window.getComputedStyle){
return window.getComputedStyle(dom,null)[attr];
}else{
return dom.currentStyle[attr];
}
}
function startMove(dom,target){
clearInterval(timer);
timer = setInterval(function(){
iSpeed = (target - oDiv.offsetLeft)/7;
iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
if(oDiv.offsetLeft == target){
clearInterval(timer);
}
oDiv.style.left = oDiv.offsetLeft + iSpeed + 'px';
},30);
}
</script>
</body>
</html>