移动Web开发之事件
触摸事件touch
touch
事件:
touch
是移动端的触摸事件
而且是一组事件
touchstart
当手指触摸屏幕的时候触发touchmove
当手指在屏幕来回的滑动时候触发touchend
当手指离开屏幕的时候触发touchcancel
当被迫终止滑动的时候触发(来电,弹消息)- 利用
touch
相关事件实现移动端常见滑动效果和移动端常见的手势事件
使用touch
:
- 绑定事件:
box.addEventListener('touchstart',function () { });
- 事件对象:
名字:TouchList
------触摸点(一个手指触摸就是一个触发点,和屏幕的接触点的个数)的集合
changedTouches
改变后的触摸点集合
targetTouches
当前元素的触发点集合
touches
页面上所有触发点集合 - 触摸点集合在每个事件触发的时候会不会去记录触摸
changedTouches
每个事件都会记录
targetTouches
,touches
在离开屏幕的时候无法记录触摸点 - 分析滑动实现的原理:
4.1 就是让触摸的元素随着手指的滑动做位置的改变
4.2 位置的改变:需要当前手指的座标
4.3 在每一个触摸点中会记录当前触摸点的座标 e.touches[0] 第一个触摸点
4.4clientX
clientY
基于浏览器窗口(视口)
4.4pageX pageY
基于页面(视口)
4.4screenX screenY
基于屏幕
<!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</title>
<style>
body{
margin: 0;
padding: 0;
}
.box{
width: 200px;
height: 200px;
background: pink;
float: left;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
window.onload = function () {
var box = document.querySelector('.box');
box.addEventListener('touchstart',function (e) {
console.log('start');
console.log(e.touches[0].clientX,e.touches[0].clientY);
console.log(e.touches[0].pageX,e.touches[0].pageY);
console.log(e.touches[0].screenX,e.touches[0].screenY);
});
box.addEventListener('touchmove',function (e) {
console.log('move');
console.log(e);
});
box.addEventListener('touchend',function (e) {
console.log('end');
console.log(e);
});
/*PC端事件*/
/*box.addEventListener('click',function (e) {
console.log('click');
console.log(e);
});*/
}
</script>
</body>
</html>
手势事件
- 理解移动端的手势事件
swipe swipeLeft swipeRight swipeUp swipeDown
左滑
和右滑
手势怎么实现原理- 没有原生手势,但可以封装
Html
结构如上,js
控制
window.onload = function () {
/*1. 理解移动端的手势事件*/
/*2. swipe swipeLeft swipeRight swipeUp swipeDown */
/*3. 左滑和右滑手势怎么实现*/
var bindSwipeEvent = function (dom,leftCallback,rightCallback) {
/*自定义手势的条件*/
/*1.必须滑动过*/
/*2.滑动的距离50px*/
var isMove = false;
var startX = 0;
var distanceX = 0;
dom.addEventListener('touchstart',function (e) {
startX = e.touches[0].clientX;
});
dom.addEventListener('touchmove',function (e) {
isMove = true;
var moveX = e.touches[0].clientX;
distanceX = moveX - startX;
});
dom.addEventListener('touchend',function (e) {
/*滑动结束*/
if(isMove && Math.abs(distanceX) > 50){
if(distanceX > 0){
//rightCallback && rightCallback(e);
rightCallback && rightCallback.call(this,e);
}else{
leftCallback && leftCallback.call(this,e);
}
}
/*重置参数*/
isMove = false;
startX = 0;
distanceX = 0;
});
}
bindSwipeEvent(document.querySelector('.box'),function (e) {
console.log(this);
console.log(e);
console.log('左滑手势');
},function (e) {
console.log(this);
console.log(e);
console.log('右滑手势');
});
}
说明
rightCallback && rightCallback.call(this,e);
- 先判断方法是否存在
rightCallback.call(this,e)
改变rightCallback
方法的this
指向,并传递参数e
.- 此时
rightCallback()
方法执行是this
的指向即为传递的this
对象
tap
事件
- 轻击 轻触 (响应速度快),提高移动端点击事件的响应,默认点击延时
300ms
- 移动端也有
click
事件
在移动为了区分是滑动还是点击,click
点击延时300ms
- 解决
click
延时,影响用户体验 响应太慢了。
解决方案
- 使用tap事件(不是移动端原生事件,通过touch相关事件衍生过来) (zepto.js tap事件)了解其原理
//tap事件原理,设置(touchend时间-touchstart时间 < 150 && 没有触发移动事件),即为tap
window.onload = function () {
/*使用tap事件*/
/*1. 响应的速度比click要快 150ms */
/*2. 不能滑动*/
var bindTapEvent = function (dom, callback) {
/*事件的执行顺序 touchstart -> touchmove -> touchend -> click*/
/*在谷歌浏览器模拟看不到300ms的效果*/
/*在真机上面才能看看到延时效果*/
var startTime = 0;
var isMove = false;
dom.addEventListener('touchstart', function () {
//console.log('touchstart');
startTime = Date.now(); //new Date().getTime();效率没有Date.now()高
/*Date.now();*/
});
dom.addEventListener('touchmove', function () {
//console.log('touchmove');
isMove = true;
});
dom.addEventListener('touchend', function (e) {
//console.log('touchend');
console.log((Date.now() - startTime));
if ((Date.now() - startTime) < 150 && !isMove) {
callback && callback.call(this, e);
}
startTime = 0;
isMove = false;
});
//click事件
/*dom.addEventListener('click',function () {
//console.log('click');
});*/
}
bindTapEvent(document.querySelector('.box'), function (e) {
console.log(this);
console.log(e);
console.log('tap事件')
});
}
- 使用一个叫:
fastclick.js
提供移动端click
响应速度的
2.1 下载:https://cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js
2.2 使用:
<script src="../js/fastclick.min.js"></script>
<script>
/*当页面的dom元素加载完成*/
document.addEventListener('DOMContentLoaded', function() {
/*初始化方法*/
FastClick.attach(document.body);
}, false);
/*正常使用click事件就可以了*/
</script>