保持后台活动状态为悬停?

发布于 2024-10-07 03:46:00 字数 4742 浏览 0 评论 0原文

我有一个无序列表,在 LI 内,其下方是下拉菜单。

Dropdown 使用 JQ/JS 来隐藏/显示状态。当鼠标悬停在列表项上时,当它下拉时,我怎样才能让背景颜色/图像停留在该项目(主LI)上,就像我在活动状态下一样,直到光标移出下拉区域。

这是否需要创建另一个 DIV 块来包含所有元素? (Menu,Submenu,JS) 如果是的话,那么另外几行 JS 如果是这样的话?

或者有没有更简单的方法来用 css 实现这一点..因为我想不出办法。 希望我的问题很清楚,请忽略所有不必要的 HTML 和 CSS 代码,

谢谢。

JS

$(function () {
$('.dropdown, .dropdown2, .dropdown3').each(function () {
$(this).parent().eq(0).hover(function () {
$('.dropdown, .dropdown2, .dropdown3:eq(0)', this).show();
}, function () {
$('.dropdown, .dropdown2, .dropdown3:eq(0)', this).hide();
});
});
});

HTML

<ul id="nav-container">
    <li><a href="#"><span>Home</span></a>
    <ul class="dropdown">
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    </ul>
    </li>

    <li><a href="#"><span>Services</span></a>
    <ul class="dropdown2">
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    </ul>
    </li>

    <li><a href="#"><span>Contact</span></a>
    <ul class="dropdown3">
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    </ul>
    </li>
</ul><!--nav-container-->

MENU CSS

#nav-container  {
 display: block;
 width: 900px;
 float: right;
 position: relative;
 left: 120px;
}
#nav-container li a span {
 display: inline;
 height: 36px;
 padding-top: 12px;
 padding-left: 13px;
 padding-right: 14px;
 float: left;
}
#nav-container .active-link span   {
 background-image: url(../img/navlist_bg2.gif);
 background-repeat: repeat-x;
 display: block;
 color: #000;

}

#nav-container li a:hover span {
 display: block;
 background-image: url(../img/navlist_bg2.gif);
 background-repeat: repeat-x;
}
#nav-container .active {
 background-image: url(../img/navlist_bg2.gif);
 background-repeat: repeat-x;
}

#nav-container li  {
 list-style-type: none;
 display: inline;
}

SUB MENU CSS(dropdown2、dropdown3 与刚刚制作的相同用于定位)

ul.dropdown
{
 margin: 0;
 padding: 0;
 display: block;
 position: absolute;
 z-index: 999;
 top: 100%;
 width: 300px;
 display: none;
 left: 80px;
 font-size: 11px;
 height: 90px;
}

ul.dropdown ul.dropdown
{
 top: 0;
 left: 95%;
}


ul.dropdown li
{
 margin: 0;
 padding: 0;
 float: none;
 position: relative;
 list-style: none;
 display: block;
 color: #3C3C3C;
}

ul.dropdown li a
{
 display: block;
 color: #3C3C3C;
 font-size: 11px;
}
ul.dropdown li a span {
 font-size: 11px;
 color: #3C3C3C;
}

I have an unordered list and inside an LI and under that is the dropdown menu.

Dropdown uses JQ/JS for the hide/show states. When hovered over a list item as it drops down how can i have a background-color/image stay on that item (main LI) like i would with active states, until the cursor has moved out of the Dropdown area.

Would this require creating another DIV block to contain all the elements? (Menu,Submenu,JS) And if so another few lines of JS if thats the case?

or is there a simpler way to achieve this with css.. as i cannot think of a way.
Hope my question is clear and please disregard all the unnecessary HTML and CSS code,

Thanks.

JS

$(function () {
$('.dropdown, .dropdown2, .dropdown3').each(function () {
$(this).parent().eq(0).hover(function () {
$('.dropdown, .dropdown2, .dropdown3:eq(0)', this).show();
}, function () {
$('.dropdown, .dropdown2, .dropdown3:eq(0)', this).hide();
});
});
});

HTML

<ul id="nav-container">
    <li><a href="#"><span>Home</span></a>
    <ul class="dropdown">
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    </ul>
    </li>

    <li><a href="#"><span>Services</span></a>
    <ul class="dropdown2">
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    </ul>
    </li>

    <li><a href="#"><span>Contact</span></a>
    <ul class="dropdown3">
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    <li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
    </ul>
    </li>
</ul><!--nav-container-->

MENU CSS

#nav-container  {
 display: block;
 width: 900px;
 float: right;
 position: relative;
 left: 120px;
}
#nav-container li a span {
 display: inline;
 height: 36px;
 padding-top: 12px;
 padding-left: 13px;
 padding-right: 14px;
 float: left;
}
#nav-container .active-link span   {
 background-image: url(../img/navlist_bg2.gif);
 background-repeat: repeat-x;
 display: block;
 color: #000;

}

#nav-container li a:hover span {
 display: block;
 background-image: url(../img/navlist_bg2.gif);
 background-repeat: repeat-x;
}
#nav-container .active {
 background-image: url(../img/navlist_bg2.gif);
 background-repeat: repeat-x;
}

#nav-container li  {
 list-style-type: none;
 display: inline;
}

SUB MENU CSS (dropdown2, dropdown3 are the same just made for positioning)

ul.dropdown
{
 margin: 0;
 padding: 0;
 display: block;
 position: absolute;
 z-index: 999;
 top: 100%;
 width: 300px;
 display: none;
 left: 80px;
 font-size: 11px;
 height: 90px;
}

ul.dropdown ul.dropdown
{
 top: 0;
 left: 95%;
}


ul.dropdown li
{
 margin: 0;
 padding: 0;
 float: none;
 position: relative;
 list-style: none;
 display: block;
 color: #3C3C3C;
}

ul.dropdown li a
{
 display: block;
 color: #3C3C3C;
 font-size: 11px;
}
ul.dropdown li a span {
 font-size: 11px;
 color: #3C3C3C;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_失温 2024-10-14 03:46:00

JS:

$(function () {
$('.menu1').hover(function () {
$('.link').addClass('active');
$('.dropdown1').show;
}, function () {
$('.dropdown1').hide;
$('.link').removeClass('active');
});
});
});

HTML:

<li class="menu1"><a class="link" href="#"><span>Home</span></a>
<ul class="dropdown1">
<li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
<li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
</ul>
</li>

对所有基本菜单项(hover1、2、3 等)重复此操作

JS:

$(function () {
$('.menu1').hover(function () {
$('.link').addClass('active');
$('.dropdown1').show;
}, function () {
$('.dropdown1').hide;
$('.link').removeClass('active');
});
});
});

HTML:

<li class="menu1"><a class="link" href="#"><span>Home</span></a>
<ul class="dropdown1">
<li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
<li><a href="#"><span class="subtxt">Menu item 2</span></a></li>
</ul>
</li>

repeat this for all base menu items (hover1, 2, 3, etc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文