Bot*_*jna 0 css jquery menu delay hover
我有一个 2 级下拉菜单,鼠标必须穿过相当狭窄的走廊才能保持菜单打开,如果它偏离轨道,菜单会意外关闭,这令人沮丧。我想让菜单不立即关闭,而是延迟关闭。
这是一个用 css 制作的标准菜单,如下所示:
ul.menu li ul {
display: none;
}
ul.menu li:hover ul {
display:block;
}
Run Code Online (Sandbox Code Playgroud)
我需要当不再有悬停状态时,菜单仍然可见至少 0.5 秒。
已经尝试过这个,但它不起作用:
<script>
$( ".menu li" ).mouseout(function() {
$(".menu li ul").css("display: block");
$(".menu li ul").css.setTimeout("display: none", 2000);
});
</script>
Run Code Online (Sandbox Code Playgroud)
您可以使用带有延迟的过渡,以使其在悬停时保持可见:
.nested {
pointer-events:none; /* this is so it behaves like display none and mouse does not interact with child when hidden */
opacity: 0;
transition: opacity 0.1s; /* change length to longer for a nicer fade */
transition-delay: 1s; /* fadeout won't happen for a second */
}
.hover:hover .nested {
pointer-events: auto;
opacity: 1;
transition: opacity 0.1s; /* fade in when hovered */
transition-delay: 0s; /* fade in immediately */
}Run Code Online (Sandbox Code Playgroud)
<div class="hover">
hover
<div class="nested">
nested
</div>
</div>Run Code Online (Sandbox Code Playgroud)
在上面,您ul将是.nested,您的父母li将是.hover