Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect desktop Safari's two-fingered swipe

In Safari on OS X, with a magic trackpad or macbook trackpad, swiping right or left with two fingers effects back and forward, respectively. Is there a way to detect this, as distinct from clicking back/forward, or hitting command+arrow etc?

The reason is that the swipe has it's own reveal-style swiping animation, and with custom ajax transitions on a site, it looks really weird when you get one following the other. This happens when browsing code on github, for example.

Update 23/6/16: Github reverted to simply swapping out the page content with no transition, which was a smart move. My current practice is to do the same for back/forward, even if some sort of fancy transition in is use on the site. This prevents clashes between whatever the browser might do and the site transition

like image 287
Greg Avatar asked Jun 26 '12 01:06

Greg


People also ask

How do I get rid of two finger swipe on Mac?

You can disable chromes gestures by going to System Preference > Mouse OR Trackpad > More Gestures > and uncheck Swipe between pages.

How do I turn off two fingers on safari?

Answer: A: Check the setting in System Preferences > Trackpad > More Gestures tab > Check "Swipe between pages". Make sure that it says left or right with two fingers.

How do you detect swipe?

Swiping in touch is the act of quickly moving your finger across the touch surface in a certain direction.

Why is my three finger swipe not working Mac?

In trackpad settings, try unchecking the box next to "Swipe between pages." Close and reopen system preferences, then check the box and choose "Swipe with three fingers" from the list. That's what should fix it, theoretically.


1 Answers

You can use the mousewheel event to see if an horizontal scroll on the trackpad has been performed before your popstate event:

// boolean that stores if a swipe has been performed.
var bScrolled = false;
// countdown in ms before resetting the boolean.
var iTime = 1000;
var oTimeout;
window.addEventListener('mousewheel', function(e) {
  if (e.wheelDeltaY === 0) {
  // there is an horizontal scroll
    if (!bScrolled) {
    // no need to set bScrolled to true if it has been done within the iTime time. 
      bScrolled = true;
      oTimeout = setTimeout(function(){
        bScrolled = false;
      }, iTime);
    }
  }
});

window.onpopstate = function() {
  // clear the timeout to be sure we keep the correct value for bScrolled
  clearTimeout(oTimeout);
  // check if there has been a swipe prior to the change of history state
  if (bScrolled) {
    // check which browser & OS the user is using, then
    // trigger your awesome custom transition.
  }
}
like image 104
RaphKomjat Avatar answered Sep 21 '22 16:09

RaphKomjat