Angularjs:当滚动到达滚动条的底部时,如何触发事件? [英] Angularjs: How to trigger event when scroll reaches to the bottom of the scroll bar in div?

查看:638
本文介绍了Angularjs:当滚动到达滚动条的底部时,如何触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当滚动条到达结束时,我试图触发事件。我发现这个这个例子。这是我的代码问题是它根本不调用loadmore()。
控制台状态的值为:

I'm trying to trigger an event when scroll bar reaches the end. I found this this example. Here is my code. The problem is that it doesn't call loadmore() at all. The values of the console statments are:

848
899
in scroll
881
899
in scroll
892
899
in scroll
897
899
in scroll
900
899

似乎从来没有去过if语句!奇怪的是,如果我在inspect元素中调试它,那么它会触发事件。


.....

我的指令:

It seems that it never goes to that if statement! The weird part is that if I debug it in inspect element then it triggers the event. ..... my directive:

directive('scrolly', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var raw = element[0];
            console.log('loading directive');
            element.bind('scroll', function () {
                console.log('in scroll');
                console.log(raw.scrollTop + raw.offsetHeight);
                console.log(raw.scrollHeight);
                if (raw.scrollTop + raw.offsetHeight == raw.scrollHeight) { //at the bottom
                    scope.$apply(attrs.scrolly);
                }
            })
        }
    }


推荐答案

而不是检查平等,请检查if语句中左侧是否大于右侧因为那是sc的情况滚动条位于底部。

Instead of checking for equality, please check if the left side is greater than the right side in the if statement since that's the case for the scrollbar to be at the bottom.

raw.scrollTop + raw.offsetHeight > raw.scrollHeight

这里是工作的 jsfiddle

这篇关于Angularjs:当滚动到达滚动条的底部时,如何触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆