javascript - 普通的 JavaScript 滚动事件可以在 AngularJS 上工作吗

标签 javascript angularjs

我想记录窗口距文档顶部的偏移量,但 jquery 滚动不起作用。普通的 JavaScript 滚动事件监听器可以在 Angular 中工作吗?

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

最佳答案

使用 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>

关于javascript - 普通的 JavaScript 滚动事件可以在 AngularJS 上工作吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504405/

相关文章:

javascript - 是否可以在 AngularJS 中调用函数并显示值

javascript - 仅使用 HTML/CSS 固定导航栏

javascript - HTML格式的YouTube视频不起作用

javascript - Angular Jasmine SpyOn $resource 处理错误

javascript - 如何在 Angular Controller 中使用 Browserified npm 包?

AngularJS - 在 Controller 中使用多个过滤器

javascript - 如何以及何时可以使用 &lt 或 &gt?

javascript - 如何从 HTML 页面中的 3 个输入范围正确获取 RGB 值

javascript - 如何从 li 获取 id?

javascript - Angular 谷歌地图主 Controller 错误