2016-09-15 37 views
0

我想從文檔的頂部記錄窗口的偏移量,但jQuery的滾動不起作用。將香草javascript的滾動事件監聽器工作在角?將香草javascript的滾動事件工作在angularjs

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

,你能否告訴我們你已經完成了什麼以及控制器/指令在哪裏等你正在實現這個? jQuery應該很好地檢測滾動,尤其是在AngularJS(不是Angular 2) – JosephGarrone

+0

爲什麼你使用'restrict:C'?你是否認真地使用'owlCarouselItem'作爲評論? –

回答

1

使用angular.element($窗口)試試這個代碼:

.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> 
+0

我改進了你的代碼,刪除了冗餘並添加了$ scope。事件監聽器不是角度摘要循環的一部分。你應該向OP解釋他們如何使用'canSwipeDown'。 –

+0

非常感謝不錯的工作 – SuperComupter