87,993
社区成员
发帖
与我相关
我的任务
分享<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>鼠标移入移出定时移动效果</title>
<style>
*{margin:0;padding:0;}
#aa{width:200px;height:200px;background:red;position:relative;left:-200px;top:0;}
#aa span{width:20px;height:50px;background:blue;position:absolute;left:200px;top:75px;color:#fff;}
</style>
</head>
<body>
<div id="aa"><span>分享</span></div>
<script>
var aa = document.getElementById("aa");
var time = null;
aa.onmouseenter=function(){
clearInterval(time);
time=setInterval(function(){
if(aa.offsetLeft==0){
clearInterval(time);
}else{
aa.style.left=aa.offsetLeft+10+"px";
}
},20);
};
aa.onmouseleave=function(){
clearInterval(time);
time = setInterval(function(){
if(aa.offsetLeft==-200){
clearInterval(time);
}else{
aa.style.left=aa.offsetLeft-10+"px";
}
},20);
}
</script>
</body>
</html>
var aa = document.getElementById("aa");
var time = null;
function animate(a,b){
clearInterval(time);
time=setInterval(function(){
if(aa.offsetLeft==a){
clearInterval(time);
}else{
aa.style.left=aa.offsetLeft+b+"px";
}
},20);
};
aa.onmouseenter=animate.bind(null,0,10);
aa.onmouseleave=animate.bind(null,-200,-10);
function MEevent(offset,step,delay) {
clearInterval(time);
time = setInterval(function () {
if (aa.offsetLeft == offset) {
clearInterval(time);
} else {
aa.style.left = aa.offsetLeft + step + "px";
}
}, delay);
}
var aa = document.getElementById("aa");
var time = null;
aa.onmouseenter = aa.onmouseleave = function (e) {
var enter = (e || window.event).type == 'mouseenter';
MEevent(enter ? 0 : -200, enter ? 10 : -10, 20);
}