Question

I have created and exact fiddle of my problem here for testing: http://jsfiddle.net/aVBUy/7/

The issue is, when I click on navbar items, I have a script which scrolls to the element id. And I am using scrollspy to highlight the nav elements when page is in correct position. However the scrollspy is only changing the active state when it hits the top of the browser/device. Because my navbar is fixed I need an offset applied to scrollspy to offset by 51px (navbar height).

I've tried everything and I can't get it to work. Please check my fiddle and see if you can find the where I'm going wrong, would help me so much.

Here's my minimised code...

HTML

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
            <a class="brand" href="#"><img src="img/logo.gif" alt="" /></a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    <li><a href="#welcome" data-scroll="#welcome">Welcome</a></li>
                    <li><a href="#about" data-scroll="#about">About</a></li>
                    <li><a href="#route" data-scroll="#route">The Route</a></li>
                    <li><a href="#bike" data-scroll="#bike">The Bike</a></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<div class="container">
    <div id="welcome" class="row-fluid">
        <div class="span12">
            <h3>Welcome</h3>
            ...
        </div>
    </div>
    <hr />
    <div id="about" class="row-fluid">
        <div class="span12">
            <h3>About the ride</h3>
            ...
        </div>
    </div>
    <hr />
    <div id="route" class="row-fluid">
        <div class="span12">
            <h3>The Route</h3>
            ...
        </div>
    </div>
    <hr />
    <div id="bike" class="row-fluid">
        <div class="span12">
            <h3>The Bike</h3>
            ...
        </div>
    </div>
    <hr>
    <footer>
        <p class="muted"><small>© 2013 All rights reserved.</small></p>
    </footer>
</div>

CSS

body {
    padding: 51px 0 0;
}
/* Override Bootstrap Responsive CSS fixed navbar */
 @media (max-width: 979px) {
    .navbar-fixed-top, .navbar-fixed-bottom {
        position: fixed;
        margin-left: 0px;
        margin-right: 0px;
    }
}
body > .container {
    padding: 0 15px;
}

SCRIPT

var offsetHeight = 51;

$('.nav-collapse').scrollspy({
    offset: offsetHeight
});

$('.navbar li a').click(function (event) {
    var scrollPos = $('body > .container').find($(this).attr('href')).offset().top - offsetHeight;
    $('body,html').animate({
        scrollTop: scrollPos
    }, 500, function () {
        $(".btn-navbar").click();
    });
    return false;
});

FIDDLE

http://jsfiddle.net/aVBUy/7/

Was it helpful?

Solution

You need to apply the offset to the body.

$('body').scrollspy({
   offset: offsetHeight
});

Also, you will need to subtract at least one from the offsetHeight in the line below, otherwise it will not work when scrolling up.

var scrollPos = $('body > .container').find($(this).attr('href')).offset().top - (offsetHeight - 1);

OTHER TIPS

You can also achieve this without javascript (via data attributes), by declaring data-offset="51" in addition to data-target.

Source: https://getbootstrap.com/docs/3.3/javascript/#scrollspy-usage

With only javascript it works as simple as this:

$( document ).ready(function() {
    //Scrollspy offset
    $("body").scrollspy({target: "#scroll-nav", offset:200});
});

There is any easier way to do this using the attributes of the HTML tag that you wish to spy on.

<ul class="nav nav-list affix" data-spy="affix" data-offset-top="585" data-offset-  bottom="425" id="prepare-navigation">
  <li class="nav-header">Where?</li>
  <li class="divider"></li>
  <li><a href="#at-the-museum-day-time">When?</a></li>
  <li class="divider"></li>
  <li><a href="#at-the-museum-overnight">Why?</a></li>
</ul>

The data-offset-top and data-offset-bottom attributes can be used in conjuntion with the amount that you wish to offest by. Much easier!

Check out this link for a better explanation: https://stackoverflow.com/a/18326668/335567

Little code correction for bootstrap 3 (there is a litle bug when non minimized version of menu is clikcked /some flickering/)

$('.navbar li a').click(function (event) {
var scrollPos = $('body > .container').find($(this).attr('href')).offset().top - (offsetHeight - 1);
$('body,html').animate({
    scrollTop: scrollPos
}, 500, function () {
    if ($("div.navbar-collapse").hasClass("in")) {
        $(".navbar-toggle").click();
    } else {

    }
});
return false;});

In the CSS , body tag's position attribute should have the value relative

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top