I have several elements under each other. I want to check when these elements get to the top of the window. I know how to do this with a single element, but not with multiple elements.
Single item:
$(function(){ var itemOffset = $('.test').offset().top; $(window).scroll(function(){ var scrollTop = $(window).scrollTop(); if(scrollTop >= itemOffset){ console.log("item has reached the top"); } });});
But now I have 5 elements with the class 'test'. And if the .test element reaches the top, I want to say: div 1 reached the top; div 2 reached the top; etc. So he should see which div it is (maybe get the attr id or something else?)
This is what I have had so far.
$(function(){ $('.test').each(function(){ var itemOffset = $(this).offset().top; $(window).scroll(function(){ var scrollTop = $(window).scrollTop(); var toet = itemOffset - scrollTop; if(scrollTop >= itemOffset){console.log("New div has reached the top!")} }); });});
Help is greatly appreciated!
source share