Ryongyon!

这世界上假绅士太多,缺少训练有素的流氓

· 实践教程 · · 10642次浏览

用 FastClick 消除移动端点击延迟

为什么存在延迟?

根据 Google 开发者文档:

...mobile browsers will wait approximately 300ms from the time that you tap the button to fire the click event. The reason for this is that the browser is waiting to see if you are actually performing a double tap.

译:从点击屏幕上的元素到触发元素的 click 事件,移动浏览器会有大约 300 毫秒的等待时间。为什么这么设计呢? 因为它想看看你是不是要进行双击(double tap)操作

FastClick 是什么?

由 FTLabs 开发,Github 项目地址:https://github.com/ftlabs/fastclick
专门用来处理移动端点击事件的 300 毫秒延迟

如何使用 FastClick?

第一步:

<script type='application/javascript' src='/path/to/fastclick.js'></script>

第二步:

<script type="text/javascript">
if ('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', function() {
        FastClick.attach(document.body);
    }, false);
}
</script>

或者, 如果您正在使用 jQuery:

<script type="text/javascript">
$(function() {
        FastClick.attach(document.body);
    });
</script>
评论 (10条)