请大家帮忙看看这两段代码

yzc-c 2015-10-12 11:17:10
这是一个关于导航条鼠标触碰伸缩的JS,第一个可以实现,但第二个我把函数从循环里拿出来后就不行了,看了好久也没找到原因,请大家看下,求指教!


window.onload=function(){
var aA=document.getElementsByTagName('a');
for(var i=0; i<aA.length; i++){
aA[i].onmouseover=function(){
var This=this;
clearInterval(This.time);
This.time= setInterval(function(){
This.style.width=This.offsetWidth+8+"px";
if(This.offsetWidth>=160)
clearInterval(This.time);
},30);
};
aA[i].onmouseout=function(){
clearInterval(this.time);
var This=this;
this.time=setInterval(function(){
This.style.width=This.offsetWidth-8+"px";
if(This.offsetWidth<=120){
This.style.width='120px';
clearInterval(This.time);
}
},30);
};
}
};



window.onload=function(){
var aA=document.getElementsByTagName('a');
for(var i=0; i<aA.length; i++){
aA[i].onmouseover(mouseOver());
aA[i].onmouseout(mouseOut());
}

function mouseOver() {
var This=this;
clearInterval(This.time);
This.time= setInterval(function(){
This.style.width=This.offsetWidth+8+"px";
if(This.offsetWidth>=160)
clearInterval(This.time);
},30);
}

function mouseOut(){
var This=this;
clearInterval(this.time);
this.time=setInterval(function(){
This.style.width=This.offsetWidth-8+"px";
if(This.offsetWidth<=120){
This.style.width='120px';
clearInterval(This.time);
}
},30);
}
};
...全文
95 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
u011294633 2015-10-12
  • 打赏
  • 举报
回复
window.onload=function(){

var aA=document.getElementsByTagName('a');

for(var i=0; i<aA.length; i++){

aA[i].onmouseover=function(){mouseOver(aA[i]);}

aA[i].onmouseout=function(){mouseOut(aA[i]);}

}



function mouseOver(y) {

var This=y;

clearInterval(This.time);

This.time= setInterval(function(){

This.style.width=This.offsetWidth+8+"px";

if(This.offsetWidth>=160)

clearInterval(This.time);

},30);

}



function mouseOut(y){

var This=y;

clearInterval(this.time);

this.time=setInterval(function(){

This.style.width=This.offsetWidth-8+"px";

if(This.offsetWidth<=120){

This.style.width='120px';

clearInterval(This.time);

}

},30);

}

};
llfylwg 2015-10-12
  • 打赏
  • 举报
回复
aA[i].onmouseover(mouseOver());

aA[i].onmouseout(mouseOut());
把这两个改为

aA[i].onmouseover=mouseOver;

aA[i].onmouseout=mouseOut;

87,955

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧