【问题标题】:Angular: how to keep resetting $timeout on scroll events, and let $timeout finish otherwiseAngular:如何在滚动事件上继续重置 $timeout,否则让 $timeout 完成
【发布时间】:2017-08-23 00:05:06
【问题描述】:

我正在开发一个小控制器,它监视滚动事件并应用 CSS 类,并应用不同的 CSS 类。长话短说,我试图让滚动条拇指在您不滚动时消失,并在您滚动时出现(就像 iPhone 上的滚动条拇指一样)。

我在实现它时遇到了麻烦。我这样做的想法是:

1) On page load, set a $scope variable to false. 
2) Watch for a scroll event on the div I want.
3) Once the scroll event starts, set the $scope variable to true. 
4) Keep on resetting the $timeout whenever a scroll event fires.
5) In the timeout function, set the $scope variable back to false if the $timeout finishes.
6) In the HTML, set an ng-class to watch for this $scope variable.

我认为这听起来很简单,但是我在实现它时遇到了很多麻烦,我不确定我是否只是缺少关于 $timeout 的东西,或者我只是在思考圈子和还没有意识到。

这是我为它设置的控制器(实际工作的 JSFiddle 链接在这堵代码墙的下方):

(function () {
    'use strict';
    angular
        .module('scrollingApp')
        .controller('scrollbarController', scrollbarController);

    function scrollbarController($scope, $timeout) {
        $scope.actuallyScrolling = false;

        $scope.scrolling = function() {
            $('.container').on('scroll', function(event) {
                $scope.actuallyScrolling = true;
                console.log('before checkScroll ', $scope.actuallyScrolling);
                checkScroll();
            });
        };

        var checkScroll = function() {
            var timeoutEnded = false;
            $timeout(function() {
                if($scope.actuallyScrolling) {
                    $scope.actuallyScrolling = false;
                    console.log('inside $timeout if statement', $scope.actuallyScrolling);
                }
            }, 1000);
            console.log($scope.actuallyScrolling);
        };
        $scope.scrolling();
    }
})();

我在这里 (https://jsfiddle.net/hurgledurf/k5naeora/) 使用我目前拥有的代码设置了一个 JSFiddle(希望它是不言自明的),并且希望任何人都可以提供任何帮助/见解。谢谢!

【问题讨论】:

    标签: javascript jquery html css angularjs


    【解决方案1】:

    Angular of not...要“重置”滚动超时应该这样完成:

    var timer;
    $('.container').on('scroll', function(event) {
      console.log('User is actually scrolling...');
    
      clearTimeout(timer);
      timer = setTimeout(function(){
        console.log('User finished scrolling.');
      },500);
    
    });
    

    它替换了你的这段代码:

    $('.container').on('scroll', function(event) {
      $scope.actuallyScrolling = true;
      console.log('before checkScroll ', $scope.actuallyScrolling);
      checkScroll();
    });
    

    【讨论】:

    • 这行得通。谢谢你。不过我很好奇,为什么我要做的就是声明一个名为 timer 的变量,然后在其上运行 clearTimeout?为什么我不需要在运行 clearTimeout 之前将计时器设置为 setTimeout 函数?
    • clearTimeout() 被调用时... 如果传递的参数是setTimeout(),它会被清除。 如果不是,就像在第一次发生时一样,无论如何都不会发生任何事情。它不会引发错误。然后将变量定义为setTimeout 倒计时 500 毫秒...但在它可以执行其回调之前,发生另一个滚动事件并且计时器被清除。新的 500 毫秒倒计时开始……以此类推……直到至少 500 毫秒没有滚动事件发生……这就是回调函数最终执行的时间。
    • 啊,有道理。非常感谢。
    • 主体是好的......但是当你使用角度时,“角度与否”并不是一个好话,因为你正在使用 jquery,并且很多新手会将 jquery 放入他们的角度代码中- 基本代码也是废话(其中有 jquery ffs)..所以我理解你为什么说它足够公平
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2022-12-05
    • 2016-06-28
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多