Scroll event in AngularJS

后端 未结 3 1945
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 07:34

I have a div with a scrollbar in it. Now I want to get an event, that triggers every time, the user scrolls.

Is that possible in AngularJS, or do I have to use jQue

相关标签:
3条回答
  • 2021-01-05 08:24

    Sergey's answer helped me a little, but what ended up working for me was this:

    .directive("scroll", function ($window) {
       return {
          link: function() {
             angular.element($window).bind("wheel", function() {
                console.log('Scrolling');
             });
          }
       }
    })
    
    0 讨论(0)
  • 2021-01-05 08:25

    You would be using jquery for adding the event listener, and maybe inside an angularjs directive to attach it to an element.

    page.html:

    <div my-scroller>
    

    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);
                });
            }
    
        }
    
    });
    

    Edit: of course you don't even need to use jquery. Angular's jqLite suffices for this, you would just call element without the jquery wrapping:

    elem.on('scroll', ...
    
    0 讨论(0)
  • 2021-01-05 08:29

    Solution for Angular 1.6:

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

    })

    Use "wheel" instead of "scroll". It takes me few hours to find.

    0 讨论(0)
提交回复
热议问题