I have the following code, though I'm not sure it is efficient as it could be.
$(window).scroll(function() {
var scrollT = $(document).scrollTop();
if (scrollT >= 180) {
$("#primary-nav-wrapper").addClass("scroll");
$("#primary-nav-wrapper li.front").addClass("active");
$("#primary-nav-wrapper .search-wrapper").removeClass("active");
$("#primary-nav-wrapper .search-field").blur();
}
else {
$("#primary-nav-wrapper").removeClass("scroll");
$("#primary-nav-wrapper .search-wrapper, #primary-nav-wrapper li.front").removeClass("active");
$("#primary-nav-wrapper .search-field").blur();
}
if (scrollT >= 400) {$("a#to-top-link").addClass("active");}
else {$("a#to-top-link").removeClass("active");}
});
Basically, what I do here is checking two if-clauses every time I scroll, but is it more resource-friendly to only check every few milliseconds? If so, how is this done? Or is it a better idea to re-write the if-else structure, e.g. if.. else if... else if... else?
active
class accomplish? Could you make a live Stack Snippet example including some HTML and CSS? \$\endgroup\$