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

后端 未结 14 1315
-上瘾入骨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:45

    If you need a version that works without jQuery, I modified Wouter Konecny's answer (which was also created by modifying this gist by Johan Sundström) to use vanilla JavaScript.

    function preventZoom(e) {
      var t2 = e.timeStamp;
      var t1 = e.currentTarget.dataset.lastTouch || t2;
      var dt = t2 - t1;
      var fingers = e.touches.length;
      e.currentTarget.dataset.lastTouch = t2;
    
      if (!dt || dt > 500 || fingers > 1) return; // not double-tap
    
      e.preventDefault();
      e.target.click();
    }
    

    Then add an event handler on touchstart that calls this function:

    myButton.addEventListener('touchstart', preventZoom); 
    

提交回复
热议问题