jquery 下拉菜单。保持选择顶部项目而不冒泡
我有一个下拉菜单,其中有淡入淡出的背景图像,通过具有 a 的同级跨度并具有父 li 的完整尺寸来完成。 html和css如下。
<div id='header'>
<ul>
<li><span></span><a href='#' title='' class='a1'>Pianos</a>
<ul>
<li><span></span><a href='#' title='' class='a1'>New</a></li>
<li><span></span><a href='#' title='' class='a1'>Used</a></li>
</ul>
</li>
<li><span></span><a href='#' title='' class='a1'>Accessories</a></li>
<li><span></span><a href='#' title='' class='a1'>Locations</a></li>
<li><span></span><a href='#' title='' class='a1'>Contact</a></li>
</ul>
</div>
*
{
margin:0px;
padding:0px;
position:relative;
}
#header>ul li
{
display:inline-block;
margin-left:-4px;
width:150px;
height:100%;
border-right:2px solid #000000;
text-align:center;
}
#header>ul li>a
{
display:block;
padding-top:5px;
}
#header>ul li>span
{
display:block;
position:absolute;
top:0px;
left:0px;
width:100%;
height:100%;
background:url('naviOn.jpg');
display:none;
}
#header>ul>li>ul
{
display:none;
position:absolute;
top:40px;right:0px;
background:url('naviOff.jpg');
}
#header>ul>li>ul>li
{
border:none;
height:40px;
}
我的 jquery 所做的是它在您悬停的 a 的前一个兄弟跨度中淡入淡出。另外,如果 li 里面有 ul,它会向下滑动。我遇到的问题是当我将鼠标悬停在下拉元素上时使顶部跨度保持不变。目前它可以工作,但如果我仅将鼠标悬停在顶部元素上,则跨度将保持隐藏状态,直到我将鼠标悬停在下拉锚点上。 jquery代码如下。
$('#header>ul a').hover(function()
{
$(this).prev('span').stop(true, true).fadeToggle(500);
}, function()
{
$(this).prev('span').stop(true, true).fadeToggle(500);
})
$('#header>ul>li').hover(function()
{
$(this).children('ul').stop(true, true).slideToggle(250);
$(this).children('span').css({display: 'block'});
},function()
{
$(this).children('ul').stop(true, true).slideToggle(250);
$(this).children('span').fadeOut(500);
})
关于如何让它发挥作用有什么想法吗?
I have a drop down menu that has fading background images done by haveing a span that is a sibling of the a and has the full demensions of the parent li. the html and css are as follows.
<div id='header'>
<ul>
<li><span></span><a href='#' title='' class='a1'>Pianos</a>
<ul>
<li><span></span><a href='#' title='' class='a1'>New</a></li>
<li><span></span><a href='#' title='' class='a1'>Used</a></li>
</ul>
</li>
<li><span></span><a href='#' title='' class='a1'>Accessories</a></li>
<li><span></span><a href='#' title='' class='a1'>Locations</a></li>
<li><span></span><a href='#' title='' class='a1'>Contact</a></li>
</ul>
</div>
*
{
margin:0px;
padding:0px;
position:relative;
}
#header>ul li
{
display:inline-block;
margin-left:-4px;
width:150px;
height:100%;
border-right:2px solid #000000;
text-align:center;
}
#header>ul li>a
{
display:block;
padding-top:5px;
}
#header>ul li>span
{
display:block;
position:absolute;
top:0px;
left:0px;
width:100%;
height:100%;
background:url('naviOn.jpg');
display:none;
}
#header>ul>li>ul
{
display:none;
position:absolute;
top:40px;right:0px;
background:url('naviOff.jpg');
}
#header>ul>li>ul>li
{
border:none;
height:40px;
}
What my jquery does is it fade in the span that is the previous sibling of the a that you are hovering over. additionally if there is a ul inside the li it will slideDown. what i'm having trouble with is making the top span stay as i hover over the drop down elements. currently it works but if i hover ofer just the top element it the span will remain hidden until i hover over the drop down anchors. jquery code below.
$('#header>ul a').hover(function()
{
$(this).prev('span').stop(true, true).fadeToggle(500);
}, function()
{
$(this).prev('span').stop(true, true).fadeToggle(500);
})
$('#header>ul>li').hover(function()
{
$(this).children('ul').stop(true, true).slideToggle(250);
$(this).children('span').css({display: 'block'});
},function()
{
$(this).children('ul').stop(true, true).slideToggle(250);
$(this).children('span').fadeOut(500);
})
any ideas on how to get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将背景淡入淡出放在
li
上而不是放在a
标记上,当您将鼠标悬停在子菜单项上时,它仍然会被激活,因为它们位于李
。尝试用以下代码替换现有的 jQuery 代码:If you put your background fading on the
li
instead of on thea
tag it'll still be activated when you hover over the sub menu items because they're inside of theli
. Try replacing your existing jQuery code with the following code: