简体   繁体   English

材料设计精简版,检查滚动是否到达底部

[英]Material design lite, Check if scroll reached to the bottom

I am Using Material Design lite with Angular.js. 我正在使用Material Design lite和Angular.js。 Having the problem to get the event called when the event reach to bottom. 在事件到达底部时遇到问题以调用事件。 I have tried almost every solution but not working. 我几乎尝试了所有解决方案但没有工作。 Like jQuery solution : 像jQuery解决方案:

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

Or Javascript one: 或Javascript一:

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

Nothings working. 没有工作。 According to this question : Material design and jQuery, scroll not working 根据这个问题: 材质设计和jQuery,滚动不起作用

Scroll event will fire on .mdl-layout__content class, but still unable to get the event for bottom reached or not. 滚动事件将在.mdl-layout__content类上触发,但仍无法获取底部到达的事件。

And probably I dont want to bind the even on "mdl-layout__content" class, since this will be included on every page. 也许我不想在“mdl-layout__content”类上绑定偶数,因为这将包含在每个页面上。 I just want this for one particular page. 我只想把它放在一个特定的页面上。

EDITED: This code is working fine but has problem: 编辑:此代码工作正常,但有问题:

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);

Problem is, every time I change the page, and comes back, the number of times the event should called is multiplying. 问题是,每次我更改页面并返回时,事件应该调用的次数是相乘的。 Probably it is binding event again and again, whenever I change the page or clicks link. 每当我更改页面或点击链接时,它可能会一次又一次地发生绑定事件。 I am using Angular.js, how can I prevent this? 我正在使用Angular.js,我该如何防止这种情况?

Well, I think you can fix your issue with the exactly detecting of the body position in the current scroll event callback. 好吧,我认为你可以通过准确检测当前滚动事件回调中的正文位置来解决你的问题。 Use the getBoundingClientRect method of an html element : 使用html elementgetBoundingClientRect方法:

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!");
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM