我正在使用Angular.js和Material Design lite。在滚动到底部时,无法触发事件。 我尝试了几乎所有的解决方案,但都没有起作用。 例如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, 滚动事件将在.mdl-layout__content类上触发,但仍然无法获取底部是否到达的事件。 而且可能我不想绑定"mdl-layout__content"类的事件,因为这将包含在每个页面中。 我只想要一个特定页面的事件。 编辑: 这段代码工作得很好,但存在问题:
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);
问题是,每次我改变页面并返回时,应该调用事件的次数都在增加。可能是每次我改变页面或单击链接时都会绑定事件。 我正在使用Angular.js,如何防止这种情况发生?
mdl-layout__content
是否具有滚动条,或者您希望在窗口滚动时出现滚动条? - Jai