
本文实例讲述了JS运动框架之分享侧边栏动画实现方法。分享给大家供大家参考。具体实现方法如下:
代码如下:
window.onload=function(){
var oDiv=document.getElementById("div1");
var oSpan=oDiv.getElementsByTagName('span')[0];
var time=null;
var speed=8;
oDiv.onmouseover=function(){//这里给整个div加鼠标移入的事件
clearInterval(time);
time=setInterval(function(){
if(oDiv.offsetLeft>=0){clearInterval(time);}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},1);
}
oDiv.onmouseout=function(){//这里给整个div加鼠标移出事件
clearInterval(time);
time=setInterval(function(){
if(oDiv.offsetLeft<=-320){clearInterval(time);}
else{
oDiv.style.left=oDiv.offsetLeft-speed+'px';
}
},1);
}
}
script>
分享到
优化后的代码:
代码如下:
window.onload=function(){
var oDiv=document.getElementById("div1");
var oSpan=oDiv.getElementsByTagName('span')[0];
var time=null;
var spe=8;
var speed=null;
function move(bord){
clearInterval(time);
time=setInterval(function(){
if(oDiv.offsetLeft>bord){speed=-spe;}
else{speed=spe;}
if(oDiv.offsetLeft==bord){clearInterval(time);}
else{
oDiv.style.left=oDiv.offsetLeft+speed+'px';
}
},1);
}
oDiv.onmouseover=function(){//这里给整个div加鼠标移入的事件
move(0);
}
oDiv.onmouseout=function(){//这里给整个div加鼠标移出事件
move(-320);
}
}
script>
分享到
希望本文所述对大家的javascript程序设计有所帮助。