Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrollspy doesn't like hidden navigation element #13071

Closed
AnonymousMeerkat opened this issue Mar 14, 2014 · 3 comments · Fixed by #14569
Closed

Scrollspy doesn't like hidden navigation element #13071

AnonymousMeerkat opened this issue Mar 14, 2014 · 3 comments · Fixed by #14569

Comments

@AnonymousMeerkat
Copy link

This is my code:

    <div class="navbar-header hidden-md hidden-lg">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbarthingy">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand active" href="#home">
            XYZ
        </a>
    </div>
    <div id="navbarthingy" class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
            <li class="hidden-xs hidden-sm"><a class="navbar-brand" href="#home">XYZ</a></li>
            <li class="hidden-md hidden-lg"><a href="#home">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#faq">F.A.Q.</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#blog">Blog</a></li>
        </ul>
    </div>

It looks fine with mobile and desktop, until I add scrollspy. One that happens, when i scroll down to about, it flickers between XYZ and About constantly (in the desktop page).

The fix for this is trivial:

    ScrollSpy.prototype.refresh = function () {
      var offsetMethod = this.$element[0] == window ? 'offset' : 'position'

      this.offsets = $([])
      this.targets = $([])

      var self     = this
      var $targets = this.$body
        .find(this.selector)
        .map(function () {
          var $el   = $(this)
          var href  = $el.data('target') || $el.attr('href')
          var $href = /^#./.test(href) && $(href)

          return ($href
            && $el.is(':visible') // <==  This is the magic line to include
            && $href.length
            && $href.is(':visible')
            && [[ $href[offsetMethod]().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null
        })
        .sort(function (a, b) { return a[0] - b[0] })
        .each(function () {
          self.offsets.push(this[0])
          self.targets.push(this[1])
        })
    }

After that fix, everything works fine =)

@juthilo juthilo added the js label Mar 15, 2014
@juthilo juthilo added this to the v3.2.0 milestone Mar 15, 2014
@fabritw
Copy link

fabritw commented Jul 3, 2014

Thanks MiJyn for this fix!

I also had to include the below line:
&& !$el.hasClass('disabled')

@twbs - Perhaps a parameter supplied at initialisation to define the element selectors to be ignored would be useful?

hnrch02 added a commit that referenced this issue Sep 8, 2014
Saranya-Raaj pushed a commit to Saranya-Raaj/bootstrap that referenced this issue Oct 9, 2014
@hnrch02 hnrch02 reopened this Oct 25, 2014
@hnrch02 hnrch02 modified the milestones: v3.3.1, v3.3.0 Oct 25, 2014
@hnrch02
Copy link
Collaborator

hnrch02 commented Oct 25, 2014

We needed to revert the fix for this because of #14872. Moved the target version to 3.3.1.

@hnrch02 hnrch02 modified the milestones: v3.3.2, v3.3.1 Nov 11, 2014
@mdo mdo modified the milestones: v3.3.2, v3.3.3 Jan 19, 2015
@cvrebert cvrebert modified the milestones: v3.3.5, v3.3.4 Mar 15, 2015
@mdo mdo modified the milestones: v3.3.5, v3.3.6 Jun 16, 2015
@cvrebert cvrebert added the v3 label Aug 19, 2015
@mdo mdo modified the milestones: v3.3.6, v3.3.7 Nov 24, 2015
@mdo mdo modified the milestones: v3.3.7, v3.3.6 Nov 24, 2015
@cvrebert cvrebert modified the milestones: v3.3.7, v3.3.8 Jul 25, 2016
@mdo
Copy link
Member

mdo commented Sep 5, 2016

Bootstrap 3 is no longer being officially developed or supported.

All work has moved onto our next major release, v4. As such, this issue or pull request is being closed as a "won't fix." For additional help and support, we recommend utilizing our community resources. Thanks for your understanding, and see you on the other side of v4!

<3,
@mdo and team

@mdo mdo closed this as completed Sep 5, 2016
@mdo mdo modified the milestone: v3.3.8 Sep 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants