gpt4 book ai didi

php - 如何使现有菜单可滚动

转载 作者:行者123 更新时间:2023-11-28 10:08:23 24 4
gpt4 key购买 nike

如何使菜单中的子菜单可滚动,这样它们就不会离开屏幕

我的html

<nav>
<ul>
<li><a href="#">parent</a></li>
<li><a href="#">parent</a></li>
<li>
<a href="#">parent width child</a>
<ul>
<li><a href="#">child</a></li>
<li><a href="#">child</a></li>
<li>
<a href="#">child with children</a>
<ul>
<li><a href="#">grand child</a></li>
<li><a href="#">grand child</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">parent</a></li>
</ul>
</nav>

我的CSS

/* reset padding and margin where necessary etc. */
ul {
list-style-type: none;
margin: 0;
padding: 0;
}

a {
text-decoration: none;
}

/* just some quick demo styles for color whatnot */
nav {
background: black;
color: white;
}

nav ul ul {
background: #555;
}

nav ul ul ul {
background: #999;
}

nav a {
color: white;
white-space:nowrap;
}

nav a:hover {
background: #f80;
}

/* important functional styles */
nav > ul:after {
/* clear the float */
content:'';
clear:both;
display: block;
}

nav li {
/* for the topmost level we want them to float. will be overridden */
float:left;
}

nav li a {
/* always apply padding and display block to the a. better user experience. */
display:block;
padding: 10px;
}

nav li ul li {
/* overridden floating here */
float: none;
}

/* here is where all the positioning takes place */
nav li {
position:relative;
}

nav li ul {
position:absolute;
left: 0; /* for top most level lets align to the left */
top: 100%; /* and have it at the bottom of the parent */
display:none; /* hide initialy */
}

nav li ul li ul {
left: 100%; /* for grandchild level lets align to the right of the list item */
top: 0; /* and have it at the top of the parent li */
}

nav ul li a:hover + ul,
nav ul li a + ul:hover {
/* show only if the element or the immediately preceding anchor is hovered*/
display:block;
}

/* enjoy! */

请参阅 jsfiddle

当有多个子菜单项或类似 50 个这样的元素时,如何使其可滚动?

最佳答案

使用 overflow-y:scroll;并为第二个 <ul> 添加高度:

nav li ul li ul {
left: 100%; /* for grandchild level lets align to the right of the list item */
top: 0; /* and have it at the top of the parent li */
height:200px;
overflow-y:scroll;
}

JSFiddle Example

关于php - 如何使现有菜单可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467968/

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