jQuery.preventMacBackScroll icon indicating copy to clipboard operation
jQuery.preventMacBackScroll copied to clipboard

prevent_right (to avoid going forward in browser history)

Open kachkaev opened this issue 10 years ago • 2 comments

Hi micho,

I noticed that the plugin does not prevent the "forward" event and decided to fix this. Here is what worked for me in OSX Yosemite for both Chrome and Safari:

      prevent_right =  x > 0 && !_($(e.target).parents()).detect(function (el) {
          var $el = $(el);
          if ($el.css("overflow") != "scroll") {
              return false;
          }
          var scrollLeft = $el.scrollLeft();
          var childrenOuterWidth = $el.children().outerWidth();
          var ownWidth = el.clientWidth;
          return ownWidth && childrenOuterWidth > ownWidth && childrenOuterWidth - scrollLeft > ownWidth; 
      });

...

if (prevent_left || prevent_up || prevent_right) {
    e.preventDefault();
}

Perhaps, this is not the most optimal solution, but it works!

Another thing that you might want to consider is checking whether a user is scrolling directly left / right or is moving up / down, but slightly inaccurately. In the latter case the browser does not suggest to go back or forward, but scrolling still becomes blocked.

Here's how I approached this issue:


absX = Math.abs(x);
absY = Math.abs(y);

prevent_left  = (absY <= absX) && ...
prevent_top   = (absX <= absY) && ...
prevent_right = (absY <= absX) && ...

Thanks for making the plugin! I wish I found it earlier and did not spend several hours on vain experiments :–) Without jQuery.preventMacBackScroll I'm sure I'd spend much more time!

kachkaev avatar Feb 26 '15 18:02 kachkaev

Hello, @kachkaev, and thanks for posting this!

I'm pretty busy these days, but if you send a PR I can take a look and merge it in. Does it break any features, or does it simply add the new you you described?

micho avatar Feb 27 '15 10:02 micho

Same problem with time now and at least for another month, sorry! :–)

I haven't spotted any problems in the latest FF, Chrome and Safari, but cannot be 100% sure about other versions. Let's leave the github issue for now as it is for those who are looking for some solution with the "forward" action immediately.

You might want to refactor the code when adding prevent_right later on. _($(e.target).parents()) repeats three times now, and this might be a bit costly.

kachkaev avatar Feb 27 '15 11:02 kachkaev