I\'m working on a page with a fixed menu that picks up after the user has scrolled a certain distance from the top, and as they scroll down the page, different links from th
Basically you are executing too much in the scroll event. As @undefined said, the browser calls this a lot more than you might think. Some tips:
When inside a function that will be called many times, have your jQuery objects already created. That way, each time the function is called, it's not recreating the same jQuery object.
var nav2 = $("#nav_2");
$(window).scroll(function() {
...
nav2.removeClass('active');
...
});
Similarly, when called over and over again, adding and removing classes can consume a lot of your processing cycles - Especially when you are accessing a whole class of elements, like in $('.nav').addClass("f-nav");
.
Instead, try to add/remove the classes only if they don't exist. Something like:
var alreadyAdded = false;
var alreadyRemoved = false;
$(window).scroll(function () {
if ($(this).scrollTop() > 259) {
if(!alreadyAdded){
$('.nav').addClass("f-nav");
alreadyAdded = true;
alreadyRemoved = false;
}
} else if(!alreadyRemoved) {
$('.nav').removeClass("f-nav");
alreadyAdded = false;
alreadyRemoved = true;
}
});
All that said, it will still probably scroll slowly with all this code attached to the scroll event. Maybe there is another way you can get the same effect. If firefox is freezing, I can only imagine how slow this must be in IE.