ZhangYang's Blog

手机移动端页面

300ms延迟

时间流

  • 那时的网页都是给 PC 用的,宽度是 1000px 左右
  • 移动端页面会自动缩放,如320px缩放成980px来看960px的页面
  • 双击是表示放大,单击是点击
  • 单击会出现300ms延迟,如果300ms内再点击就是放大,否则是点击
  • BAT程序员解决单击出现300ms延迟用touchstart事件
  • 导致一系列问题,所以造了fackclick.js库

解决方法

1
2
// 如今都是给移动端宽度写页面,不需要缩放,所以不需要也没有300ms延迟
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

触屏事件

触屏事件的应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>触屏事件</title>
<style>
.box{
width: 50vw;
height: 50vh;
background-color: green;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
var box = document.querySelector('.box')
box.addEventListener('touchstart',function(){
console.log('开始摸了')
})
box.addEventListener('touchcancel',function(){
console.log('取消触摸')
})
box.addEventListener('touchend',function(){
console.log('摸完了')
})
box.addEventListener('touchenter',function () {
console.log('摸进去了')
})
box.addEventListener('touchleave',function(){
console.log('摸出来了')
})
box.addEventListener('touchmove',function () {
console.log('边摸边动')
})
</script>
</body>
</html>

触屏事件的写字小例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>触屏事件</title>
<style>
/*手机端初始化*/
*,*::after,*::before{
box-sizing: border-box;
}
img{
max-width: 100%;
max-height: 100%;
}
body{
margin: 0;
}
.box{
width: 100vw;
height: 100vh;
background-color: green;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
var box = document.querySelector('.box')
function addPoint(x,y) {
let div = document.createElement('div')
div.style.position = 'absolute'
div.style.top = y + 'px'
div.style.left = x + 'px'
div.style.height = '5px'
div.style.width = '5px'
div.style.background = 'black'
document.body.appendChild(div)
}
box.addEventListener('touchstart',function(e){
console.log('开始摸了')
let {pageX,pageY} = e.touches[0]
addPoint(pageX,pageY)
})
</script>
</body>
</html>