jquery:附加一个div会导致鼠标悬停/鼠标悬停循环
我有一个用于导航菜单的 ul。当用户将鼠标悬停在子 li 上时,我会向 li 附加一个类为“nav-about”的 div(或 nav-无论该 li 的类是什么),然后向下滑动附加的 div。我遇到一个问题,我的代码陷入循环,并且 li 的鼠标悬停/鼠标移出来回切换,直到我将鼠标移出 li。感谢您提供的所有帮助。
html
<div id="nav" class="nav">
<ul id="header-menu" class="mainnav ">
<li class="about">
<a class="" id="about" href="http://www.example.com/projects/joanroot/about/">About</a>
</li>
<li class="paintings">
<a class="" id="paintings" href="http://www.example.com/projects/joanroot/paintings/">Paintings</a>
</li>
<li class="worksonpaper">
<a class="" id="worksonpaper" href="http://www.example.com/projects/joanroot/works-on-paper/">Works On Paper</a>
</li>
<li class="lectureseries">
<a class="" id="lectureseries" href="http://www.example.com/projects/joanroot/lecture-series/">Lecture Series</a>
</li>
<li class="contact">
<a class="" id="contact" href="http://www.example.com/projects/joanroot/contact/">Contact</a>
</li>
</ul>
</div>
css
div#nav {
overflow: hidden;
}
.mainnav, #nav > ul {
background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 0 transparent;
float: left;
list-style: none outside none;
margin: 0;
padding: 0;
width: 973px;
}
#nav > ul > li {
display: inline;
}
#nav > ul > li > a {
display: block;
float: left;
height: 54px;
overflow: hidden;
text-indent: -9999px;
z-index: 100;
}
#nav ul li a#about {
width: 154px;
}
div.nav-about {
background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 -55px transparent;
height: 54px;
overflow: hidden;
position: absolute;
width: 154px;
}
jquery
InitilizeMenu('#header-menu',400,'slide','slide');
function InitilizeMenu(parent,moveSpeed,inStyle,outStyle) {
$(parent).children('li').each(function() {
var thisClass = $(this).attr('class');
AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle);
});
}
function AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle) {
$(parent+' .'+thisClass).mouseover(function() {
$(this).append('<div class="nav-'+thisClass+'"></div>')
switch(inStyle) {
case 'slide':
$('div.nav-'+thisClass).css({display:'none'}).slideDown(moveSpeed);
break;
case 'fade':
$('div.nav-'+thisClass).css({display:'none'}).fadeIn(moveSpeed);
break;
case 'none':
$('div.nav-'+thisClass);
break;
default:
$('div.nav-'+thisClass);
break;
}
}).mouseout(function(){
switch(outStyle) {
case 'slide':
$('div.nav-'+thisClass).slideUp(moveSpeed,function(){
$(this).remove();
});
break;
case 'fade':
$('div.nav-'+thisClass).fadeOut(moveSpeed,function(){
$(this).remove();
});
break;
case 'none':
$('div.nav-'+thisClass).remove();
break;
default:
$('div.nav-'+thisClass).remove();
break;
}
});
}
I have a ul that I'm using for a nav menu. When a user mouses over the child li, I append to the li a div with the class of "nav-about" (or nav-whatever the class is for that li), and slideDown the appended div. I'm having a problem where my code get's caught in a loop and the li's mouseover/mouseout toggle back and forth until I move the mouse out of the li. Thank you for any and all help.
html
<div id="nav" class="nav">
<ul id="header-menu" class="mainnav ">
<li class="about">
<a class="" id="about" href="http://www.example.com/projects/joanroot/about/">About</a>
</li>
<li class="paintings">
<a class="" id="paintings" href="http://www.example.com/projects/joanroot/paintings/">Paintings</a>
</li>
<li class="worksonpaper">
<a class="" id="worksonpaper" href="http://www.example.com/projects/joanroot/works-on-paper/">Works On Paper</a>
</li>
<li class="lectureseries">
<a class="" id="lectureseries" href="http://www.example.com/projects/joanroot/lecture-series/">Lecture Series</a>
</li>
<li class="contact">
<a class="" id="contact" href="http://www.example.com/projects/joanroot/contact/">Contact</a>
</li>
</ul>
</div>
css
div#nav {
overflow: hidden;
}
.mainnav, #nav > ul {
background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 0 transparent;
float: left;
list-style: none outside none;
margin: 0;
padding: 0;
width: 973px;
}
#nav > ul > li {
display: inline;
}
#nav > ul > li > a {
display: block;
float: left;
height: 54px;
overflow: hidden;
text-indent: -9999px;
z-index: 100;
}
#nav ul li a#about {
width: 154px;
}
div.nav-about {
background: url("http://www.example.com/projects/joanroot/images/main-menu.gif") no-repeat scroll 0 -55px transparent;
height: 54px;
overflow: hidden;
position: absolute;
width: 154px;
}
jquery
InitilizeMenu('#header-menu',400,'slide','slide');
function InitilizeMenu(parent,moveSpeed,inStyle,outStyle) {
$(parent).children('li').each(function() {
var thisClass = $(this).attr('class');
AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle);
});
}
function AttachNavEvents(parent,thisClass,moveSpeed,inStyle,outStyle) {
$(parent+' .'+thisClass).mouseover(function() {
$(this).append('<div class="nav-'+thisClass+'"></div>')
switch(inStyle) {
case 'slide':
$('div.nav-'+thisClass).css({display:'none'}).slideDown(moveSpeed);
break;
case 'fade':
$('div.nav-'+thisClass).css({display:'none'}).fadeIn(moveSpeed);
break;
case 'none':
$('div.nav-'+thisClass);
break;
default:
$('div.nav-'+thisClass);
break;
}
}).mouseout(function(){
switch(outStyle) {
case 'slide':
$('div.nav-'+thisClass).slideUp(moveSpeed,function(){
$(this).remove();
});
break;
case 'fade':
$('div.nav-'+thisClass).fadeOut(moveSpeed,function(){
$(this).remove();
});
break;
case 'none':
$('div.nav-'+thisClass).remove();
break;
default:
$('div.nav-'+thisClass).remove();
break;
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用 .hover() 代替:
Why not use .hover() instead: