Test in JQuery if an element is at the top of screen

后端 未结 5 1441
予麋鹿
予麋鹿 2020-12-23 11:42

I have a div that is positioned about 100px from the top of the browser window. When the user scrolls down, I want the div to stay where it is

5条回答
  •  失恋的感觉
    2020-12-23 12:06

    hey you can do like this :

    var distance = $('.yourclass').offset().top;
    
    $(window).scroll(function() {
        if ( $(this).scrollTop() >= distance ) {
            console.log('is in top');
        } else {
            console.log('is not in top');
        }
    });

提交回复
热议问题