gpt4 book ai didi

android - 如何在移动设备中滚动固定菜单的子菜单

转载 作者:太空宇宙 更新时间:2023-11-04 10:34:11 30 4
gpt4 key购买 nike

我为移动设备制作了一个粘性菜单。一切都很好,但是有一个问题是子菜单溢出视口(viewport),在滚动时,只有文档主体滚动,菜单的隐藏部分一直在视口(viewport)之外。我为容器使用了 overflow:scroll,但我无法修复它。

section {
height:200px;
width:100%;
margin:0;
padding:0;
}
.p1 {background:#656161}
.p2 {background:#87E857}
.p3 {background:#E28EBC}
.p4 {background:#DBDE46}
.p5 {background:#57BCEF}
.p6 {background:#ff0000}
ul {
list-style:none;
margin:0;
padding:0;
}
nav {
position:fixed;
top:0;
left:0;
background:#73CADB;
width:100%;
height:38px;
}

nav ul > li {
float:left;
width:150px;
position:relative;
padding:10px;
background: #73CADB;
border-right:1px solid #fff;
-webkit-transition:all .3s ease;
transition:all .3s ease;
}
nav ul > li i{
position:absolute;
right:20px;
}
nav ul > li:hover,
nav ul > li.hovered{
background: #E6A735;
}

nav ul > li a{
text-decoration:none;
color:#fff;
}
nav ul > li > ul{
position:absolute;
width:70%;
top:38px;
left:0px;
}
nav ul > li > ul li{
display:block;
}
<body>
<nav>
<ul>
<li><a href="#">link</a></li>
<li class="hovered"><a href="#">link <i class="fa fa-caret-down"></i></a>
<ul>
<li><a href="#">sublink1</a></li>
<li><a href="#">sublink2</a></li>
<li><a href="#">sublink3</a></li>
<li><a href="#">sublink4</a></li>
<li><a href="#">sublink5</a></li>
<li><a href="#">sublink6</a></li>
<li><a href="#">sublink7</a></li>
<li><a href="#">sublink8</a></li>
<li><a href="#">sublink9</a></li>
<li><a href="#">sublink10</a></li>
<li><a href="#">sublink11</a></li>
<li><a href="#">sublink12</a></li>
<li><a href="#">sublink13</a></li>
<li><a href="#">sublink14</a></li>
<li><a href="#">sublink15</a></li>
<li><a href="#">sublink16</a></li>
<li><a href="#">sublink17</a></li>
<li><a href="#">sublink18</a></li>
<li><a href="#">sublink19</a></li>
<li><a href="#">sublink20</a></li>
<li><a href="#">sublink21</a></li>
<li><a href="#">sublink22</a></li>
<li><a href="#">sublink23</a></li>
<li><a href="#">sublink24</a></li>
<li><a href="#">sublink25</a></li>
<li><a href="#">sublink26</a></li>
<li><a href="#">sublink27</a></li>
<li><a href="#">sublink28</a></li>
<li><a href="#">sublink29</a></li>
<li><a href="#">sublink30</a></li>
<li><a href="#">sublink31</a></li>
<li><a href="#">sublink32</a></li>
<li><a href="#">sublink33</a></li>
</ul>
</li>
</ul>
</nav>
<section class="p1"></section>
<section class="p2"></section>
<section class="p3"></section>
<section class="p4"></section>
<section class="p5"></section>
<section class="p6"></section>
</body>

这是 fiddle :

https://jsfiddle.net/3ewatm9d/3/

最佳答案

向元素添加一些 max-height 以便它可以达到溢出限制。

nav ul > li > ul{
position:absolute;
width:70%;
top:38px;
left:0px;
max-height: 200px;
overflow:auto;
width:auto;
}

section {
height:200px;
width:100%;
margin:0;
padding:0;
}
.p1 {background:#656161}
.p2 {background:#87E857}
.p3 {background:#E28EBC}
.p4 {background:#DBDE46}
.p5 {background:#57BCEF}
.p6 {background:#ff0000}
ul {
list-style:none;
margin:0;
padding:0;
}
nav {
position:fixed;
top:0;
left:0;
background:#73CADB;
width:100%;
height:38px;
}

nav ul > li {
float:left;
width:150px;
position:relative;
padding:10px;
background: #73CADB;
border-right:1px solid #fff;
-webkit-transition:all .3s ease;
transition:all .3s ease;
}
nav ul > li i{
position:absolute;
right:20px;
}
nav ul > li:hover,
nav ul > li.hovered{
background: #E6A735;
}

nav ul > li a{
text-decoration:none;
color:#fff;
}
nav ul > li > ul{
position:absolute;
width:70%;
top:38px;
left:0px;
max-height: 150px;
overflow:auto;
width:auto;
}
nav ul > li > ul li{
display:block;
}
<body>
<nav>
<ul>
<li><a href="#">link</a></li>
<li class="hovered"><a href="#">link <i class="fa fa-caret-down"></i></a>
<ul>
<li><a href="#">sublink1</a></li>
<li><a href="#">sublink2</a></li>
<li><a href="#">sublink3</a></li>
<li><a href="#">sublink4</a></li>
<li><a href="#">sublink5</a></li>
<li><a href="#">sublink6</a></li>
<li><a href="#">sublink7</a></li>
<li><a href="#">sublink8</a></li>
<li><a href="#">sublink9</a></li>
<li><a href="#">sublink10</a></li>
<li><a href="#">sublink11</a></li>
<li><a href="#">sublink12</a></li>
<li><a href="#">sublink13</a></li>
<li><a href="#">sublink14</a></li>
<li><a href="#">sublink15</a></li>
<li><a href="#">sublink16</a></li>
<li><a href="#">sublink17</a></li>
<li><a href="#">sublink18</a></li>
<li><a href="#">sublink19</a></li>
<li><a href="#">sublink20</a></li>
<li><a href="#">sublink21</a></li>
<li><a href="#">sublink22</a></li>
<li><a href="#">sublink23</a></li>
<li><a href="#">sublink24</a></li>
<li><a href="#">sublink25</a></li>
<li><a href="#">sublink26</a></li>
<li><a href="#">sublink27</a></li>
<li><a href="#">sublink28</a></li>
<li><a href="#">sublink29</a></li>
<li><a href="#">sublink30</a></li>
<li><a href="#">sublink31</a></li>
<li><a href="#">sublink32</a></li>
<li><a href="#">sublink33</a></li>
</ul>
</li>
</ul>
</nav>
<section class="p1"></section>
<section class="p2"></section>
<section class="p3"></section>
<section class="p4"></section>
<section class="p5"></section>
<section class="p6"></section>
</body>

关于android - 如何在移动设备中滚动固定菜单的子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36281508/

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