touch系事件是触屏设备上的一系列事件,当用户触摸屏幕时及进行一系列操做时发生的事件。
包含touchstart, touchmove, touchend事件。javascript
咱们将焦点集中在事件这个抽象对象上去,例如当手指在触摸屏上移动过程当中发生的touchmove事件,咱们去查找此事件相关的属性,根据先后事件的发生(例如touchstart和touchend事件),去判断是否符合程序触发任务的条件(例如当用户向上滑动时你要作一个页面动画)。html
须要指出的是,touch事件同其余dom事件同样(由于自己就属于dom事件,只不过用在触屏设备的新增html5事件而已),用addEventListener绑定,在事件处理时使用e.prevantDefault()来阻止默认事件执行(例如你在滚动div时,使用它来阻止页面滚动),使用e.stopPropagation()来阻止事件向上冒泡,等等。html5
touchList:touch类对象组成的数组
touch对象:touch对象表示一个触点,触点属性包含identifier, target, clientX/clientY, pageX/pageY, screenX/screenY, force(接触面最小椭圆角度)/radiusX(接触面最小椭圆X轴)/radiusY(接触面最小椭圆Y轴)/rotationAngle(chrome上目前仍是带有webkit前缀的webkitForce(压力)/webkitRadiusX/webkitRadiusY/webkitRotationAngle), 其中identifier用来标识该触点,由于屏幕上可能同时存在多个触点。java
实验:web
当触摸点在触摸平面上移动时,触发touchmove事件。
实验:chrome
再次拿出touch对象,但这时咱们实验一下touch对象的target属性:
简单地让Fa从div#test滑出到body区域
过程当中咱们记录了touchmove事件,为简单起见,咱们查看最后一个touchmove的事件对象,咱们发现:
现象:此时的changedTouches中的touch对象的target为div#test,因此target属性指的是触发事件时所在的元素;
咱们在此又发现一个现象:
从始至终,这一系列的touchmove事件都会触发div#test上的touchmove事件回调;数组
旨在观察当move过程当中删除div#test的状况。代码以下:
```javascript
var test=document.getElementById('test');dom
window.addEventListener('touchmove',function(e){
console.log('move on the window');
console.log(e);
},false);ide
test.addEventListener('touchmove',function(e){
e.preventDefault();
//e.stopPropagation();
console.log('move on the test');
console.log(e);
if(e.changedTouches[0].clientY>420){
//由于该测试中div#test的高度为400px且起点为(0,0)
if(test.parentNode){
test.parentNode.removeChild(test);
console.log('remove')
}
}
},false);
```
咱们依然简单地让Fa从div#test滑出到body区域。
现象:在控制台上能够看出:
当div#test被删除后,只执行了div#test上的touchmove, 但已经再也不冒泡到window。
注意:remove打印出来以前,事件已经冒泡到了window,因此随后有一个window的touchend的回调被执行。测试
当触摸点离开屏幕时触发touchend事件。
实验:
在div#test上触屏后离开,触点无移动
现象:触发div#test的touchend事件
在div#test上滑动,且过程当中没有离开div#test
现象:不会触发touchend事件
在div#test上滑动,且最终中止到body上并抬起手指
现象:不会触发touchend事件