提问者:小点点

IOS HTML 禁用双击缩放


我正在设计一个主要专注于数据输入的网站。在我的一个表单中,我有按钮可以快速增加和减少表单字段中的数字值。我正在使用

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

禁用变焦,这似乎可以使用Firefox应用程序进行IOS。然而,当另一个用户用Safari测试它时,点击按钮太快导致页面放大,分散用户注意力,无法快速增加值。似乎从IOS 10开始,苹果出于可访问性原因删除了user-calable=no,所以这就是为什么它只适用于Firefox等第三方浏览器。我发现最接近禁用双击缩放的是这个

var lastTouchEnd = 0;
document.addEventListener('touchend', function (event) {
    var now = (new Date()).getTime();
    if (now - lastTouchEnd <= 300) {
        event.preventDefault();
    }
    lastTouchEnd = now;
}, false);

从https://stackoverflow.com/a/38573198但是,这完全禁用了快速点击,这虽然阻止了双击缩放,但也阻止了用户快速输入值。有什么方法可以允许快速按下按钮,同时禁用双击缩放吗?


共3个答案

匿名用户

CSS 属性触摸操作对我有用。在 iOS 11.1 上测试。

button {
    touch-action: manipulation;
}

有关详细信息,请参阅MDN:https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action

匿名用户

我最终通过使用以下代码解决了这个问题:参见上面Greg的回答

$(document).click(function(event) {
    element = document.elementFromPoint(event.clientX, event.clientY);
    if(document.getElementById("div_excluded_from_doubletap").contains(element)) {
        event.preventDefault();
        clickFunction(element);
    }
});

匿名用户

我做了一个有点复杂的回答,但它在阻止双击和缩放方面工作得非常好和可靠,并允许几乎所有其他类型的交互

let drags = new Set() //set of all active drags
document.addEventListener("touchmove", function(event){
  if(!event.isTrusted)return //don't react to fake touches
  Array.from(event.changedTouches).forEach(function(touch){
    drags.add(touch.identifier) //mark this touch as a drag
  })
})
document.addEventListener("touchend", function(event){
  if(!event.isTrusted)return
  let isDrag = false
  Array.from(event.changedTouches).forEach(function(touch){
    if(drags.has(touch.identifier)){
      isDrag = true
    }
    drags.delete(touch.identifier) //touch ended, so delete it
  })
  if(!isDrag && document.activeElement == document.body){
    //note that double-tap only happens when the body is active
    event.preventDefault() //don't zoom
    event.stopPropagation() //don't relay event
    event.target.focus() //in case it's an input element
    event.target.click() //in case it has a click handler
    event.target.dispatchEvent(new TouchEvent("touchend",event))
    //dispatch a copy of this event (for other touch handlers)
  }
})

注意:greg的答案不能始终如一地工作(双击某些元素仍然会缩放)

如果你想防止捏缩放,你需要一些JS和CSS(不要问我为什么):

document.addEventListener('touchmove', function(event){
  if (event.scale !== 1) event.preventDefault(); //if a scale gesture, don't
})

*{touch-action: pan-x pan-y} /*only allow scroll gestures*/

相关问题