最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS call function when a div is scrolled within (x) pixels from bottom of screen - Stack Overflow

programmeradmin4浏览0评论

I'm trying to work with this AngularJS infinite-scroll directive. Here's the source:

angular.module('infiniteScroll', [])
    .directive('infiniteScroll', [ "$window", function ($window) {
        return {
            link:function (scope, element, attrs) {
                var offset = parseInt(attrs.threshold) || 0;
                var e = element[0];

                element.bind('scroll', function () {
                    if (scope.$eval(attrs.canLoad) && e.scrollTop + e.offsetHeight >= e.scrollHeight - offset) {
                        scope.$apply(attrs.infiniteScroll);
                    }
                });
            }
        };
    }]);

This appears to work only for elements which are contained in a scrollbox. Here's a Plunker which demonstrates this.

However, I'd like to be able to apply this simple directive to DOM elements which are static, calling scope.$apply(attrs.infiniteScroll) when the element nears the bottom of the window's scroll.

Any ideas on how to modify the directive to allow for this? I've tried changing the e.scrollTop to window.scrollTop but to no avail, this is a little beyond my current skill level unfortunately.

I'm trying to work with this AngularJS infinite-scroll directive. Here's the source:

angular.module('infiniteScroll', [])
    .directive('infiniteScroll', [ "$window", function ($window) {
        return {
            link:function (scope, element, attrs) {
                var offset = parseInt(attrs.threshold) || 0;
                var e = element[0];

                element.bind('scroll', function () {
                    if (scope.$eval(attrs.canLoad) && e.scrollTop + e.offsetHeight >= e.scrollHeight - offset) {
                        scope.$apply(attrs.infiniteScroll);
                    }
                });
            }
        };
    }]);

This appears to work only for elements which are contained in a scrollbox. Here's a Plunker which demonstrates this.

However, I'd like to be able to apply this simple directive to DOM elements which are static, calling scope.$apply(attrs.infiniteScroll) when the element nears the bottom of the window's scroll.

Any ideas on how to modify the directive to allow for this? I've tried changing the e.scrollTop to window.scrollTop but to no avail, this is a little beyond my current skill level unfortunately.

Share Improve this question asked Dec 8, 2013 at 12:07 JVGJVG 21.2k48 gold badges141 silver badges216 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

I'm not pletely sure that I got your question right. If you want to have a directive which calls a function, if a certain DOM element es close to the visible area of the whole window, how about a directive like this:

myApp.directive('scrollTrigger', function($window) {
    return {
        link : function(scope, element, attrs) {
            var offset = parseInt(attrs.threshold) || 0;
            var e = jQuery(element[0]);
            var doc = jQuery(document);
            angular.element(document).bind('scroll', function() {
                if (doc.scrollTop() + $window.innerHeight + offset > e.offset().top) {
                    scope.$apply(attrs.scrollTrigger);
                }
            });
        }
    };
});

With this element somewhere below the visible area in your HTML:

<div scroll-trigger="scrollEventCallback()" threshold="100">Trigger element</div>

The function scrollEventCallback() gets called, if it is 100 pixel or less below the currently visible area of the document. Please note, that I used jQuery in this example, so for this to work you will have to include it as well. I hope this is what you asked for.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论