gpt4 book ai didi

html - 失去鼠标焦点时如何防止菜单关闭?

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

我使用在网上找到的示例构建了一个不错的下拉菜单,但我的经理遇到了菜单有时难以使用的问题,因为如果鼠标从菜单上掉下来,它就会关闭。我已经阅读了一些关于如何绕过它的示例 here ,但我的问题是我无法使用这些解决方案来处理我的代码。

有人可以给我一些指示吗?下面包含我的 CSS 代码和一个 html 示例:

#cssmenu ul,
#cssmenu li,
#cssmenu span,
#cssmenu a {
padding: 0;
position: relative;
margin-left:auto;
margin-right:auto;
text-align:center;
font-family: "charles modern"
}
#cssmenu {
line-height: 1;
background: #ffffff;
margin-left:auto;
margin-right:auto;
text-align:center;
}
#cssmenu .padding{
display: block;
position: absolute;
z-index: 78;

width: 100%;
height: 100%;

top: 0px;
left: 0px;
background: none !important;
}
/* Padding amount for first-level dropdown */
#cssmenu > li > .submenu > .padding{
width: 400%;
left: -30%;
height: 160%;
}

#cssmenu .buffer{
display: block;
position: absolute;
bottom: 0px;
right: 100%;

width: 100%;
height: 100%;
background: none !important;
}

#cssmenu .shadow{
display: none !important;
}
#cssmenu:after,
#cssmenu ul:after {
content: '';
display: block;
clear: both;
}
#cssmenu a {
background: #ffffff;
color: #00A0DF;
display: block;
font-family: "charles modern", "charles modern light", Calibri, sans-serif;
font-weight: bold;
padding: 19px 20px;
text-decoration: none;
margin-left:auto;
margin-right:auto;
text-align:center;
z-index:99999;

}
#cssmenu ul {
list-style: none;
}
#cssmenu > ul > li {
display: inline-block;
float: center;
margin:0 auto;
}
#cssmenu.align-center {
text-align: center;
}
#cssmenu.align-center > ul > li {
float: none;
}
#cssmenu.align-center ul ul {
text-align: left;
}
#cssmenu.align-right > ul {
float: right;
}
#cssmenu.align-right ul ul {
text-align: right;
}
#cssmenu > ul > li > a {
color: #ffffff;
font-size: 12px;
}
#cssmenu > ul > li:hover:after {
content: '';
display: block;
width: 0;
height: 0;
position: absolute;
left: 50%;
bottom: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #0fa1e0;
margin-left: -10px;
}
#cssmenu > ul > li:first-child > a {
border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
}
#cssmenu.align-right > ul > li:first-child > a,
#cssmenu.align-center > ul > li:first-child > a {
border-radius: 0;
-moz-border-radius: 0;
-webkit-border-radius: 0;
}
#cssmenu.align-right > ul > li:last-child > a {
border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
}
#cssmenu > ul > li.active > a {
color: #047aea;
text-align: center;
padding: 14px 14px;
font-size:150%;
font-style: bold;
background: #ffffff;
}

#cssmenu > ul > li:hover > a {
background-color: #00A0DF;
color: white;
}

#cssmenu .has-sub {
z-index: 1;
}
#cssmenu .has-sub:hover > ul {
display: block;
}
#cssmenu .has-sub ul {
display: none;
position: absolute;
width: 250px;
top: 100%;
left: 0;
font-size: 100%;
}
#cssmenu.align-right .has-sub ul {
left: auto;
right: 0;
}
#cssmenu .has-sub ul li {
*margin-bottom: -1px;
}
#cssmenu .has-sub ul li a {
background: #f2f2f2;
display: block;
line-height: 120%;
padding: 6px 6px;
color: black;
font-syle: bold;
font-size: 105%;
}
#cssmenu .has-sub ul li {
background: #f2f2f2;
}
#cssmenu .has-sub ul li:hover a {
background: #00A0DF;
}
#cssmenu ul ul li:hover > a {
color: #ffffff;
}
#cssmenu .has-sub .has-sub:hover > ul {
display: block;
}
#cssmenu .has-sub .has-sub ul {
display: none;
position: absolute;
left: 100%;
top: 0;
height: 250px;
overflow: auto;
}
#cssmenu.align-right .has-sub .has-sub ul,
#cssmenu.align-right ul ul ul {
left: auto;
right: 100%;
}
#cssmenu .has-sub .has-sub ul li a {
background: #f2f2f2;
}

#cssmenu .has-sub .has-sub ul li {
background: #f2f2f2;
}

#cssmenu .has-sub .has-sub ul li a:hover {
background: #00A0DF;
}
#cssmenu ul ul li.last > a,
#cssmenu ul ul li:last-child > a,
#cssmenu ul ul ul li.last > a,
#cssmenu ul ul ul li:last-child > a,
#cssmenu .has-sub ul li:last-child > a,
#cssmenu .has-sub ul li.last > a {
border-bottom: 0;
}
<div id='cssmenu'>
<ul>
<li class='active has-sub'><center>Image 1</center></a>
<ul>
<li class='has-sub'><a href='#'><span>Add New Item</span></a></li>
<li class='has-sub'><a href='#'><span>View Item</span></a></li>
<li class='has-sub last'><a href='#'><span>Item With Sub-Items:</span></a>
<ul>
<li><a href='#'><span>User 1</span></a></li>
<li><a href='#'><span>User 2</span></a></li>
</ul>
</li>
</ul>
<li class='active has-sub'><center>&nbsp;&nbsp;&nbsp;&nbsp;Image 2</center></a>
<ul>
<li class='has-sub'><a href='#'><span>Add New Item</span></a></li>
<li class='has-sub'><a href='#'><span>View Item</span></a></li>
</ul>
</li>
</ul>
</div>

最佳答案

编辑:现在使用涉及边框和容器 div 的不同(更简单)方法。只需将 .level-2-menu, .level-3-menuborder 属性的颜色从 red 更改为 transparent 在你的实际代码中。唯一的问题是 3 级菜单挡住了前一个 2 级元素的右下角。

new jsFiddle


听起来您需要的是每个菜单周围的一些区域,您可以在其中关闭可见菜单但仍保持菜单打开。下面是一些代码来演示这个概念。您应该能够将这个概念应用到您的代码中,并按照您喜欢的方式设计它。

jsFiddle

background-color: red 只是为了使额外的悬停区域可见以用于演示目的。

HTML:

<div class="cssmenu">
<ul class="level-1-menu">
<li class="level-1-item">
<a href="#">Level 1 Item 1</a>
<ul class="level-2-menu">
<li class="level-2-item"><a href="#">Level 2 Item 1</a></li>
<li class="level-2-item"><a href="#">Level 2 Item 2</a></li>
<li class="level-2-item">
<a href="#">Level 2 Item 3 with Sub-Items</a>
<ul class="level-3-menu">
<li class="level-3-item"><a href="#">Level 3 Item 1</a></li>
<li class="level-3-item"><a href="#">Level 3 Item 2 longer</a></li>
</ul>
</li>
</ul>
</li>
<li class="level-1-item">
<a href="#">Level 1 Item 2</a>
<ul class="level-2-menu">
<li class="level-2-item"><a href="#">Level 2 Item 1</a></li>
<li class="level-2-item"><a href="#">Level 2 Item 2 longer</a></li>
</ul>
</li>
</ul>
</div>

CSS:

.cssmenu {
background-color: white;
}
.cssmenu ul {
background-color: white;
list-style: none;
padding: 0;
}

.cssmenu li {
padding: 20px 0;
position: relative;
}

.cssmenu li:hover {
background-color: lightblue;
}

.cssmenu li a {
padding: 20px;
}

.level-1-menu {
text-align: center;
}

.level-1-item {
display: inline-block;
text-align: left;
}

.level-2-menu,
.level-3-menu {
display: none;
position: absolute;
white-space: nowrap;
}

.level-1-item:hover .level-2-menu,
.level-2-item:hover .level-3-menu {
display: block;
}

.level-2-menu {
left: 0;
top: 100%;
}

.level-3-menu {
left: 100%;
top: 0;
}

.level-2-item {
position: relative;
}

.level-2-item::after,
.level-3-item::after {
background-color: red;
bottom: 0;
content: '';
left: -30px;
position: absolute;
right: -30px;
top: 0;
z-index: -1;
}

.level-2-item:first-child::after,
.level-3-item:first-child::after {
top: -30px;
}

.level-2-item:last-child::after,
.level-3-item:last-child::after {
bottom: -30px;
}

关于html - 失去鼠标焦点时如何防止菜单关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38133285/

25 4 0