gpt4 book ai didi

javascript - 将悬停区域扩展到外部元素

转载 作者:可可西里 更新时间:2023-11-01 02:08:25 25 4
gpt4 key购买 nike

我有一个下拉菜单,子菜单放在不同的元素上。所以基本上当鼠标离开菜单项时,子菜单会立即关闭,因为子菜单不是子菜单。

var menuItem = $(".menu-item");


menuItem.hover(hoverIn, hoverOut);

function hoverIn() {
var mnItemMeta = $(this)[0].getBoundingClientRect();

$(".sub-menu").css({
opacity: 1,
left: mnItemMeta.left
})
}

function hoverOut() {
$(".sub-menu").css({
opacity: 0
})
}
html,body{background-color: #efefef;}
.menu {
list-style: none;
padding-left: 0;
display: flex;
justify-content: center;
}
a {
display: block;
padding: 10px 20px;
text-decoration: none;
color: inherit;
}
.sub-menu {
opacity: 0;
background-color: white;
position: absolute;
transition: .2s ease;
}
.sub-menu-list {
list-style: none;
padding-left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="menu">
<li class="menu-item"><a href="#">Menu Item</a>
</li>
</ul>
<div class="sub-menu">
<ul class="sub-menu-list">
<li><a href="#">Sub Menu 1</a>
</li>
<li><a href="#">Sub Menu 2</a>
</li>
<li><a href="#">Sub Menu 3</a>
</li>
<li><a href="#">Sub Menu 4</a>
</li>
</ul>
</div>

https://jsfiddle.net/yans_fied/6wj0of90/

问题是如何扩展悬停区域,因此当光标指向子菜单时,它会忽略 hoverOut 操作。

注意:不要告诉我将子菜单放在菜单项中,我已经知道它是如何工作的。这是针对需要将子菜单放置在菜单项之外的不同情况。

最佳答案

您可以将子菜单放在menu-item中。

var menuItem = $(".menu-item");
menuItem.hover(hoverIn, hoverOut);

function hoverIn() {
var mnItemMeta = $(this)[0].getBoundingClientRect();

$(".sub-menu").css({
opacity: 1,
left: mnItemMeta.left
})
}

function hoverOut() {
$(".sub-menu").css({
opacity: 0
})
}
html, body {
background-color: #efefef;
}
.menu {
list-style: none;
padding-left: 0;
display: flex;
justify-content: center;
}
a {
display: block;
padding: 10px 20px;
text-decoration: none;
color: inherit;
}
.sub-menu {
opacity: 0;
background-color: white;
position: absolute;
transition: .2s ease;
}
.sub-menu-list {
list-style: none;
padding-left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul class="menu">
<li class="menu-item"><a href="#">Menu Item</a>
<div class="sub-menu">
<ul class="sub-menu-list">
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
<li><a href="#">Sub Menu 4</a></li>
</ul>
</div>
</li>
</ul>

另一种方法是检查.menu-item.sub-menuhover 状态。您需要在此处设置一点超时,以防止它提前关闭。

var timeout,
hovered = false,
menuItem = $(".menu-item, .sub-menu").hover(hoverIn, hoverOut);;

function hoverIn() {
hovered = true;

var mnItemMeta = this.getBoundingClientRect();

$(".sub-menu").show().css({
opacity: 1,
left: mnItemMeta.left,
});
}

function hoverOut() {
hovered = false;

clearTimeout(timeout);
timeout = setTimeout(function() {
if (!hovered) {
$(".sub-menu").css({
opacity: 0,
}).hide()
}
}, 100);
}
html, body {
background-color: #efefef;
}
.menu {
list-style: none;
padding-left: 0;
display: flex;
justify-content: center;
}
a {
display: block;
padding: 10px 20px;
text-decoration: none;
color: inherit;
}
.sub-menu {
opacity: 0;
background-color: white;
position: absolute;
transition: .2s ease;
}
.sub-menu-list {
list-style: none;
padding-left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul class="menu">
<li class="menu-item"><a href="#">Menu Item</a></li>
</ul>
<div class="sub-menu">
<ul class="sub-menu-list">
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
<li><a href="#">Sub Menu 4</a></li>
</ul>
</div>

关于javascript - 将悬停区域扩展到外部元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39612178/

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