筋斗云

var list=myTab("navBar").children;
var liWidth=0;

for(var i=0;i<list.length;i++){
list[i].onmouseover=function(){
liWidth=this.offsetLeft;
animate(myTab(“cloud”),liWidth);
console.log(liWidth);
}
list[i].onmouseout=function(){
liWidth=0;
animate(myTab(“cloud”),liWidth);
}
list[i].onclick=function(){
liWidth=this.offsetLeft;
// animate(myTab(“cloud1”),liWidth);
myTab(“cloud1”).style.left=liWidth+“px”;
}

}
function animate(ele,target) {
clearInterval(ele.timeId);
ele.timeId=setInterval(function () {
//获取元素当前位置
var current=ele.offsetLeft;
//移动步数
var step=(target-current)/10;
step=step>0 ? Math.ceil(step):Math.floor(step);
current+=step;
ele.style.left=current+“px”;
if(current==target){
clearInterval(ele.timeId);
}
//测试代码
console.log(“目标位置”+target+",当前的位置"+current+"," +
“每次移动的步数”+step)
},20);
}

猜你喜欢

转载自blog.csdn.net/qq_43469418/article/details/86499839