Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:03:56+00:00 2026-05-27T19:03:56+00:00

Some very helpful folks wrote and then modified a script to change the class

  • 0

Some very helpful folks wrote and then modified a script to change the class on a nav item when scrolling the page to a related section. Here’s the original post: Use jQuery to change a class dependent on scroll position.

My first issue is that the last section of the array is being ignored. Someone else had this question and a solution was offered, which worked for them, but not for me: jQuery class change based on scroll ignoring last section.

The second issue is that even setting aside that the last section in my array is not ever getting selected, the new class name is only getting added to my nav items, but never removed, so that by the time I scroll to the bottom of the page, every item except the last in my nav has “selected” as a class. I’d really appreciate any insight. Here’s my code, which is virtually identical to the modified version, except I’m using headings rather than sections (which is maybe significant?):

var $anchs = $('.content h1');
    var $navs = $('#zone-submenu div .content > .item-list > ul > li');

    topsArray = $anchs.map(function(){
        return $(this).position().top - 100;
     }).get(),
     topsArray.push(window.height);

    var len = topsArray.length;
    var currentIndex = 0; 

    var getCurrent = function( top ) {   // take the current top position, and see which
        for( var i = 0; i < len; i++ ) {   // index should be displayed
            if( top > topsArray[i] && topsArray[i+1] && top < topsArray[i+1] ) {
                return i;
            }
        }
    };
    $(document).scroll(function(e) {
        var scrollTop = $(this).scrollTop();
        var checkIndex = getCurrent( scrollTop );
        if( checkIndex !== currentIndex ) {
            currentIndex = checkIndex;
            $navs.eq( currentIndex ).addClass("selected").siblings(".selected").removeClass(".selected");
        }
    });

I’m afraid I can’t post the site itself – it’s in development and I can’t reveal it before it goes live. Thanks for any help, though!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:03:57+00:00Added an answer on May 27, 2026 at 7:03 pm

    This looks a bit odd:

    currentIndex = checkIndex;
    $navs.eq( currentIndex ).addClass("selected").siblings(".selected").removeClass(".selected");
    

    From what I can tell in your code, it should be:

    $navs.eq( currentIndex ).removeClass('selected');  // no dot
    currentIndex = checkIndex;
    $navs.eq( currentIndex ).addClass("selected");
    

    UPDATED
    Fixing the “not displaying the last index item” …

    You are referencing an invalid array element in your getCurrent function – [i+1] is invalid when i == len-1. You need to just return the last element if that happens.

    var getCurrent = function( top ) {   // take the current top position, and see which
        for( var i = 0; i < len-1; i++ ) {   // index should be displayed
            if( top > topsArray[i] && topsArray[i+1] && top < topsArray[i+1] ) {
                return i;
            }
        }
        return len-1;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After receving some help from another very helpful member I'm stuck at another point
EDIT (2011-07-23) Have gotten some very helpful answers, both of which I've tried implementing.
I'm trying to do some very simple page redirection on a shared server that
The title is the question. Will be very helpful if some one can also
From the very helpful templates I´ve got some controls. But how do I discover
I have looked for some info on this and haven't found anything very helpful.
Thanks to some very helpful stackOverflow users at Bit twiddling: which bit is set?
We have some very large data files (5 gig to 1TB) where we need
Say I need some very special multiplication operator. It may be implemented in following
I have some very simple code to generate an assembly and invoke a method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.