scrollTop检查用户是否滚动到某个元素

scrollTop to check whether user has scroll to certain element

本文关键字:元素 滚动 检查 用户 是否 scrollTop      更新时间:2023-09-26

我想当用户到达说div#btm,它会淡出。我知道怎么做css部分,只需要添加过渡和不透明度0。但是如何使用scrollTop来检查用户是否滚动到那个元素呢?

我不想使用outerHeight,因为我的元素不是在窗口的底部

遵循以下代码片段

有一些属性/方法可以使用:

$().scrollTop()//how much has been scrolled
$().innerHeight()// inner height of the element
DOMElement.scrollHeight//height of the content of the element

所以你可以取前两个属性的和,当它等于最后一个属性时,你就到达了终点:

jQuery(function($) {
    $('#flux').bind('scroll', function() {
        if($(this).scrollTop() + $(this).innerHeight() >= this.scrollHeight) {
            alert('end reached');
        }
    })
});

$(document).ready(function() {
                   $('#btm').bind('scroll', function() 
                  {
                  if($(this).scrollTop() + $(this).innerHeight() >= this.scrollHeight) 
                      {
                         alert('end reached');
                      }
                  });
                 });