gpt4 book ai didi

javascript - jquery悬停功能显示和隐藏元素

转载 作者:行者123 更新时间:2023-11-28 05:22:32 25 4
gpt4 key购买 nike

我有一个悬停功能,可以显示和隐藏我的子菜单,直到有人悬停在它们上面。

问题是,当我尝试将鼠标向下移动到显示它的菜单时,它又消失了。

谁能指出我在这里做错了什么?

$(document).ready(function() {
$('.menu-Link > a').hover(function() {
$(this).next('.menu').show();
}, function() {
$(this).next('.menu').hide();
});
});
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
/*background-color: #333333;*/
background-color: transparent;
transition: 0.2s;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: transparent;
display: inline-block;
}
nav ul li {
float: left;
}
nav ul li a.link {
display: block;
color: black;
text-align: center;
padding: 24px 16px;
text-decoration: none;
transition: 0.2s;
font-weight: bold;
text-transform: uppercase;
position: relative;
}
nav ul li div.arrow-down {
float: right;
width: 0;
height: 0;
margin-left: 5px;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 5px solid #fff;
margin-top: 10px;
transition: 0.2s;
}
nav ul li div.menu {
display: none;
z-index: 999;
position: relative;
background-color: #fff;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
padding: 15px 0;
margin-top: -15px;
}
nav ul li div.menu a {
color: #333333;
padding-left: 15px;
}
nav ul li a.link:hover {
color: #808080;
text-decoration: none;
transition: 0.2s;
}
nav ul li a.link:hover div.arrow-down {
border-top: 5px solid #808080;
transition: 0.2s;
}
nav ul li:first-child {
padding-left: 100px;
}
nav ul.right {
float: right;
}
nav ul.right li:last-child {
padding-right: 100px;
}
nav.color {
background-color: #333333;
transition: 0.2s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul class="left">
<li class="menu-Link">
<a href="" class="link">Home</a>
<div class="menu">
<a href="">subcat.Title</a>
</div>
</li>
<li class="menu-Link">
<a href="" class="link">Test</a>
<div class="menu">
<a href="">subcat.Title</a>
</div>
</li>
</ul>
<ul class="right">
<li><a href="/">Basket</a>
</li>
</ul>
</nav>

最佳答案

问题是因为事件附加到 a 元素,因此当用户尝试将鼠标悬停在出现的菜单上时,将调用悬停的 mouseout 部分a 和 JS 代码试图再次隐藏菜单。要解决此问题,请将 hover() 事件附加到包含 a 和菜单的元素。在这种情况下,.menu-link 元素本身。您还需要将 next() 更改为 find() 以适应更改。试试这个:

$(document).ready(function() {
$('.menu-Link').hover(function() {
$(this).find('.menu').show();
}, function() {
$(this).find('.menu').hide();
});
});
nav {
position: fixed;
top: 0;
left: 0;
width: 100%;
/*background-color: #333333;*/
background-color: transparent;
transition: 0.2s;
}
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: transparent;
display: inline-block;
}
nav ul li {
float: left;
}
nav ul li a.link {
display: block;
color: black;
text-align: center;
padding: 24px 16px;
text-decoration: none;
transition: 0.2s;
font-weight: bold;
text-transform: uppercase;
position: relative;
}
nav ul li div.arrow-down {
float: right;
width: 0;
height: 0;
margin-left: 5px;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 5px solid #fff;
margin-top: 10px;
transition: 0.2s;
}
nav ul li div.menu {
display: none;
z-index: 999;
position: relative;
background-color: #fff;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.2);
padding: 15px 0;
margin-top: -15px;
}
nav ul li div.menu a {
color: #333333;
padding-left: 15px;
}
nav ul li a.link:hover {
color: #808080;
text-decoration: none;
transition: 0.2s;
}
nav ul li a.link:hover div.arrow-down {
border-top: 5px solid #808080;
transition: 0.2s;
}
nav ul li:first-child {
padding-left: 100px;
}
nav ul.right {
float: right;
}
nav ul.right li:last-child {
padding-right: 100px;
}
nav.color {
background-color: #333333;
transition: 0.2s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul class="left">
<li class="menu-Link">
<a href="" class="link">Home</a>
<div class="menu">
<a href="">subcat.Title</a>
</div>
</li>
<li class="menu-Link">
<a href="" class="link">Test</a>
<div class="menu">
<a href="">subcat.Title</a>
</div>
</li>
</ul>
<ul class="right">
<li><a href="/">Basket</a>
</li>
</ul>
</nav>

另请注意,您可以单独在 CSS 中实现此目的,而无需任何 JS 代码,只需删除 hover() 处理程序并将此行添加到您的 CSS 中:

nav ul li:hover div.menu {
display: block;
}

关于javascript - jquery悬停功能显示和隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36834221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com