I want to add some class for specify element when i’m scrolling my page/window, so i use offset for this but it’s not warking – what is wrong? Much thx for help. This is my code:
$(window).scroll(function() {
var top_offset = $('body').offset().top;
if ((top_offset >= 40)) {
$('nav').addClass('docked_nav');
}
});
You have already fetched the offest value in the variable
top_offset, it contains numeric value now and is not an object.So either use
Or dont use any variable
Arun Killu’s answer is also need to be considered