1、300ms延迟由来

300 毫秒延迟的主要原因是解决双击缩放(double tap to zoom)。双击缩放,顾名思义,即用手指在屏幕上快速点击两次,iOS 自带的 Safari 浏览器会将网页缩放至原始比例。 那么这和 300 毫秒延迟有什么联系呢? 假定这么一个场景。用户在 iOS Safari 里边点击了一个链接。由于用户可以进行双击缩放或者双击滚动的操作,当用户一次点击屏幕之后,浏览器并不能立刻判断用户是确实要打开这个链接,还是想要进行双击操作。因此,iOS Safari 就等待 300 毫秒,以判断用户是否再次点击了屏幕。 鉴于iPhone的成功,其他移动浏览器都复制了 iPhone Safari 浏览器的多数约定,包括双击缩放,几乎现在所有的移动端浏览器都有这个功能。、

2、解决方案

(1)添加viewpoint meta标签

<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />

(2)FastClick

https://github.com/ftlabs/fastclick

移动端事件触发顺序:在移动端,手指点击一个元素,会经过:touchstart --> touchmove -> touchend -->click。

fastclick.js的原理是:FastClick的实现原理是在检测到touchend事件的时候,会通过DOM自定义事件立即出发模拟一个click事件,并把浏览器在300ms之后真正的click事件阻止掉。

fastclick同样可以解决移动端点透现象。

点透现象:当A/B两个层上下z轴重叠,上层的A点击后消失或移开(这一点很重要),并且B元素本身有默认click事件(如a标签)或绑定了click事件。在这种情况下,点击A/B重叠的部分,就会出现点透的现象。点透现象的关键点:

A/B两个层上下z轴重叠。

上层的A点击后消失或移开。(这一点很重要)

B元素本身有默认click事件(如a标签) 或 B绑定了click事件。

在以上情况下,点击A/B重叠的部分,就会出现点透的现象。

示例代码:

<!doctype html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>移动端点透现象</title>
<style>
* {
margin: 0px;
padding: 0px;
} #div1 {
/*红色半透明遮盖层A*/
width: 300px;
height: 300px;
background-color: rgba(255, 0, 0, 0.25);
} #div2 {
/*黄色内容层B*/
width: 240px;
height: 240px;
background-color: yellow;
position: absolute;
left: 30px;
top: 30px;
z-index: -1;
} #console {
/*绿色状态输出框*/
border: 1px solid green;
position: absolute;
top: 300px;
width: 100%;
}
</style>
</head> <body>
<div id="div1"></div>
<div id="div2">
<a href="https://www.baidu.com/">www.baidu.com</a>
</div>
<div id="console"></div>
<script type="text/javascript">
var div1 = document.getElementById("div1");
var div2 = document.getElementById('div2'); function handle(e) {
var tar = e.target,
eve = e.type;
console.log("target:" + tar.id + " event:" + eve)
if(tar.id === "div1") {
div1.style.display = "none";
}
}
div1.addEventListener("touchend", handle);
div1.addEventListener("touchstart", handle);
div2.addEventListener('click', handle);
</script>
</body> </html>

解决方法:

<!doctype html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>移动端点透现象解决方法</title>
<style>
* {
margin: 0px;
padding: 0px;
} #div1 {
/*红色半透明遮盖层A*/
width: 300px;
height: 300px;
background-color: rgba(255, 0, 0, 0.25);
} #div2 {
/*黄色内容层B*/
width: 240px;
height: 240px;
background-color: yellow;
position: absolute;
left: 30px;
top: 30px;
z-index: -1;
} #console {
/*绿色状态输出框*/
border: 1px solid green;
position: absolute;
top: 300px;
width: 100%;
}
</style>
</head> <body>
<div id="div1"></div>
<div id="div2">
<a href="https://www.baidu.com/">www.baidu.com</a>
</div>
<div id="console"></div>
<script src="https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js"></script>
<script type="text/javascript">
if('addEventListener' in document) {
document.addEventListener('DOMContentLoaded', function() {
FastClick.attach(document.body);
}, false);
}
var div1 = document.getElementById("div1");
var div2 = document.getElementById('div2'); function handle(e) {
var tar = e.target,
         eve = e.type;
console.log("target:" + tar.id + " event:" + eve)
if(tar.id === "div1") {
div1.style.display = "none";
}
}
div1.addEventListener("touchend", handle);
div1.addEventListener("touchstart", handle);
div2.addEventListener('click', handle);
</script>
</body> </html>

移动端300ms延迟由来及解决方案的更多相关文章

  1. 移动端300ms延迟问题和点击穿透问题

    一.移动端300ms延迟问题: 一般情况下,如果没有经过特殊处理,移动端浏览器在派发点击事件的时候,通常会出现300ms左右的延迟.也就是说,当我们点击页面的时候移动端浏览器并不是立即作出反应,而是会 ...

  2. touch-action 解决移动端300ms延迟问题

    CSS3 新属性, touch-action: manipulation; 可以有效的解决移动端300ms延迟的问题 移动端300ms延迟问题一直都是h5APP的痛点, 有很多库或者方法都可以解决, ...

  3. js解决苹果移动端300ms延迟的问题

    做移动端页面开发的可能会了解到,ios系统click事件会有卡顿的现象,这个问题的根源是苹果本身自带的safari有双击放大页面的功能,再次双击会返回到原始尺寸,所以在第一次点击的系统会延迟300ms ...

  4. 移动端300ms延迟解决的几种方法;

    方案一:禁用缩放 当HTML文档头部包含如下meta标签时: <meta name="viewport" content="user-scalable=no&quo ...

  5. 解决移动端300ms延迟fastclick

    为什么要使用fastclick 移动设备上的浏览器默认会在用户点击屏幕大约延迟300毫秒后才会触发点击事件,这是为了检查用户是否在做双击.为了能够立即响应用户的点击事件,才有了fastclick. f ...

  6. 用Fastclick解决移动端300ms延迟问题

    移动设备上的浏览器默认会在用户点击屏幕大约延迟300毫秒后才会触发点击事件,这是为了检查用户是否在做双击. 为了能够立即响应用户的点击事件,才有了FastClick. 用法: 引入fastclick. ...

  7. 移动端300ms延迟解决方法在vue 里面的一些小坑

    话不多说上图: 至于import为什么会报错,瞅下面这个图: 总结:要搞懂个必须了解下es6的解构赋值才能在这方面装逼,网上资料一大堆自行百度.

  8. 移动端300ms延迟原理,穿透、遮罩层滑动导致下面滑动总结

    遮罩层滑动导致下面滑动 1,阻止弹层滑动,使用默认事件,使用这种方式弹层不能滑动 document.getElementById("model").addEventListener ...

  9. 移动端的300ms延迟和点击穿透

    移动端300ms延迟:假定这么一个场景.用户在 浏览器里边点击了一个链接.由于用户可以进行双击缩放或者双击滚动的操作,当用户一次点击屏幕之后,浏览器并不能立刻判断用户是确实要打开这个链接,还是想要进行 ...

随机推荐

  1. mysql数据库备份shell

    sip=xxx.xxx.xxx.xxx user=user passwd=passwd back_path=/home/xxxxx/mysqlbak data_name=data_name date= ...

  2. A class of finite groups with abelian 2-Sylow subgroups By CHIH-HAN SAH

    Remark: 1.An element of a group which conjugate to its inverse is called a real element. If $G$ has ...

  3. mysql 取得各种时间

    转载 取得当前日期:DATE_FORMAT(NOW(),'%e'): 取得当前年月:DATE_FORMAT(NOW(),'%Y-%c'):Y:四位.y:两位:m:两位.c:前面不加0: /*当前时间加 ...

  4. 非常棒的轨迹插件Better Trails v1.4.6

    点击下载

  5. Mac电脑Dock栏开启放大特效

    1 右击Dock栏空白处,选择启用放大 2 在Dock偏好设置中调整图标放大的倍数 3 滑动鼠标,查看放大效果

  6. ACM-ICPC 2018 沈阳赛区网络预赛 J. Ka Chang(树状数组+分块)

    Given a rooted tree ( the root is node 1 ) of N nodes. Initially, each node has zero point. Then, yo ...

  7. [leetcode]387. First Unique Character in a String第一个不重复字母

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  8. [leetcode]658. Find K Closest Elements绝对距离最近的K个元素

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  9. 11-web网页制作APP

    如何将H5和WebApp 加壳成apk.ipa     问题:已经做好的纯H5的站点 想分别加两个壳子,变成apk和ipa ,要怎么实现? 要点: 1. app只是壳子,打开app直接跳转到H5的Ur ...

  10. Activity横竖屏切换时 一些数据的保存

    private VideoView videoView; 02.private static final String VIDEO_PATH = Environment 03. .getExterna ...