代码之家  ›  专栏  ›  技术社区  ›  tcooc

检查靠近/在页面底部

  •  2
  • tcooc  · 技术社区  · 16 年前

    我需要检查一个元素是否距离页面底部x像素,以便动态加载新内容。当前,即使滚动条位于底部,滚动条的顶部和高度也不匹配。

    jquery是允许的,不过基本的javascript会更有帮助。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Glorfindel Doug L.    7 年前

    您可能需要尝试以下操作(仅在Firefox 3.5和IE 8中测试):

    function pollScrollPosition() {
       var y = (document.all) ? document.body.scrollTop : window.pageYOffset;
       var max = window.scrollMaxY || 
                 (document.body.scrollHeight - document.body.clientHeight);
    
       if ((max - y) < 100) {
          console.log('Within the bottom 100 pixels. Do Something!');
       }
    }
    
    // Check the scroll position every 250ms
    setInterval(pollScrollPosition, 250);
    

    Checking is near/at the bottom of the page