Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Fixed navigation bar – add active class to link items based on scroll

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #44094
    jshjohnson
    Member

    Hey,

    I would like to achieve something like this:

    http://jsfiddle.net/gUWdJ/3/ for this website i’m working on: http://oandg.co.uk.s156312.gridserver.com/

    I have managed to get it partially working but for the “home” section it still highlights ‘About Us’

    $(‘nav a’).on(‘click’, function() {

    var scrollAnchor = $(this).attr(‘data-scroll’),
    scrollPoint = $(‘section[data-anchor=”‘ + scrollAnchor + ‘”]’).offset().top – 28;

    return false;

    })

    $(window).scroll(function() {
    var windscroll = $(window).scrollTop();
    if (windscroll >= 1000) {
    $(‘body section’).each(function(i) {
    if ($(this).position().top <= windscroll - 1000) {
    $(‘nav a.active’).removeClass(‘active’);
    $(‘nav a’).eq(i).addClass(‘active’);
    }
    });

    } else {
    $(‘nav a.active’).removeClass(‘active’);
    $(‘nav a:first’).addClass(‘active’);
    }

    }).scroll();

Viewing 1 post (of 1 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.