防止颤动下拉菜单隐藏其父对话框的方法是通过以下步骤:
以下是一个示例代码:
HTML代码:
<div class="dialog">
<button class="dropdown-btn">下拉菜单</button>
<ul class="dropdown-menu">
<li>菜单项1</li>
<li>菜单项2</li>
<li>菜单项3</li>
</ul>
</div>
CSS代码:
.dialog {
position: relative;
}
.dropdown-menu {
position: fixed;
z-index: 999;
display: none;
transition: opacity 0.3s ease-in-out;
}
.dropdown-menu.show {
display: block;
}
.dropdown-btn:hover + .dropdown-menu,
.dropdown-menu:hover {
display: block;
}
.dropdown-menu li {
cursor: pointer;
}
.dropdown-menu li:hover {
background-color: #f1f1f1;
}
JavaScript代码:
const dropdownBtn = document.querySelector('.dropdown-btn');
const dropdownMenu = document.querySelector('.dropdown-menu');
dropdownBtn.addEventListener('click', function(event) {
event.stopPropagation();
dropdownMenu.classList.toggle('show');
});
document.addEventListener('click', function() {
dropdownMenu.classList.remove('show');
});
这样,当鼠标移入下拉菜单或者点击下拉菜单时,下拉菜单会显示,并且不会隐藏父对话框。当鼠标移出下拉菜单时,下拉菜单会隐藏。
领取专属 10元无门槛券
手把手带您无忧上云