如何检测 ios 设备上网站的三次点击?

how to detect triple click on website on ios device?

我必须在移动设备上三次点击我的网站正文时显示和隐藏 div 我在 JavaScript 中编写的以下代码在 android 设备中工作正常,但在 IOS 中不起作用。 那你能帮我解决一下吗?

代码是:

window.onload = function() {

  document.querySelector('body').addEventListener('click', function(evt) {

    evt.preventDefault();
    if (evt.detail === 3) {
      document.getElementById('mmu').style.height = '100px';
    }

    if (evt.detail === 1) {
      document.getElementById('mmu').style.height = '0px';
    }

  });

}
#mmu {
  width: 100px;
  height: 0px;
  position: fixed;
  left: 0;
  cursor: pointer;
  top: 0;
  background: red;
}

body {
  height: 1000px;
  background: #eee;
  width: 100%;
  cursor: pointer;
}
<div id="mmu"></div>

在 iOS 上,click 事件不会正常触发。相反,您需要监视触摸事件,例如 touchend 以检查点击了多少次。

例如,您可能会像这样

尝试检查点击是否在足够的超时内完成window
TOUCH_TIMEOUT_MILLISECONDS = 500
touch_count = 0
window.onload = function () {
    document.querySelector('body').addEventListener('touchend', function (evt) {
        touch_count += 1

        setTimeout(function () {
            touch_count = 0
        }, TOUCH_TIMEOUT_MILLISECONDS);

        if (touch_count === 3) {
            document.getElementById('mmu').style.height = '100px';
        }

        if (touch_count === 1) {
            document.getElementById('mmu').style.height = '0px';
        }

        evt.preventDefault();
    });

}

根据您的要求,您可能还需要考虑从同一操作触发的 touchendclick 事件。