will vanilla javascript会在angularjs上滚动事件

时间:2016-09-15 06:32:09

标签: javascript angularjs

我想从文档顶部记录窗口的偏移量但是jquery滚动不起作用。将vanilla javascript滚动事件监听器以角度工作吗?

app.directive('owlCarouselItem', function($touch, $timeout, $rootScope, $window){
    return {
        restrict: 'C',
        transclude: false,
        link: function(scope, element) {
            // this is the part of my directive the on scroll event is not firing
                $('html, body').on('scroll', function() {
                    if($(this).scrollTop() == 0){
                        console.log($(this).scrollTop());
                        canSwipeDown = true;
                    }else{
                        console.log($(this).scrollTop());
                        canSwipeDown = false;
                    }
                });

1 个答案:

答案 0 :(得分:1)

使用angular.element($ window)尝试此代码:

.directive('scrollDir', function($window) {
    return {
        restrict: 'EAC',
        link: function(scope, attrs, element) {
            var canSwipeDown = false
            // this is the part of my directive the on scroll event is not firing
            angular.element($window).on('scroll', function() {
                canSwipeDown = element.scrollTop() === 0
                scope.$apply()
            });
        }
    };
});

你可以将指令粘贴到body标签上 像这样 HTML:

<body scroll-dir>