javascript - Bootstrap JS Scrollspy 进入 Bootstrap 面板

标签 javascript jquery html css twitter-bootstrap

我在将 Bootstrap JS Scrollspy 添加到 Bootstrap 面板时遇到了一些问题。我想要一个固定的 Nav 进入面板,如 W3School Example . 如果我尝试设置固定导航,我的导航栏会进入真正的导航栏位置。 我不知道如何为它设置好的选项,我的面板有一个固定的高度,所以我必须允许溢出。

$(document).ready(function(){
  $('#fakeBody').scrollspy({target: "#navbarPnlBody", offset: 50});

  // Add smooth scrolling on all links inside the navbar
  $("#pnlNavBar a").on('click', function(event) {
    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('.scrollbar,#fakeBody').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){
   
        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    }  // End if
  });
});
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<!--REAL MENU-->
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navBar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="#" class="navbar-brand">Real Navbar Title</a>
    </div>
    <div class="collapse navbar-collapse" id="navBar">
      <ul class="nav navbar-nav">
        <li><a href="#"><span class="glyphicon glyphicon-home"></span> Home</a></li>
      </ul>
    </div>
  </div>
</nav>
<!--END FAKE MENU-->



  <kbd>OTHER PANEL AND EXTC...</kbd>



<div class="container-fluid" style="max-height: 10;">
<div class="panel panel-primary">
  <div class="panel-heading">
    <span class="glyphicon glyphicon-search"></span>
    Header
  </div>
  <div class="panel-body">
    
    
    
    
    <div id="fakeBody" data-spy="scroll" data-target="#navbarPnlBody" data-offset="">
      <nav id="navbarPnlBody" class="navbar navbar-default navbar-static">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#pnlNavBar">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a href="#" class="navbar-brand">Navbar Title</a>
          </div>
          <div>
            <div class="collapse navbar-collapse" id="pnlNavBar">
              <ul class="nav navbar-nav">
                <li class="active"><a href="#section1">Section 1</a></li>
                <li><a href="#section2">Section 2</a></li>
                <li><a href="#section3">Section 3</a></li>
              </ul>
            </div>
          </div>
        </div>
      </nav>

      <div id="section1" class="container-fluid">
        <h1>Section 1</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
      </div>
      <div id="section2" class="container-fluid">
        <h1>Section 2</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
      </div>
      <div id="section3" class="container-fluid">
        <h1>Section 3</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
      </div>
    </div>
    
    
  </div>
  </div>
</div>

最佳答案

将其添加到真正的导航栏 div 中:

style="position: fixed; width: 100%; z-index: 1;"

你应该得到:

$(document).ready(function(){
  $('#fakeBody').scrollspy({target: "#navbarPnlBody", offset: 50});

  // Add smooth scrolling on all links inside the navbar
  $("#pnlNavBar a").on('click', function(event) {
    // Make sure this.hash has a value before overriding default behavior
    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('.scrollbar,#fakeBody').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){
   
        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    }  // End if
  });
});
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<!--REAL MENU-->
<nav class="navbar navbar-default" style="position: fixed; width: 100%; z-index: 1;">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navBar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a href="#" class="navbar-brand">Real Navbar Title</a>
    </div>
    <div class="collapse navbar-collapse" id="navBar">
      <ul class="nav navbar-nav">
        <li><a href="#"><span class="glyphicon glyphicon-home"></span> Home</a></li>
      </ul>
    </div>
  </div>
</nav>
<!--END FAKE MENU-->



  <kbd>OTHER PANEL AND EXTC...</kbd>



<div class="container-fluid" style="max-height: 10;">
<div class="panel panel-primary">
  <div class="panel-heading">
    <span class="glyphicon glyphicon-search"></span>
    Header
  </div>
  <div class="panel-body">
    
    
    
    
    <div id="fakeBody" data-spy="scroll" data-target="#navbarPnlBody" data-offset="">
      <nav id="navbarPnlBody" class="navbar navbar-default navbar-static">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#pnlNavBar">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a href="#" class="navbar-brand">Navbar Title</a>
          </div>
          <div>
            <div class="collapse navbar-collapse" id="pnlNavBar">
              <ul class="nav navbar-nav">
                <li class="active"><a href="#section1">Section 1</a></li>
                <li><a href="#section2">Section 2</a></li>
                <li><a href="#section3">Section 3</a></li>
              </ul>
            </div>
          </div>
        </div>
      </nav>

      <div id="section1" class="container-fluid">
        <h1>Section 1</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
      </div>
      <div id="section2" class="container-fluid">
        <h1>Section 2</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
      </div>
      <div id="section3" class="container-fluid">
        <h1>Section 3</h1>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
        <p>Try to scroll this section and look at the navigation bar while scrolling! Try to scroll this section and look at the navigation bar while scrolling!</p>
      </div>
    </div>
    
    
  </div>
  </div>
</div>

关于javascript - Bootstrap JS Scrollspy 进入 Bootstrap 面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38768772/

相关文章:

javascript - 使用 javascript 计算所有显示的具有值的输入(未隐藏)

javascript - setPosition 在 iphone safari 浏览器上不更新

javascript - Sails.js:特定于 socket.io 的策略

javascript - 如何为下一页/上一页获取 Page Flipper 动画

html - 在文本框上显示搜索图像

javascript - 如何在 jQuery 的 .html() 方法中使用 JavaScript 变量

javascript - 从ajax接收多个变量并使用php将其写入文本文件

javascript - 在设定的时间间隔后更改图像属性

jQuery 没有将 css 设置为直接在 IE 中的页面加载上隐藏 div

javascript - jQuery 点击事件似乎有点晚了