I want to do something when two elements have the same distance to the browsers top.
I’ve tried this:
var scrollTop = $(window).scrollTop(),
_menuOffset = $('#2').offset().top,
_aboutOffset = $('#1').offset().top;
distance1 = (_menuOffset - scrollTop);
distance2 = (_aboutOffset - scrollTop);
if (distance1 == distance2 ) {
alert('test');
}
fiddle: http://jsfiddle.net/Rxs2m/135/
Hope someone can help.
Thanks!
http://jsfiddle.net/Rxs2m/135/
The IDs are incorrect, so
.offset()returns nothing (and.offset().topcauses an error).Change to
$("#en")and$("#to").Finally, wrap everything in a callback to
$(window).scroll()and it is checked each time the window scrolls.