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事件