bootstrap scrollspy highlights last navigation link button after loading

10,573

Solution 1

have you added data-spy="scroll" to the <body> tag (or any other tag that's relevant)?

Solution 2

If you have the data-spy set on the body element, make sure that the body element isn't set to height: 100%;. This was the problem in my case.

Solution 3

This was just happening to me, and I had data-spy="scroll" on an extra element inside the one I was trying to scroll (it was left over from something, shouldn't have been there). Removing this fixed the issue.

Solution 4

Latest bootstrap documentation suggests following

add data-spy="scroll" to the element you want to spy on (most typically this would be the body) and data-target=".navbar" to select which nav to use

Via data-attributes

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

$(function () {
    $('#MenuUl a:first').tab('show');
  })

I hope that helps

Share:
10,573

Related videos on Youtube

Alon
Author by

Alon

I'm a Web Developer

Updated on June 04, 2022

Comments

  • Alon
    Alon about 2 years

    I've built a navigation bar at the top with 5 links. At the page itself I have added 5 sections with id's according to the name after the # tag in the element. The problem is that the last navigation link button is highlighted ('active' class was added to it on runtime in the beginning although in the code itself the first navbar item is set to "active". I have the latest bootstrap version which is v2.0.4 What is wrong ?

    <div class="navbar" id="MenuBar">
        <div class="topHeadContentLogo"></div>
        <ul class="nav nav-pills" id="MenuUl">
            <li class="active"><a href="#Weekly">Weekly</a></li>
            <li class=""><a href="#Post">Post</a></li>
            <li class=""><a href="#Audience">Audience</a></li>
            <li><a href="#Website">Website</a></li>
            <li><a href="#FAQ">FAQ</a></li>
        </ul>
    </div>
    
    <div id="contentDiv">
        <section id="Weekly">
            <h1>weekly</h1>
        </section>
    
        <section id="Post">
            <h1>Post</h1>
        </section>
    
        <section id="Audience">
            <h1>Audience</h1>
        </section>
    
        <section id="Website">
            <h1>Website</h1>
        </section>
    
        <section id="FAQ">
            <h1>FAQ</h1>
        </section>
    </div>
    
    
    
    $('#MenuBar').scrollspy(); after document ready
    
    • davebowker
      davebowker almost 12 years
      Strange, I'm having the same issue with the data attr added in the body tag. Any solution to this?
    • pwnjack
      pwnjack over 7 years
      I encountered the same issue, i did a jquery workaround by simply removing the "active" class on page load and adding it again to the first element instead of the wrong, like this: $('.post-index li.active').removeClass('active'); $('.post-index li:first-child').addClass('active');
  • Alon
    Alon about 12 years
    Yes, I have added that tag, but still the last element is being activated on loading
  • Luca
    Luca about 12 years
    have you tried adding enough content to the different sections, so that they do not all appear in your browser window at the same time? I've tried your code in a bootstrap page I've built and it seems to be all ok. I just added a lot of text to the sections to make sure that only one at time was displayed in my browser window.
  • Kyle Cureau
    Kyle Cureau over 11 years
    This was my issue! Thanks
  • knownasilya
    knownasilya about 11 years
    Thank you, that helped.. I should have read it more clearly, skipped the intro and went to the using with JavaScript section too quick.
  • markus
    markus over 10 years
    How to use a sticky footer and a scrollspy then?
  • Adrian P.
    Adrian P. almost 10 years
    If you have a portofolio items on one section be sure you don't use filter to show only one category. It's a conflict.
  • Asaf Bartov
    Asaf Bartov about 4 years
    thank you! This was my issue, and it drove me nuts. :)