Here's what I'm trying to achieve in jQuery: I've multiple hero sections displayed one after the other in my HTML. Each time a hero section reaches the top of the window I want to add class 'link', if not remove that class.
Each hero section has class 'parallax' and as ID 'parallax-1', 'parallax-2', 'parallax-3', etc.
<section class="parallax" id="parallax-1"></section>
<section class="parallax" id="parallax-2"></section>
<section class="parallax" id="parallax-3"></section>
...
JS code:
var target = $('.parallax').attr('id'),
$window = $(window);
$window.on('load resize scroll',function(){
var $div = $('#parallax'+target);
if ( $window.scrollTop() >= $div.offset().top ) {
$div.addClass('link');
} else {
$div.removeClass('link');
}
});
So the code above doesn't work and this is the error in browser console:
undefined is not an object (evaluating '$div.offset().top')
I don't understand what is wrong exactly because each ID exists. Thank you for your help!