Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, November 25, 2016

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

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


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 used on android phones and Chrome as browser. Also, the site is saved on the home screen so Android manage the whole thing as an app (using a WebView I guess).

Chrome Beta (and I think also the Android System WebView) has introduced a "pull down to refresh" feature (See this link for example).

This is an handy feature but I was wondering if it can be disabled with some meta tag (or javascript stuff) because the refresh can be easily triggered by the user while navigating the list and the whole app is reloaded.

Also this is a feature not needed by the application.

I know that this feature is still available only in Chrome beta, but I have the sensation that this is landing on the stable app, too.

Thank you!

Edit: I've uninstalled Chrome Beta and the link pinned to the home screen now opens with the stable Chrome. So the pinned links starts with Chrome and not with a webview.

Edit: today (2015-03-19) the pull-down-to-refresh has come to the stable chrome.

Edit: from @Evyn answer I follow this link and got this javascript/jquery code that work.

var lastTouchY = 0;  var preventPullToRefresh = false;    $('body').on('touchstart', function (e) {      if (e.originalEvent.touches.length != 1) { return; }      lastTouchY = e.originalEvent.touches[0].clientY;      preventPullToRefresh = window.pageYOffset == 0;  });    $('body').on('touchmove', function (e) {      var touchY = e.originalEvent.touches[0].clientY;      var touchYDelta = touchY - lastTouchY;      lastTouchY = touchY;      if (preventPullToRefresh) {          // To suppress pull-to-refresh it is sufficient to preventDefault the first overscrolling touchmove.          preventPullToRefresh = false;          if (touchYDelta > 0) {              e.preventDefault();              return;          }      }  });

As @bcintegrity pointed out, I hope for a site manifest solution (and/or a meta-tag) in the future.

Moreover suggestions for the code above are welcome.

Answer by Kevin Busse for Disabling android's chrome pull-down-to-refresh feature


At the moment you can only disable this feature via chrome://flags/#disable-pull-to-refresh-effect - open directly from your device.

You could try to catch touchmove events, but chances are very slim to achieve an acceptable result.

Answer by Eclectic for Disabling android's chrome pull-down-to-refresh feature


I use MooTools, and I have created a Class to disable refresh on a targeted element, but the crux of it is (native JS):

var target = window; // this can be any scrollable element  var last_y = 0;  target.addEventListener('touchmove', function(e){      var scrolly = target.pageYOffset || target.scrollTop || 0;      var direction = e.changedTouches[0].pageY > last_y ? 1 : -1;      if(direction>0 && scrolly===0){          e.preventDefault();      }      last_y = e.changedTouches[0].pageY;  });  

All we do here is find the y direction of the touchmove, and if we are moving down the screen and the target scroll is 0, we stop the event. Thus, no refresh.

This means we are firing on every 'move', which can be expensive, but is the best solution I have found so far ...

Answer by Evyn for Disabling android's chrome pull-down-to-refresh feature


The default action of the pull-to-refresh effect can be effectively prevented by doing any of the following :

  1. preventDefault?ing some portion of the touch sequence, including any of the following (in order of most disruptive to least disruptive):
    • a. The entire touch stream (not ideal).
    • b. All top overscrolling touchmoves.
    • c. The first top overscrolling touchmove.
    • d. The first top overscrolling touchmove only when 1) the initial touchstart occurred when the page y scroll offset was zero and 2) the touchmove would induce top overscroll.
  2. Applying ?touch-action: none? to touch-targeted elements, where appropriate, disabling default actions (including pull-to-refresh) of the touch sequence.
  3. Applying ?overflow-y: hidden? to the body element, using a div for scrollable content if necessary.
  4. Disabling the effect locally via chrome://flags (disable-pull-to-refresh-effect).

See more

Answer by Nicolas Bouliane for Disabling android's chrome pull-down-to-refresh feature


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.

Answer by Sacky San for Disabling android's chrome pull-down-to-refresh feature


Simple Javascript

I implemented using standard javascript. Simple and easy to implement. Just paste and it works fine.

  

Answer by kofifus for Disabling android's chrome pull-down-to-refresh feature


Note that overflow-y is not inherited, so you need to set it on ALL block elements.

You can do this with jQuery simply by:

        $(document.body).css('overflow-y', 'hidden');           $('*').filter(function(index) {              return $(this).css('display') == 'block';          }).css('overflow-y', 'hidden');  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.