javascript - Material design lite,检查滚动是否到达底部

标签 javascript jquery angularjs scroll material-design-lite

我正在使用 Material Design lite 和 Angular.js。当事件到达底部时无法调用事件。 我几乎尝试了所有解决方案,但没有奏效。 像 jQuery 解决方案:

$(window).scroll(function() {
    if($(window).scrollTop() + $(window).height() ==      
        $(document).height())        
    {
        alert("bottom!");
    }
});

或者 Javascript 之一:

document.addEventListener('scroll', function (event) {
    if (document.body.scrollHeight == 
        document.body.scrollTop + window.innerHeight) {
        alert("Bottom!");
    }
});

没有任何效果。 根据这个问题:Material design and jQuery, scroll not working

Scroll 事件将在 .mdl-layout__content 类上触发,但仍然无法获取是否到达底部的事件。

而且我可能不想在“mdl-layout__content”类上绑定(bind)偶数,因为这将包含在每个页面上。 我只想要一个特定页面。

已编辑: 此代码工作正常但有问题:

function getDocHeight() {
     var D = document;
     return Math.max(
            document.getElementById("porduct-page-wrapper").scrollHeight,
            document.getElementById("porduct-page-wrapper").offsetHeight,
            document.getElementById("porduct-page-wrapper").clientHeight
        );
}
window.addEventListener('scroll', function(){
    if($('.mdl-layout__content').scrollTop() + $('.mdl-layout__content').height() == getDocHeight()) {
           alert("bottom!");
    }
}, true);

问题是,每次我更改页面并返回时,事件应调用的次数都会成倍增加。每当我更改页面或单击链接时,它可能一次又一次地绑定(bind)事件。 我正在使用 Angular.js,如何防止这种情况发生?

最佳答案

好吧,我认为您可以通过在当前滚动事件回调中准确检测主体位置来解决您的问题。使用 getBoundingClientRect html 元素 的方法:

document.addEventListener('scroll', function (event) {
  var bodyRect = document.getElementsByTagName('body')[0].getBoundingClientRect(); 
  if (bodyRect.bottom - window.innerHeight <= 20){
    // less then 20px rest to get the bottom
    alert("Bottom!");
  }
});

关于javascript - Material design lite,检查滚动是否到达底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33098807/

相关文章:

javascript - Javascript 正则表达式中的 "AND"...我缺少什么?

javascript - "interactive"的 document.readystate 与 ondomcontentloaded?

javascript - Angular http json 请求问题

javascript - 从 JS 确定 Web 浏览器选择/焦点颜色?

javascript - Angular 2.0 迁移路径

javascript - 使用 foreach knockout 渲染复选框

javascript - 如何将 javascript 中的数据返回到 html 表单中?

javascript - 将 CKEditor 集成到 elFinder

javascript - 通过 Controller 设置默认顺序

angularjs - 如何防止 AngularJS 路由尝试使用我的服务器端 SSO 重新进行身份验证?