Disabling android's chrome pull-down-to-refresh feature

前端 未结 15 2574
予麋鹿
予麋鹿 2020-12-02 04:20

I\'ve created a small HTML5 web application for my company.

This application displays a list of items and everything works fine.

The application is mainly us

15条回答
  •  天命终不由人
    2020-12-02 04:41

    AngularJS

    I have successfully disabled it with this AngularJS directive:

    //Prevents "pull to reload" behaviour in Chrome. Assign to child scrollable elements.
    angular.module('hereApp.directive').directive('noPullToReload', function() {
        'use strict';
    
        return {
            link: function(scope, element) {
                var initialY = null,
                    previousY = null,
                    bindScrollEvent = function(e){
                        previousY = initialY = e.touches[0].clientY;
    
                        // Pull to reload won't be activated if the element is not initially at scrollTop === 0
                        if(element[0].scrollTop <= 0){
                            element.on("touchmove", blockScroll);
                        }
                    },
                    blockScroll = function(e){
                        if(previousY && previousY < e.touches[0].clientY){ //Scrolling up
                            e.preventDefault();
                        }
                        else if(initialY >= e.touches[0].clientY){ //Scrolling down
                            //As soon as you scroll down, there is no risk of pulling to reload
                            element.off("touchmove", blockScroll);
                        }
                        previousY = e.touches[0].clientY;
                    },
                    unbindScrollEvent = function(e){
                        element.off("touchmove", blockScroll);
                    };
                element.on("touchstart", bindScrollEvent);
                element.on("touchend", unbindScrollEvent);
            }
        };
    });
    

    It's safe to stop watching as soon as the user scrolls down, as the pull to refresh has no chance of being triggered.

    Likewise, if scrolltop > 0, the event won't be triggered. In my implementation, I bind the touchmove event on touchstart, only if scrollTop <= 0. I unbind it as soon as the user scrolls down (initialY >= e.touches[0].clientY). If the user scrolls up (previousY < e.touches[0].clientY), then I call preventDefault().

    This saves us from watching scroll events needlessly, yet blocks overscrolling.

    jQuery

    If you are using jQuery, this is the untested equivalent. element is a jQuery element:

    var initialY = null,
        previousY = null,
        bindScrollEvent = function(e){
            previousY = initialY = e.touches[0].clientY;
    
            // Pull to reload won't be activated if the element is not initially at scrollTop === 0
            if(element[0].scrollTop <= 0){
                element.on("touchmove", blockScroll);
            }
        },
        blockScroll = function(e){
            if(previousY && previousY < e.touches[0].clientY){ //Scrolling up
                e.preventDefault();
            }
            else if(initialY >= e.touches[0].clientY){ //Scrolling down
                //As soon as you scroll down, there is no risk of pulling to reload
                element.off("touchmove");
            }
            previousY = e.touches[0].clientY;
        },
        unbindScrollEvent = function(e){
            element.off("touchmove");
        };
    element.on("touchstart", bindScrollEvent);
    element.on("touchend", unbindScrollEvent);
    

    Naturally, the same can also be achieved with pure JS.

提交回复
热议问题