简体   繁体   English

为我的导航栏下拉菜单启用滚动条

[英]Enable scroll bar for my Nav Bar Dropdown

I made a navigation bar for my website with some dropdown at hover content. 我为网站制作了一个导航栏,并在悬停内容上添加了一些下拉菜单。 However, it seems that scrolling is disabled for them and I have tried many ways but failed to enable the scrollbar. 但是,似乎没有为他们启用滚动功能,我尝试了许多方法,但未能启用滚动条。 Since the dropdown content is quite long I would like the scrollbar to only appear when the resolution or window size is small. 由于下拉内容很长,因此我希望滚动条在分辨率或窗口大小较小时显示。

CSS(Not full, I didn't put most the parts regarding the styles): CSS(不完整,关于样式,我没有放置大部分内容):

 ul.nav li a { display: block; color: white; text-align: center; padding: 16px 16px; text-decoration: none; font-family: Arial; z-index: 5; } ul.nav li a:hover:(.active), .dropdown:hover .dropbtn { background-color: #760808; } .dropdown-content { display: none; position: absolute; background-color: #830303; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 50; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; z-index: 50; } 
 <ul class="nav"> <li><a href="homepage.html">Homepage</a> </li> <li class="dropdown"> <a class="active" href="javascript:void(0" class="dropbtn">a</a> <div class="dropdown-content"> <a href="#">Link 1</a><a href="#">Link 2</a> <a href="#">Link 3</a></div> </li> <li class="dropdown"> <a href="javascript:void(0" class="dropbtn">be</a> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> </div> </li> <li class="dropdown"> <a href="javascript:void(0" class="dropbtn">c</a> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> </div> </li> <li><a href="../d.html">d</a></li> <li><a href="../e.html">e</a></li> </ul> 

.dropdown-content {
   max-height: 240px; /* or any other value you want */
   overflow-y: auto;
}

But do note your current href expressions are invalid. 但请注意,您当前的href表达式无效。 Should be 应该

href="javascript:void(0)"

(you need to close the paranthesis). (您需要关闭括号)。

 $('.dropdown').on('mouseenter', function(e){ $('.dropdown-content', this).show(); }).on('mouseleave', function(e){ $('.dropdown-content', this).hide(); }) 
 ul.nav { display: flex; } ul.nav li { display: block; } ul.nav li a { display: block; text-align: center; padding: 16px 16px; text-decoration: none; font-family: Arial; z-index: 5; } ul.nav li a:hover:(.active), .dropdown:hover .dropbtn { background-color: #760808; } .dropdown-content { display: none; position: absolute; background-color: #830303; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2); z-index: 50; max-height: 240px; overflow-y: auto; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; text-align: left; z-index: 50; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="nav"> <li><a href="homepage.html">Homepage</a> </li> <li class="dropdown"> <a class="active" href="javascript:void(0)" class="dropbtn">a</a> <div class="dropdown-content"> <a href="#">Link 1</a><a href="#">Link 2</a> <a href="#">Link 3</a></div> </li> <li class="dropdown"> <a href="javascript:void(0)" class="dropbtn">be</a> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> </div> </li> <li class="dropdown"> <a href="javascript:void(0)" class="dropbtn">c</a> <div class="dropdown-content"> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> </div> </li> <li><a href="../d.html">d</a></li> <li><a href="../e.html">e</a></li> </ul> 

You might also be interested in reading why using javascript in href's is considered bad practice from this question and it's accepted answer. 您可能还想读一读为什么从此问题中将在href中使用javascript视为不良做法,并将其作为可接受的答案。

Which is to say: returning void(0) works, but if you ever get tempted to use other javascript expressions in hrefs you'll probably forget the context is different, which will lead to subtle errors and unexpected behavior for your apps. 这就是说:返回void(0)是可行的,但是如果您试图在hrefs中使用其他javascript表达式,则可能会忘记上下文是不同的,这将导致细微的错误和应用程序意外的行为。

There seems to be some errors in your css and inline javascript: 您的CSS和内联javascript中似乎存在一些错误:

//JAVASCRIPT
href="javascript:void(0)"

//CSS
ul.nav li a.active:hover{}

I can't really recreate your issue based on the code you provided. 我真的无法根据您提供的代码来重新创建您的问题。 However, for the problem that you are facing you will need to look into the overflow-y property in css. 但是,对于您面临的问题,您需要查看css中的overflow-y属性。 Particularly you would want to set that property to scroll Such as overflow-y:scroll for each element that you would like to add a vertical scrollbar too. 特别是,您希望将该属性设置为scroll例如,也要为每个元素添加一个垂直滚动条,例如overflow-y:scroll Similarly if you would like a horizontal scroll bar then set overflow-x:scroll . 同样,如果您想要水平滚动条,请设置overflow-x:scroll

As far as window size is concerned you will need to use media queries for a css only solution. 就窗口大小而言,您将需要针对仅css解决方案使用媒体查询 There is a lot of information on media queries and how they work and you will need to figure out what works for your application. 有关媒体查询以及它们如何工作的信息很多,您将需要弄清楚哪种方法适用于您的应用程序。 This is an example of what it may look like: 这是它可能看起来的示例:

@media screen and (min-width: 480px) {
    .dropdown-content {
        overflow-y: scroll;
        /* Optionally: "overflow-y: auto;" */
    }
}

NOTE: 注意:

If you are designing your website for view on small screens like touchscreen devices, there is no hover event for those devices, hover is in effect like on click or on focus . 如果您将网站设计为在小屏幕(如触摸屏设备)上查看,则不会发生这些设备的hover事件, hover实际上是on clickon focus So please be mindful with using hover and designing for mobile devices. 因此,请注意在移动设备上使用悬停和进行设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM