문제

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().top causes 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top