How to use Bootstrap scroll spy?

前端 未结 8 561
眼角桃花
眼角桃花 2020-12-08 02:52

I cannot quite get the scroll spy to work properly with a vertical nav. Below you can find the code I use. For some reason, only \"Two\" gets active.

Anyone has an i

相关标签:
8条回答
  • 2020-12-08 03:29
    $(window).scroll(function() {
        if ($(".navbar").offset().top > 50) {
            $(".navbar-fixed-top").addClass("top-nav-collapse");
        } else {
            $(".navbar-fixed-top").removeClass("top-nav-collapse");
        }
    });
    
    //jQuery for page scrolling feature - requires jQuery Easing plugin
    $(function() {
        $('a.page-scroll').bind('click', function(event) {
            var $anchor = $(this);
            $('html, body').stop().animate({
                scrollTop: $($anchor.attr('href')).offset().top
            }, 1500, 'easeInOutExpo');
            event.preventDefault();
        });
    });
    
    0 讨论(0)
  • 2020-12-08 03:34
    .nameClasse {
      position: relative;
      overflow: auto;
      height: 330px;
    }
    
    <div data-spy="scroll" data-target="#navbarVertical" data-offset="0" class="nameClass">
    
    0 讨论(0)
  • 2020-12-08 03:35

    After hours of frustration and research, I came to find that many people are also not pleased with Bootstrap Affix's documentation (www.mtjhax.wordpress.com). After all the looking around though, this solution on Bootply worked for me: http://bootply.com/63937

    Say your navigation had the id "navbar", the body should look like this:

    <body data-spy="scroll" data-target="#navbar">
        ...
    </body>
    

    And the navbar itself should be as follows:

    <div class="navbar" id="navbar">
        <ul class="nav navbar-nav">
            <li>...</li>
            <li>...</li>
            <li>...</li>
        </ul>               
    </div>
    

    You then initialized the plugin...

    $('#nav').affix({
        offset: {top: 0}
    }); 
    

    ... and give the navigation proper styling:

    #nav.affix {
        position: fixed;
        top: 0;
        width: 100%
    }
    
    0 讨论(0)
  • 2020-12-08 03:36

    I had to do two things to get scrollspy to work for me.

    First, I had to give a height to the <body> element:

    <style>
    
      .body
      {
        height: 100%;
      }
    
    </style>    
    
    <body data-spy="scroll" data-target="#scrollElement" data-offset="0">
    

    I think this is because scroll-spy is trying to get the height of the element that you're spying on, but if there isn't a height specified for your element, it gets passed a null (or 0?) value, which seems to be breaking the script's calculations.

    Second, I had to make sure that there was enough vertical space between my id elements (that the nav anchors are pointing towards).

    I couldn't figure out the exact amount of space necessary (it did vary depending on the data-offset attribute, so it probably has something to do with that), but I reckon that if you have a height set to your spy element and scrollspy still isn't working - just add more content between your id elements.

    0 讨论(0)
  • 2020-12-08 03:37

    you can do this like http://jsfiddle.net/mCxqY/

    <body data-spy="scroll" data-target="#navbar">
        <div id="post1" class="box">
            <h1>Post 1</h1>
            <p> Scroll Down↓</p>
        </div>
        <div id="post2" class="box"><h1>Post 2</h1></div>
        <div id="post3" class="box"><h1>Post 3</h1></div>
    
        <div id="navbar">
            <ul class="nav">
                <li><a href="#post1">Post 1</a></li>
                <li><a href="#post2">Post 2</a></li>
                <li><a href="#post3">Post 3</a></li>
            </ul>
        </div>
    
        <script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-scrollspy.js"></script>
    </body>​
    

    css

    @import url(http://twitter.github.com/bootstrap/assets/css/bootstrap.css);
    .box{
        margin: 20px; padding: 15px;
        background: #eee;
        height: 500px;
    }
    #navbar{
        position: fixed;
        bottom: 0; left: 20px;
        width: 100%;
        background: #fff;
    }
    .nav li a{
        float: left;
        width: 80px;
        padding: 15px 0;
    }
    .nav li a:hover{
        color: #f33 !important;
        background: none;
    }
    .nav li.active a{
        color: #f55;
        text-decoration: underline;
    }
    

    0 讨论(0)
  • 2020-12-08 03:44

    Thanks Jerreck I was able to make mine works with the help of

    height: 100%;
    

    in the body selector of my CSS

    Also as recommended by the guys at Bootstrap http://getbootstrap.com/javascript/#scrollspy

    I've added position: relative; in the body too.

    The beginning of the body selector in my CSS goes like this:

    body {
        position: relative;
        height: 100%;
        /* … */
    }
    

    In the <body> in HTML I had to remove

    data-offset="0"
    

    so it is now like this

    <body data-spy="scroll" data-target="#navigation">
    
    0 讨论(0)
提交回复
热议问题