AngularJS中的滚动事件

Tre*_*eam 7 javascript angularjs

我有一个带有滚动条的div.现在我希望得到一个事件,每次触发,用户滚动.

这是可能的AngularJS,还是我必须使用jQuery?

编辑: 到目前为止我想出了以下内容:

// JS
.directive('scroll', function() {
    return function(scope, element, attrs){

        angular.element(element).bind("scroll", function(){
            console.log(1);
        });
    };
});

// HTML
<div class="wrapper" style="height: 1550px" scroll>
[...]
</div>
Run Code Online (Sandbox Code Playgroud)

但这不起作用(我在Firebug-Console中看不到任何日志).

Ser*_* NN 15

Angular 1.6的解决方案:

.directive("scroll", function () {
return {
  link: function(scope, element, attrs) {
      element.bind("wheel", function() {
         console.log('Scrolled below header.');
      });
  }
}
Run Code Online (Sandbox Code Playgroud)

})

使用"滚轮"而不是"滚动".我需要几个小时才能找到.


fra*_*ies 4

您将使用 jquery 添加事件侦听器,并且可能在 angularjs 指令中将其附加到元素。

页面.html:

<div my-scroller>
Run Code Online (Sandbox Code Playgroud)

myscroller.js:

app.directive('myScroller', function(){

    return {

        restrict: 'A',
        link: function(scope,elem,attrs){
            $(elem).on('scroll', function(evt){
               console.log(evt.offsetX + ':' + evt.offsetY);
            });
        }

    }

});
Run Code Online (Sandbox Code Playgroud)

编辑:当然你甚至不需要使用jquery。Angular 的 jqLit​​e 足以满足这一点,您只需调用 element 而无需 jquery 包装:

elem.on('scroll', ...
Run Code Online (Sandbox Code Playgroud)