處理Touch事件能讓你瞭解到用戶的每一根手指的位置,在touch事件觸發的時候產生,能夠經過touch event handler的event對象取到,若是基於zepto.js開發,通常是經過event.touches[0]來獲取屬性。重要屬性如 下:clientX,clientY:觸摸點相對於瀏覽器窗口viewport的位置;pageX,pageY: 觸摸點相對於頁面的位置;screenX,screenY:觸摸點相對於屏幕的位置 ;identifier:touch對象的unique IDjavascript
你能夠綁定如下四種Touch事件來了解基本的touch事件:css
touchstart:手指觸摸屏幕上的時候觸發 html
touchmove:手指在屏幕上移動的時候觸發 java
touchend:手指從屏幕上拿起的時候觸發 git
touchcancel:系統取消touch事件的時候觸發github
html:瀏覽器
<div id="touch_test">app
<span class="clear">clear</span>ide
<ul id="touchs">測試
</ul>
</div>
css:
#touchs{
margin: 10px;width: 100px;height: auto;min-height: 100px;
border:1px solid #c2ddb6;border-radius: 2px;background: #c2ddb6;
}
#touchs li {list-style: none;}
.clear{
margin-left: 10px;display:inline-block;height: 24px;width: 40px;color:#fff;
font-size: 14px;line-height: 24px;background: #c2ddb6;text-align: center;
}
js:
<script type="text/javascript" src="script/zepto.min.js"></script>
<script type="text/javascript">
;(function($){
$('#touchs').find('li').remove();
$('#touchs').bind("touchstart",function(event){
var touchpros =event.touches[0];
console.log(touchpros);
$('#touchs').append('<li>touchstart...</li>');
});
$('#touchs').bind("touchmove",function(){
$('#touchs').append('<li>touchmove...</li>');
});
$('#touchs').bind("touchend",function(){
$('#touchs').append('<li>touchend...</li>');
});
$('#touchs').bind("touchcancel",function(){
$('#touchs').append('<li>touchcancel...</li>');
});
$('.clear').bind("click",function(){
$('#touchs').find('li').remove();
});
})(Zepto);
</script>
當你觸摸屏幕並擡起手指,只觸發touchstart和touched。點擊clear 能夠清除本次測試的數據,能夠再次測試。
當若是手指觸摸屏幕並移動後擡起會觸發touchstart,屢次touchmove,touchend或touchcanel
能夠根據基本的touch事件來封裝成你想要實現複雜的效果,好比向左或向右滑動,
向上或向下滑動,並在滑動時封裝你想實現的效果。
打開:https://github.com/madrobby/zepto/tree/master/src;
touch.js封裝好了滑動事件的處理,將其添加到本身的項目中,就能夠直接調用向右、右、上、下滑動的事件。這樣zepto.js官網手冊中的例子就能夠正常運行了。