gpt4 book ai didi

javascript - 如何限制列表父 div 的高度并使内容可滚动?

转载 作者:行者123 更新时间:2023-12-01 03:30:35 24 4
gpt4 key购买 nike

我成功地更改了背景中的 div 以适应所有元素:

       newHeight = document.getElementById("searchResultsList").offsetHeight+115;//document.getElementById(newHeight).offsetHeight+newHeight;//document.getElementsByClassName("searchUserDiv").offsetHeight
document.getElementById('searchDiv').style.height = ''+newHeight+'px';

但是问题是,在某些时候我需要停止向 div 添加高度,而是使 div 可滚动。我找到了导航元素,但还没有找到一种方法来设置最大高度(此时列表可滚动),当动态添加元素时它可以达到。 我怎样才能实现这一目标?

<小时/>

//In for each loop I do the following

$('.searchResultsList').append('<li class="searchUserDiv" id="'+newHeight+'"><img class="searchUserProfImg" id="searchUserProfImg" src="'+profileImg+'"><label class="searchUsername" id="searchUsername">'+username+'</label></li>');
newHeight = document.getElementById("searchResultsList").offsetHeight+115;//document.getElementById(newHeight).offsetHeight+newHeight;//document.getElementsByClassName("searchUserDiv").offsetHeight
document.getElementById('searchDiv').style.height = ''+newHeight+'px';
.searchBar {
width: 500px;
height: 55px;
background-color: #3C3C3C;
border-radius: 50px;
margin-top: 12px;

box-shadow: 0 1px 2px rgba(0, 0, 0, 0);
transition: box-shadow 0.05s ease-in-out;
}

.searchBar:hover {
box-shadow: 0 0px 10px rgba(0, 0, 0, 0.5);
}

.searchBtn {
margin-top: 1px;
display: inline-block;
width: 12%;
height: 100%;
background-color: transparent;
border-radius: 5px;
border: none;
cursor: pointer;
/* background-image:url('../images/searchBtnIcon.png');
background-size: 85%;
background-repeat:no-repeat;
vertical-align: middle; */
}

.searchBtn img {
vertical-align: bottom;
width: 96%;
/* height: 100%; */
}

/* .searchBar button:hover {
background-color: white;
} */

.searchDiv {
vertical-align: top;
height: 100%;
margin-left: 50px;
display: inline-block;
background-color: #292929;
border-bottom-left-radius: 30px;
border-bottom-right-radius: 30px;
}

.searchDiv input {
display: inline-block;
padding-bottom: 5px;
padding-left: 30px;
/* width: 500px;
height: 50px; */
width: 78%;
height: 95%;
border: none;
/* background-color: #3C3C3C; */
background-color: transparent;
color: white;
font: Arial;
font-family: sans-serif;
font-size: 18px;
font-weight:bold;
outline: none;
/* border-radius: 50px; */
vertical-align: top;
}

.searchDiv:hover ::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
color: #838383;
opacity: 1; /* Firefox */
}
.searchDiv:hover {
/* height: 600px; */
/* background-color: #292929; */
/* #3C3C3C; */
box-shadow: 0 0px 10px rgba(41, 41, 41, 0.8);
}

.searchResultsList {
/* background-color: grey; */
padding-left: 0px;
}

.searchResultsList li {
list-style: none;

transition: 0.06s background-color;
}
.searchResultsList li:hover {
background-color: rgba(255, 255, 255, 0.05);
}
.searchResultsList li:active {
background-color: rgba(255, 255, 255, 0.1);
}
.searchResultsList * {
/* background-color: grey; */
/* margin: 13px 0 13px 0; */
margin-top: 6px;
margin-bottom: 6px;
/* margin-left: 0px; */
cursor: pointer;
}

.searchUserDiv {
display: flex;
align-items: center;
/* background-color: #353258;
border: 1px solid #4152F1; */
color: white;
cursor: pointer;
padding-left: 10px;
width: 98%;
/* border-radius: 32px; */
}
.searchUsername {
display: flex;
align-items: center;
font-size: 16px;
font-weight: bold;
font: Arial;
font-family: sans-serif;
margin-top: 7px;
padding-left: 13px;
}
.searchUserProfImg {
border-radius: 25px;
border: 1px solid #505050;
margin-left: 5px;
height: 47px;
width:47px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="searchDiv" id="searchDiv">
<div class="searchBar">
<input type="text" placeholder="Search" name="search" id="search" required>
<button class="searchBtn" id="searchBtn"> <img src="images/searchBtnIcon.png"></button>
</div>
<nav>
<ul class="searchResultsList" id="searchResultsList" style="display: none;">
<li class="searchUserDiv">
<img class="searchUserProfImg" id="searchUserProfImg" src="images/defaultProfImg.png">
<label class="searchUsername" id="searchUsername">Noha harrarri</label>
</li>
<li class="searchUserDiv">
<img class="searchUserProfImg" id="searchUserProfImg" src="images/defaultProfImg.png">
<label class="searchUsername" id="searchUsername">Nohasomething</label>
</li>
<li class="searchUserDiv">
<img class="searchUserProfImg" id="searchUserProfImg" src="images/defaultProfImg.png">
<label class="searchUsername" id="searchUsername">Noha_2</label>
</li>
</ul>
</nav>
</div>

最佳答案

将高度设置为固定数字,例如 200px,然后设置 div 的溢出滚动。

关于javascript - 如何限制列表父 div 的高度并使内容可滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60246107/

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