为下拉菜单添加带搜索栏的滚动条

huangapple go评论73阅读模式
英文:

Add a scroll bar for dropdown menu with search bar

问题

这是我的HTML代码:

<div class="location-dropdown dropdown">
    <button onclick="locations_dropdown()" class="dropbtn" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">{{site_network_status['sitename']}}</button>
    <div id="myDropdown" class="dropdown-content">
        <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
        <div>
            <a class="dropdown-item" href="link1">link1</a>
            <a class="dropdown-item" href="link2">link2</a>
        </div>
    </div>
</div>

这是用于显示所有选项并允许用户搜索的JavaScript代码:

function locations_dropdown() {
    document.getElementById("myDropdown").classList.toggle("show");
}

function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myDropdown");
    a = div.getElementsByTagName("a");
    for (i = 0; i < a.length; i++) {
        txtValue = a[i].textContent || a[i].innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
        }
    }
}

这段代码可以正常工作。但是在下拉菜单中有太多选项链接。所以我想要一个选项,可以滚动查看这些选项。我只想显示大约10个选项,然后从中滚动。

任何帮助将不胜感激。

更新

这是我正在使用的CSS文件:

.dropbtn {
    background-color: #04AA6D;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

#myInput {
    box-sizing: border-box;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
    border-bottom: 1px solid #ddd;
}

#myInput:focus {outline: 3px solid #ddd;}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    border: 1px solid #ddd;
    z-index: 1;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
英文:

This is my HTML code

                &lt;div class=&quot;location-dropdown dropdown&quot;&gt;
                    &lt;button onclick=&quot;locations_dropdown()&quot; class=&quot;dropbtn&quot; id=&quot;dropdownMenuLink&quot; data-bs-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;{{site_network_status[&#39;sitename&#39;]}}&lt;/button&gt;
                    &lt;div id=&quot;myDropdown&quot; class=&quot;dropdown-content&quot;&gt;
                        &lt;input type=&quot;text&quot; placeholder=&quot;Search..&quot; id=&quot;myInput&quot; onkeyup=&quot;filterFunction()&quot;&gt;
                        &lt;div&gt;
                            &lt;a class=&quot;dropdown-item&quot; href=&quot;link1&quot;&gt;link1&lt;/a&gt;
                            &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link2&lt;/a&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;

And this is my js code used to show all the options and let user search.

function locations_dropdown() {
    document.getElementById(&quot;myDropdown&quot;).classList.toggle(&quot;show&quot;);
  }
  
  function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById(&quot;myInput&quot;);
    filter = input.value.toUpperCase();
    div = document.getElementById(&quot;myDropdown&quot;);
    a = div.getElementsByTagName(&quot;a&quot;);
    for (i = 0; i &lt; a.length; i++) {
      txtValue = a[i].textContent || a[i].innerText;
      if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
        a[i].style.display = &quot;&quot;;
      } else {
        a[i].style.display = &quot;none&quot;;
      }
    }
  }

this works fine. But I have way too many option links in the dropdown menu. So i want an option to be able to scroll through those options. i only want to be able to show around 10 options and scroll from that down.

Any help will be appreciated.

Update

this is the css file i am using.

.dropbtn {
    background-color: #04AA6D;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
  }
  
  .dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
  }
  
  #myInput {
    box-sizing: border-box;
    background-image: url(&#39;searchicon.png&#39;);
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
    border-bottom: 1px solid #ddd;
  }
  
  #myInput:focus {outline: 3px solid #ddd;}
  
  .dropdown {
    position: relative;
    display: inline-block;
  }
  
  .dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    border: 1px solid #ddd;
    z-index: 1;
  }
  
  .dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
  }
  
  .dropdown a:hover {background-color: #ddd;}
  
  .show {display: block;}

答案1

得分: 2

添加可滚动容器类并进行样式设置。

.scrolable-container {
  overflow: auto;
  max-height: 420px;
}

在HTML代码中,将这个类应用于需要可滚动容器的元素。

英文:

add scrolable-container class and style it.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-css -->

h1,
h2 {
  font-family: Lato;
}

.dropbtn {
  background-color: #04aa6d;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover,
.dropbtn:focus {
  background-color: #3e8e41;
}

#myInput {
  box-sizing: border-box;
  background-image: url(&#39;searchicon.png&#39;);
  background-position: 14px 12px;
  background-repeat: no-repeat;
  font-size: 16px;
  padding: 14px 20px 12px 45px;
  border: none;
  border-bottom: 1px solid #ddd;
}

#myInput:focus {
  outline: 3px solid #ddd;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  min-width: 230px;
  overflow: auto;
  border: 1px solid #ddd;
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {
  background-color: #ddd;
}

.show {
  display: block;
}

.scrolable-container {
  overflow: auto;
  max-height: 420px;
}

<!-- language: lang-html -->

&lt;body&gt;
&lt;div class=&quot;location-dropdown dropdown&quot;&gt;
&lt;button onclick=&quot;locations_dropdown()&quot; class=&quot;dropbtn&quot; id=&quot;dropdownMenuLink&quot; data-bs-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;{{site_network_status[&#39;sitename&#39;]}}&lt;/button&gt;
&lt;div id=&quot;myDropdown&quot; class=&quot;dropdown-content&quot;&gt;
    &lt;input type=&quot;text&quot; placeholder=&quot;Search..&quot; id=&quot;myInput&quot; onkeyup=&quot;filterFunction()&quot;&gt;
    &lt;div class=&quot;scrolable-container&quot;&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link1&quot;&gt;link1&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link2&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link3&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link4&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link5&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link6&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link7&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link8&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link9&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link10&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link11&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link12&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link13&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link14&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link15&lt;/a&gt;
        &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link16&lt;/a&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
  function locations_dropdown() {
    document.getElementById(&quot;myDropdown&quot;).classList.toggle(&quot;show&quot;);
  }
  
  function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById(&quot;myInput&quot;);
    filter = input.value.toUpperCase();
    div = document.getElementById(&quot;myDropdown&quot;);
    a = div.getElementsByTagName(&quot;a&quot;);
    for (i = 0; i &lt; a.length; i++) {
      txtValue = a[i].textContent || a[i].innerText;
      if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
        a[i].style.display = &quot;&quot;;
      } else {
        a[i].style.display = &quot;none&quot;;
      }
    }
  }
&lt;/script&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 15:09:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定