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

标签 javascript jquery html css

我成功地更改了背景中的 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 可滚动。我找到了 nav 元素,但还没有找到一种方法来设置最大高度(此时列表可滚动),当动态添加元素时它可以达到。 我怎样才能实现这一目标?


//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/

相关文章:

javascript - Node.js v.11.6.0 : How to resolve circular dependency?

javascript - 输入搜索字段在响应式网站的移动版本上无法正常工作

通过 Python 执行 Javascript

html - 为什么我无法将 Text-decoration-style 从 Underline 移除为 None?

Javascript:访问父元素时未定义

javascript - 如何将最长元素的宽度分配给其他元素

javascript - 从客户端传递选择查询

javascript - 将附加 Prop 传递给 Div - React

javascript - 使用 Knockout.js 从 Json 响应创建表

php - 如果值为 true,则为每个项目添加类