【问题标题】:How to make a navbar appear in certain section of the page? (bootstrap too)如何使导航栏出现在页面的某些部分? (也是自举)
【发布时间】:2015-12-13 01:39:13
【问题描述】:

我正在制作一个网站,当他处于移动模式 (> 767px) 时会看到一个导航栏。我已经知道了,但我希望这个导航栏只出现在第 1 节之后。

默认情况下,导航栏一直显示。我希望它仅在我看到第 2 节时出现。

请看下面的例子:

示例:http://jsfiddle.net/gtw7375/3zc5Ltzp/

HTML:

<nav class="navbar navbar-default navbar-fixed-bottom visible-xs">
  <div class="container-fluid"> 

    <div class="navbar-header">
         <!-- <a class="navbar-brand" href="#">LOGO PSTCH</a> </div> -->


    <div class="collapse navbar-collapse visible-xs" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
       <li><a  href="#" data-toggle="modal" data-target="#myModal1" data-direction="bottom"> About</a></li>
       <li><a  href="#" data-toggle="modal" data-target="#myModal2" data-direction='bottom'> Sobre </a></li>
       <li><a  href="#" data-toggle="modal" data-target="#myModal3" data-direction='bottom'> Contact </a></li>


      </ul>    
    </div>
    </div>  
  </div> 
</nav>

 <div id="logo"> 

            <center>
                            <a href="#desce" class="page-scroll  btn btn-xl">SECTION 1</a>
            </center>
  </div>

  <div id="content">

    <p> SECTION 2  </p>

      <p> The navbar will appear here down/hereafter!</p>
  </div>

CSS:

html, body {
    height:100%;
    padding:0;
    margin:0;

}


#logo{
            background: black;
            width: 100%;
            height: 100%;          


}

#content {
        border: 1px solid black;
        width: 100%;
        height: 50%;
}

.navbar .nav  li{
    float:none;
    display:inline-block;
    *display:inline; /* Internet Explorer 7 compatibility */
    *zoom:1;
    vertical-align: bottom;

}


.navbar .navbar-collapse {
  text-align: center;
}

我该怎么做?

【问题讨论】:

  • 请在您的帖子中包含所有相关代码,并且不要仅包含指向代码托管站点的链接。您的帖子应该独立于任何其他资源;考虑一下如果该网站将来出现故障会发生什么!

标签: html css twitter-bootstrap frontend


【解决方案1】:

如果你使用JQuery,你可以监听窗口的滚动事件,检查它是否滚动到元素的顶部偏移之外并采取相应的行动。以下是使用 JQuery 插件的 Javascript 代码。

$(document).ready(function(){

  $(".navbar").hide(); //Hide the navigation bar first

    $(window).scroll(function () {  //Listen for the window's scroll event
        if (isScrolledAfterElement("#content")) { //if it has scrolled beyond the #content elment
            $('.navbar').fadeIn();  //Show the navigation bar
        } else {
            $('.navbar').fadeOut(); //Else hide it
        }
    });

    //Function that returns true if the window has scrolled beyond the given element
    function isScrolledAfterElement(elem) {
        var $elem = $(elem);
        var $window = $(window);

        var docViewTop = $window.scrollTop();
        var docViewBottom = docViewTop + $window.height();

        var elemTop = $elem.offset().top;

        return elemTop <= docViewBottom;
    }
});

您可以在jsfiddle 中看到这一点。我从导航栏中删除了 visible-xs 类,还为 #logo 元素添加了一个 ma​​rgin-bottom 属性以使效果明显对所有用户(您不必对您的项目执行此操作)。

【讨论】:

    【解决方案2】:

    我不太明白你想用你的代码做什么。

    <div id="box01">
         <p>This will be hidden, only visible after 767px.</p>
    </div>
    
    .box01 {
        display: none;
    }
    @media screen and (max-width : 767px) {
        .box01 {
            display: block;
        }
    }
    

    【讨论】:

    • 我希望导航栏仅在页面存放在第 2 部分时出现。如示例所示。明白了吗?
    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多