0

this code is working in Firefox but not in Chrome v.23. When I trace the code in debugger everything works fine but Chrome doesn't change class. Any ideas?

$(document).scroll(function () {

    $('a.nav').each(function () {

        var divTop = $('#' + this.title).offset().top;
        var distance = divTop - $("html").scrollTop();
        if (distance > 210 && distance < 255) {

            $('a.active').removeClass('active');
            $(this).addClass('active');

        }
    });
});
Javad M. Amiri
  • 594
  • 5
  • 20
  • But addClass() and removeClass() work fine for me in chrome – Dineshkani Dec 04 '12 at 13:35
  • can you recreate the problem and share the code at http://jsfiddle.net/ – Muthu Kumaran Dec 04 '12 at 13:37
  • Yikes that code is going to be a performance problem. You should probably look at throttling the scroll. And if you finds a match for active, you should exit out of the each loop. And you should not keep looking up the html's scrolltop, do that outside the each. – epascarello Dec 04 '12 at 13:41

1 Answers1

0

The problem is targeting $("html") for scrolltop.

See this SO thread: jQuery scrollTop() doesn't seem to work in Safari or Chrome (Windows)

See the second answer for details on what's actually going on with your scenario, i.e. try using $('body')

Either of the first two solutions should work for you, though.

Community
  • 1
  • 1
Eli Gassert
  • 9,745
  • 3
  • 30
  • 39