只有10个可见链接的下拉菜单

时间:2017-04-20 19:11:50

标签: javascript html

所以,我想知道是否有可能使下拉菜单只有10个可见链接,其余的将是不可见的。如果不可能,是否可以制作它,这样你就可以只有10并向下滚动。有什么建议吗?

function myFunction() {
    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++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
        }
    }
}
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}
#myInput {
    border-box: box-sizing;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
}

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

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    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;}
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#custom">Custom</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#custom">Custom</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#custom">Custom</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
  </div>
</div>

3 个答案:

答案 0 :(得分:1)

编辑:新答案:

我误解了这个问题。您可以稍微修改过滤器功能以跟踪显示的项目(10),然后只要打开菜单就调用一次。这样,您在打开菜单和过滤时会显示最多10个项目。小提琴:https://jsfiddle.net/d3kta5sw/

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

function filterFunction() {
    var input, filter, ul, li, a, i,
        elementsToShow = 10,
        elementsShowing = 0;

    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myDropdown");
    a = div.getElementsByTagName("a");

    for (i = 0; i < a.length; i++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1 &&
            elementsShowing < elementsToShow) {
            a[i].style.display = "";
            elementsShowing++;
        } else {
            a[i].style.display = "none";
        }
    }
}

旧答案(仅适用于前10个可见项目):

您可以在纯CSS中执行此操作。尝试添加以下内容,这将隐藏第11个'a'元素及其后:

.dropdown-content a:nth-of-type(n+11) {
  display:none;
}

答案 1 :(得分:0)

CSS选项:

.show{
    overflow:overlay: //to hide to the other elements
    height:200px; //to show desired number of elements
}

最好使用这个元素:

.dropdown-content

答案 2 :(得分:0)

您可以添加前10个项目的高度值并在CSS中手动设置,或者您可以使用Javascript计算前10个项目并将它们的高度加在一起。因为它现在都具有相同的高度,所以你可以做h*10但是如果由于某种原因,一个项目突破到第二行,你的高度将会关闭。我已经更新了你的剪辑。

我已更新myFunction功能。您会注意到我在计算高度之前添加了show类,因为如果隐藏了父级,offsetHeight将始终返回0。

单击关闭按钮时隐藏

我们可以使用blur事件处理此问题。而不是像onclick这样的事件使用属性,我们只用JS来处理这个。

返回下拉列表

要在单击下拉列表后返回到下拉列表的顶部,我们需要使用scrollTop属性。在myFunction()我们在scrollTop = 0元素上设置dropDown的最后一行,它将我们滚动到顶部!

var btn = document.querySelector('.dropbtn');

btn.addEventListener('blur', function() {
  var dd = document.querySelector('.dropdown-content');
  if ( dd.classList.contains('show') ) {
    dd.classList.remove('show');
  }
});

&#13;
&#13;
function myFunction() {
  var dropDown = document.getElementById('myDropdown'),
    items = dropDown.children,
    height = 0;
  dropDown.classList.toggle('show');
  for (var i = 1; i < 10; i++) {
    height += items[i].offsetHeight;
  }
  dropDown.style.height = height + 'px';
  dropDown.scrollTop = 0;
}

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++) {
    if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
      a[i].style.display = "";
    } else {
      a[i].style.display = "none";
    }
  }
}

var btn = document.querySelector('.dropbtn');

btn.addEventListener('blur', function() {
  var dd = document.querySelector('.dropdown-content');
  if ( dd.classList.contains('show') ) {
    dd.classList.remove('show');
  }
});
&#13;
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

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

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

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

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f6f6f6;
  min-width: 230px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  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;
}
&#13;
<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#custom">Custom</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#custom">Custom</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
    <a href="#about">About</a>
    <a href="#base">Base</a>
    <a href="#blog">Blog</a>
    <a href="#contact">Contact</a>
    <a href="#custom">Custom</a>
    <a href="#support">Support</a>
    <a href="#tools">Tools</a>
  </div>
</div>
&#13;
&#13;
&#13;