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 8130559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:45:37+00:00 2026-06-06T08:45:37+00:00

I need help with selecting a li element when a user scrolls to a

  • 0

I need help with selecting a li element when a user scrolls to a particular section on a page:

See here for my progress: http://jsfiddle.net/FvBqA/349/

If you click on the “services” link the text color should change to red as you are on that section of the page, same again if you were to click on “home” or “contact”.

I have an idea regarding the jquery is that you’d need to be able to add and remove classes to the LI so that the css can pick up the selected class and render as per.

The functionality can be viewed here: http://demicreative.com/#capabilities

If you scroll down and see the work section you will notice the nav item is selected – I am after the same functionality.

  • 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-06-06T08:45:39+00:00Added an answer on June 6, 2026 at 8:45 am

    Try this:

    var header_height = $('#nav').outerHeight();
    var sections = [];
    $('#nav a').each(function(){    
        var section = $(this.hash).offset()
        sections.push({
            'link':$(this).parent(),
            'top' : $(this.hash).offset().top-header_height,
            'bottom' : $(this.hash).offset().top + $(this.hash).outerHeight() -header_height
        });
    });
    
    $('#nav a').click(function(e) {
        e.preventDefault();
        $('html,body').animate({
            scrollTop: $(this.hash).offset().top - header_height
        }, 500);
    });
    
    
    $(window).scroll(function(){   
        for(var i = 0; i < sections.length; i++)
            if($(window).scrollTop() >= sections[i].top &&
               $(window).scrollTop() <= sections[i].bottom){
                sections[i].link.addClass('selected')
                    .siblings().removeClass('selected');
            }
    });​
    

    I attached the event directly to links in the #nav. I add the selected class to the parent element and remove the it from the parent element’s siblings. I also make the scroll offset 70 px less, the height of the nav and it’s padding.

    Updated: There is no reference to any particular section only sections this means if you expand, adding new links or sections it will still work without having to updated the JavaScript.

    Fiddle:
    http://jsfiddle.net/iambriansreed/5Ta8j/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help to create regular expression matching string www.*.abc.*/somestring Here * is wild card
I need some help to be able to add element to a JList and
Need help with this error. Can't see what's wrong. Error: Warning: mysql_query() expects parameter
I need some help with selecting a cell in my gridview. I have a
I have a jsFiddle set up for this: http://jsfiddle.net/eventide/TAmam/ I'm trying to apply the
I need help selecting or creating a clustering algorithm according to certain criteria. Imagine
Here is the scenario where I need help I ve got a report -
Need help writing a script downloads data from google insight using c# this is
need help/guide for sql select query, I have 2 table stock and stock_history, in
need help regarding USSD Gateway. I have to develop an app, which will directly

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.