check if element got scrolled to top

后端 未结 1 922
南笙
南笙 2020-12-24 15:16

I want to check if an element is scrolled to top with an offset of ~ 100px.

I´ve got a page with 5 subcontents and 2 classes to make backgrounds. It looks like this:

相关标签:
1条回答
  • 2020-12-24 15:32

    You have to listen for the scroll event, then check each element against the currently scrolled distance, something like :

    $(window).on('scroll', function() {
        var scrollTop = $(this).scrollTop();
    
        $('.background1, .background2').each(function() {
            var topDistance = $(this).offset().top;
    
            if ( (topDistance+100) < scrollTop ) {
                alert( $(this).text() + ' was scrolled to the top' );
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题