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

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

Add a scroll bar for dropdown menu with search bar

问题

这是我的HTML代码:

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

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

  1. function locations_dropdown() {
  2. document.getElementById("myDropdown").classList.toggle("show");
  3. }
  4. function filterFunction() {
  5. var input, filter, ul, li, a, i;
  6. input = document.getElementById("myInput");
  7. filter = input.value.toUpperCase();
  8. div = document.getElementById("myDropdown");
  9. a = div.getElementsByTagName("a");
  10. for (i = 0; i < a.length; i++) {
  11. txtValue = a[i].textContent || a[i].innerText;
  12. if (txtValue.toUpperCase().indexOf(filter) > -1) {
  13. a[i].style.display = "";
  14. } else {
  15. a[i].style.display = "none";
  16. }
  17. }
  18. }

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

任何帮助将不胜感激。

更新

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

  1. .dropbtn {
  2. background-color: #04AA6D;
  3. color: white;
  4. padding: 16px;
  5. font-size: 16px;
  6. border: none;
  7. cursor: pointer;
  8. }
  9. .dropbtn:hover, .dropbtn:focus {
  10. background-color: #3e8e41;
  11. }
  12. #myInput {
  13. box-sizing: border-box;
  14. background-image: url('searchicon.png');
  15. background-position: 14px 12px;
  16. background-repeat: no-repeat;
  17. font-size: 16px;
  18. padding: 14px 20px 12px 45px;
  19. border: none;
  20. border-bottom: 1px solid #ddd;
  21. }
  22. #myInput:focus {outline: 3px solid #ddd;}
  23. .dropdown {
  24. position: relative;
  25. display: inline-block;
  26. }
  27. .dropdown-content {
  28. display: none;
  29. position: absolute;
  30. background-color: #f6f6f6;
  31. min-width: 230px;
  32. overflow: auto;
  33. border: 1px solid #ddd;
  34. z-index: 1;
  35. }
  36. .dropdown-content a {
  37. color: black;
  38. padding: 12px 16px;
  39. text-decoration: none;
  40. display: block;
  41. }
  42. .dropdown a:hover {background-color: #ddd;}
  43. .show {display: block;}
英文:

This is my HTML code

  1. &lt;div class=&quot;location-dropdown dropdown&quot;&gt;
  2. &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;
  3. &lt;div id=&quot;myDropdown&quot; class=&quot;dropdown-content&quot;&gt;
  4. &lt;input type=&quot;text&quot; placeholder=&quot;Search..&quot; id=&quot;myInput&quot; onkeyup=&quot;filterFunction()&quot;&gt;
  5. &lt;div&gt;
  6. &lt;a class=&quot;dropdown-item&quot; href=&quot;link1&quot;&gt;link1&lt;/a&gt;
  7. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link2&lt;/a&gt;
  8. &lt;/div&gt;
  9. &lt;/div&gt;
  10. &lt;/div&gt;

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

  1. function locations_dropdown() {
  2. document.getElementById(&quot;myDropdown&quot;).classList.toggle(&quot;show&quot;);
  3. }
  4. function filterFunction() {
  5. var input, filter, ul, li, a, i;
  6. input = document.getElementById(&quot;myInput&quot;);
  7. filter = input.value.toUpperCase();
  8. div = document.getElementById(&quot;myDropdown&quot;);
  9. a = div.getElementsByTagName(&quot;a&quot;);
  10. for (i = 0; i &lt; a.length; i++) {
  11. txtValue = a[i].textContent || a[i].innerText;
  12. if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
  13. a[i].style.display = &quot;&quot;;
  14. } else {
  15. a[i].style.display = &quot;none&quot;;
  16. }
  17. }
  18. }

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.

  1. .dropbtn {
  2. background-color: #04AA6D;
  3. color: white;
  4. padding: 16px;
  5. font-size: 16px;
  6. border: none;
  7. cursor: pointer;
  8. }
  9. .dropbtn:hover, .dropbtn:focus {
  10. background-color: #3e8e41;
  11. }
  12. #myInput {
  13. box-sizing: border-box;
  14. background-image: url(&#39;searchicon.png&#39;);
  15. background-position: 14px 12px;
  16. background-repeat: no-repeat;
  17. font-size: 16px;
  18. padding: 14px 20px 12px 45px;
  19. border: none;
  20. border-bottom: 1px solid #ddd;
  21. }
  22. #myInput:focus {outline: 3px solid #ddd;}
  23. .dropdown {
  24. position: relative;
  25. display: inline-block;
  26. }
  27. .dropdown-content {
  28. display: none;
  29. position: absolute;
  30. background-color: #f6f6f6;
  31. min-width: 230px;
  32. overflow: auto;
  33. border: 1px solid #ddd;
  34. z-index: 1;
  35. }
  36. .dropdown-content a {
  37. color: black;
  38. padding: 12px 16px;
  39. text-decoration: none;
  40. display: block;
  41. }
  42. .dropdown a:hover {background-color: #ddd;}
  43. .show {display: block;}

答案1

得分: 2

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

  1. .scrolable-container {
  2. overflow: auto;
  3. max-height: 420px;
  4. }

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

英文:

add scrolable-container class and style it.

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

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

  1. h1,
  2. h2 {
  3. font-family: Lato;
  4. }
  5. .dropbtn {
  6. background-color: #04aa6d;
  7. color: white;
  8. padding: 16px;
  9. font-size: 16px;
  10. border: none;
  11. cursor: pointer;
  12. }
  13. .dropbtn:hover,
  14. .dropbtn:focus {
  15. background-color: #3e8e41;
  16. }
  17. #myInput {
  18. box-sizing: border-box;
  19. background-image: url(&#39;searchicon.png&#39;);
  20. background-position: 14px 12px;
  21. background-repeat: no-repeat;
  22. font-size: 16px;
  23. padding: 14px 20px 12px 45px;
  24. border: none;
  25. border-bottom: 1px solid #ddd;
  26. }
  27. #myInput:focus {
  28. outline: 3px solid #ddd;
  29. }
  30. .dropdown {
  31. position: relative;
  32. display: inline-block;
  33. }
  34. .dropdown-content {
  35. display: none;
  36. position: absolute;
  37. background-color: #f6f6f6;
  38. min-width: 230px;
  39. overflow: auto;
  40. border: 1px solid #ddd;
  41. z-index: 1;
  42. }
  43. .dropdown-content a {
  44. color: black;
  45. padding: 12px 16px;
  46. text-decoration: none;
  47. display: block;
  48. }
  49. .dropdown a:hover {
  50. background-color: #ddd;
  51. }
  52. .show {
  53. display: block;
  54. }
  55. .scrolable-container {
  56. overflow: auto;
  57. max-height: 420px;
  58. }

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

  1. &lt;body&gt;
  2. &lt;div class=&quot;location-dropdown dropdown&quot;&gt;
  3. &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;
  4. &lt;div id=&quot;myDropdown&quot; class=&quot;dropdown-content&quot;&gt;
  5. &lt;input type=&quot;text&quot; placeholder=&quot;Search..&quot; id=&quot;myInput&quot; onkeyup=&quot;filterFunction()&quot;&gt;
  6. &lt;div class=&quot;scrolable-container&quot;&gt;
  7. &lt;a class=&quot;dropdown-item&quot; href=&quot;link1&quot;&gt;link1&lt;/a&gt;
  8. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link2&lt;/a&gt;
  9. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link3&lt;/a&gt;
  10. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link4&lt;/a&gt;
  11. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link5&lt;/a&gt;
  12. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link6&lt;/a&gt;
  13. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link7&lt;/a&gt;
  14. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link8&lt;/a&gt;
  15. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link9&lt;/a&gt;
  16. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link10&lt;/a&gt;
  17. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link11&lt;/a&gt;
  18. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link12&lt;/a&gt;
  19. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link13&lt;/a&gt;
  20. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link14&lt;/a&gt;
  21. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link15&lt;/a&gt;
  22. &lt;a class=&quot;dropdown-item&quot; href=&quot;link2&quot;&gt;link16&lt;/a&gt;
  23. &lt;/div&gt;
  24. &lt;/div&gt;
  25. &lt;/div&gt;
  26. &lt;script&gt;
  27. function locations_dropdown() {
  28. document.getElementById(&quot;myDropdown&quot;).classList.toggle(&quot;show&quot;);
  29. }
  30. function filterFunction() {
  31. var input, filter, ul, li, a, i;
  32. input = document.getElementById(&quot;myInput&quot;);
  33. filter = input.value.toUpperCase();
  34. div = document.getElementById(&quot;myDropdown&quot;);
  35. a = div.getElementsByTagName(&quot;a&quot;);
  36. for (i = 0; i &lt; a.length; i++) {
  37. txtValue = a[i].textContent || a[i].innerText;
  38. if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
  39. a[i].style.display = &quot;&quot;;
  40. } else {
  41. a[i].style.display = &quot;none&quot;;
  42. }
  43. }
  44. }
  45. &lt;/script&gt;
  46. &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:

确定