Disable double-tap “zoom” option in browser on touch devices

后端 未结 14 1261
-上瘾入骨i
-上瘾入骨i 2020-11-28 02:58

I want to disable the double-tap zoom functionality on specified elements in the browser (on touch devices), witho

相关标签:
14条回答
  • 2020-11-28 03:33

    Using CSS touch-events: none Completly takes out all the touch events. Just leaving this here in case someone also has this problems, took me 2 hours to find this solution and it's only one line of css. https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action

    0 讨论(0)
  • 2020-11-28 03:38
    * {
        -ms-touch-action: manipulation;
        touch-action: manipulation;
    }
    

    Disable double tap to zoom on touch screens. Internet explorer included.

    0 讨论(0)
  • 2020-11-28 03:40

    Here we go

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

    0 讨论(0)
  • 2020-11-28 03:41

    I just wanted to answer my question properly as some people do not read the comments below an answer. So here it is:

    (function($) {
      $.fn.nodoubletapzoom = function() {
          $(this).bind('touchstart', function preventZoom(e) {
            var t2 = e.timeStamp
              , t1 = $(this).data('lastTouch') || t2
              , dt = t2 - t1
              , fingers = e.originalEvent.touches.length;
            $(this).data('lastTouch', t2);
            if (!dt || dt > 500 || fingers > 1) return; // not double-tap
    
            e.preventDefault(); // double tap - prevent the zoom
            // also synthesize click events we just swallowed up
            $(this).trigger('click').trigger('click');
          });
      };
    })(jQuery);
    

    I did not write this, i just modified it. I found the iOS-only version here: https://gist.github.com/2047491 (thanks Kablam)

    0 讨论(0)
  • 2020-11-28 03:42

    Note (as of 2020-08-04): this solution does not appear to work in iOS Safari v12+. I will update this answer and delete this note once I find a clear solution that covers iOS Safari.

    CSS-only solution

    Add touch-action: manipulation to any element on which you want to disable double tap zoom, like with the following disable-dbl-tap-zoom class:

    .disable-dbl-tap-zoom {
      touch-action: manipulation;
    }
    

    From the touch-action docs (emphasis mine):

    manipulation

    Enable panning and pinch zoom gestures, but disable additional non-standard gestures such as double-tap to zoom.

    This value works on Android and on iOS.

    0 讨论(0)
  • 2020-11-28 03:42

    Simple prevent the default behavior of click, dblclick or touchend events will disable the zoom functionality.

    If you have already a callback on one of this events just call a event.preventDefault().

    0 讨论(0)
提交回复
热议问题