简体   繁体   English

在移动视口中时,如何阻止bootstrap 4导航栏粘在页面顶部?

[英]How can I stop the bootstrap 4 navbar from sticking to the top of the page when in mobile viewport?

I want the navbar to be sticky on wide screen, and this works. 我希望导航栏在宽屏上保持粘性,并且可以正常工作。 I don't want it to collapse when on a mobile viewport. 我不希望它在移动视口中折叠。 How can I make it so that the navbar simply unsticks from the top when on a small screen? 在小屏幕上,如何使导航栏从顶部完全松开? Here is my HTML: 这是我的HTML:

<div  class="navbar-light" style="position: sticky; top: 0px; background-color: #fff0d8;">  
<div class="container">
<ul class="sc-nav nav navbar-nav nav-fill w-100 flex-md-row">
<li class="nav-item">
    <a class="nav-link" href="#">aaa</a>
</li>
<li class="nav-item">
    <a class="nav-link" href="#">bbbb</a>
</li>
<li class="nav-item">
    <a class="nav-link" href="#">ccc</a>
</li>
<li class="nav-item">
    <a class="nav-link" href="#">ddd</a>
</li>
<li class="nav-item">
    <a class="nav-link" href="#">eee</a>
</li>
<li class="nav-item">
    <a class="nav-link" href="#">ffffffffff</a>
</li>
<li class="nav-item">
    <a class="nav-link" href="#">sdasdasdasd</a>
</li>
</ul>

Just use a media query to change the position to relative . 只需使用媒体查询position更改为relative Note that you'll need !important to override your inline style declaration, as inline styles have the maximum level of specificity . 请注意,您需要!important来覆盖内联样式声明,因为内联样式具有最高的特异性

@media screen and (max-width: 768px) { /* Bootstrap's mobile view breakpoint */
  .navbar-light {
    position: relative !important;
  }
}

This can be seen in the following example: 在以下示例中可以看到:

 @media screen and (max-width: 480px) { .navbar-light { position: relative !important; } } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> <div class="navbar-light" style="position: sticky; top: 0px; background-color: #fff0d8;"> <div class="container"> <ul class="sc-nav nav navbar-nav nav-fill w-100 flex-md-row"> <li class="nav-item"> <a class="nav-link" href="#">aaa</a> </li> <li class="nav-item"> <a class="nav-link" href="#">bbbb</a> </li> <li class="nav-item"> <a class="nav-link" href="#">ccc</a> </li> <li class="nav-item"> <a class="nav-link" href="#">ddd</a> </li> <li class="nav-item"> <a class="nav-link" href="#">eee</a> </li> <li class="nav-item"> <a class="nav-link" href="#">ffffffffff</a> </li> <li class="nav-item"> <a class="nav-link" href="#">sdasdasdasd</a> </li> </ul> 

Hope this helps! 希望这可以帮助! :) :)

Sticky top 粘顶

Position an element at the top of the viewport, from edge to edge, but only after you scroll past it. 将元素从一个边缘到另一个边缘放置在视口的顶部,但仅在滚动经过该元素之后才可以。 The .sticky-top utility uses CSS's position: sticky , which isn't fully supported in all browsers. .sticky-top实用程序使用CSS的position: sticky ,并非所有浏览器都完全支持它。

Microsoft Edge and IE11 will render position: sticky as position: relative . Microsoft Edge和IE11将呈现position: sticky作为position: relative As such, we wrap the styles in a @supports query, limiting the stickiness to only browsers that properly can render it. 这样,我们将样式包装在@supports查询中,从而将粘性限制为仅适当地可以呈现它的浏览器。

<div class="sticky-top">...</div>

  • and create a custom break point at @media screen and (max-width: 767px) to match flex-md-row break point. 并在@media screen and (max-width: 767px)创建一个自定义断点, @media screen and (max-width: 767px)匹配flex-md-row断点。

https://getbootstrap.com/docs/4.0/layout/overview/#responsive-breakpoints https://getbootstrap.com/docs/4.0/layout/overview/#sensitive-breakpoints

// Extra small devices (portrait phones, less than 576px) //额外的小型设备(纵向手机,小于576px)

// No media query since this is the default in Bootstrap //没有媒体查询,因为这是Bootstrap中的默认设置

// Small devices (landscape phones, 576px and up) //小型设备(横向手机,576像素及以上)

@media (min-width: 576px) { ... } @media(最小宽度:576像素){...}

// Medium devices (tablets, 768px and up) //中型设备(平板电脑,768像素及以上)

@media (min-width: 768px) { ... } @media(最小宽度:768像素){...}

// Large devices (desktops, 992px and up) //大型设备(台式机,992px及以上)

@media (min-width: 992px) { ... } @media(最小宽度:992px){...}

// Extra large devices (large desktops, 1200px and up) //特大型设备(大型台式机,1200px及以上)

@media (min-width: 1200px) { ... } @media(最小宽度:1200px){...}

 @media screen and (max-width: 767px) { .sticky-top { position: relative !important; } } 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" > <div class="navbar-light" style="background-color: #fff0d8;"> <div class="container"> <ul class="sc-nav nav navbar-nav nav-fill w-100 flex-md-row sticky-top"> <li class="nav-item"> <a class="nav-link" href="#">aaa</a> </li> <li class="nav-item"> <a class="nav-link" href="#">bbbb</a> </li> <li class="nav-item"> <a class="nav-link" href="#">ccc</a> </li> <li class="nav-item"> <a class="nav-link" href="#">ddd</a> </li> <li class="nav-item"> <a class="nav-link" href="#">eee</a> </li> <li class="nav-item"> <a class="nav-link" href="#">ffffffffff</a> </li> <li class="nav-item"> <a class="nav-link" href="#">sdasdasdasd</a> </li> </ul> <div style="height:200vh">give a scroll to the demo</div> 

暂无
暂无

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

相关问题 当用户单击jQueryUI单选按钮时,如何停止浏览器视口移至页面顶部? - How can I stop the browser viewport moving to the top of the page when the user clicks on a jQueryUI radio button? Bootstrap Navbar-当前面的下拉列表折叠时,如何确保下拉列表的顶部在视口中? - Bootstrap Navbar - How do I ensure that the top of a dropdown is in the viewport when the preceding dropdown collapses? 将导航栏粘贴到页面顶部时,它从中间到最左侧 - Upon sticking navbar to top of page, it goes from the middle to the very left 滚动 div 后,如何将引导程序导航栏固定到顶部? - How can I fix the bootstrap navbar to the top after a div was scrolled? 放大CSS HTML时如何防止DIV停留在页面左侧 - How to stop DIV from sticking to left of page when zoom in CSS HTML 如何加速Bootstrap导航栏中的移动下拉列表? - How can I speed up the mobile dropdown in the Bootstrap navbar? Twitter引导手风琴向导每次都会滚动到页面顶部。 我该如何阻止呢? - Twitter bootstrap accordion wizard scrolls to the top of the page every time. how can i stop that? 导航栏展开时,如何使它滚动并固定到页面顶部? - How can I make the navbar scroll and fix to the top of the page when it is expanded? 仅在移动设备上滚动到顶部时显示导航栏(引导程序4) - Show Navbar only when scrolling to top on mobile (Bootstrap 4) 防止浏览器视口在运行时页面高度增加时粘在底部? - Prevent browser viewport from sticking to bottom when page height increases in runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM