Bootstrap scrollspy offset on a fixed navbar does not work
Asked Answered
N

6

16

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/

Neogothic answered 26/7, 2013 at 11:11 Comment(0)
M
23

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);
Marginate answered 26/7, 2013 at 15:23 Comment(2)
I use offset: $(window).height() * 0.2Unaccustomed
This answer may have worked in older versions of Bootstrap, but it does not work with Bootstrap 4. FYI to others looking for a solution to this issue.Septate
W
12

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

Wadding answered 15/3, 2014 at 2:22 Comment(1)
This also works for Bootstrap 4, getbootstrap.com/docs/4.0/components/scrollspy/#optionsExit
O
4

With only javascript it works as simple as this:

$( document ).ready(function() {
    //Scrollspy offset
    $("body").scrollspy({target: "#scroll-nav", offset:200});
});
Orthopter answered 9/3, 2015 at 9:44 Comment(0)
L
2

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://mcmap.net/q/747869/-bootstrap-scrollspy-and-sticky-navigation-not-working-together

Letishaletitia answered 7/11, 2013 at 17:4 Comment(0)
A
1

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;});
Alena answered 10/12, 2013 at 13:36 Comment(0)
N
0

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

Neidaneidhardt answered 10/5, 2017 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.