为什么会出现 300ms 延迟?
系统需要判断点击后300ms之内是否有第二次点击,如果有,就是放大缩小整个窗口查
看窗口内容。如果没有第二次点击,就会传入click事件给页面。那么页面的click就会导
致有300ms延迟。
解决的方法
通用的js解决方案,使用fastclick
原理
在检测到 touchend 事件的时候,会通过DOM自定义事件立即出发模拟一个click事件
并把浏览器在300ms之后真正的click事件阻止掉
使用
在 javascript 保重下载了 fastclick.js,把它添加到 html 页面
<script type='application/javascript' src='/path/to/fastclick.js'></script>
if ('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', function() {
FastClick.attach(document.body);
}, false);
}
CommonJS 模块里面可以这样使用
npm install fastclick -S
var attachFastClick = require('fastclick');
attachFastClick(document.body);
Vue 中使用
npm install fastclick -S
import FastClick from 'fastclick'
FastClick.attach(document.body);
fastclick github地址
see you ~