1.touchstart:只要將手指放在了屏幕上(而無論是幾隻),都會觸發touchstart事件。javascript
2.touchmove: 當咱們用手指在屏幕上滑動時,這個事件會被連續觸發。 若是咱們不但願頁面隨之滑動,咱們可使用event的preventDefault來阻止這個默認行爲。css
3.touchend: 當手指滑動後離開屏幕,這時就觸發了touchend事件。html
4.touchcancel: 系統中止跟蹤觸摸時候會觸發。例如在觸摸過程當中忽然頁面alert()一個提示框,此時會觸發該事件,這個事件比較少用。java
1. touches,這是一個類數組對象,包含了全部的手指信息,若是隻有一個手指,那麼咱們用touches[0]來表示。jquery
2. targetTouches 。 手指在目標區域的手指信息。數組
3. changedTouches:最近一次觸發該事件的手指信息。ide
4. touchend時,touches與targetTouches信息會被刪除,changedTouches保存的最後一次的信息,最好用於計算手指信息。post
先看效果圖:優化
它的實現原理很是簡單,就是將紅色圓形的postion屬性設爲absolute,而後,當咱們滑動它時,就觸發了touchmove事件,將其Left和top設置爲event的pageX和pageY便可,爲保證觸發中心與圓心在同一位置,只需將pageX加上width的一半,pageY加上height的一半。雲計算
源碼以下:
<!DOCTYPE html> <html> <head> <title>touchExample</title> <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0"> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <style> #touchDiv{ position: absolute; width: 50px; height: 50px; top: 20px; left: 20px; text-align: center; line-height: 50px; color:white; border-radius: 50%; background-color: red; } </style> </head> <body> <div id="touchDiv">點我</div> <script> var touchDiv = document.getElementById("touchDiv"); var x,y; touchDiv.addEventListener("touchstart",canDrag); touchDiv.addEventListener("touchmove",drag); touchDiv.addEventListener("touchend",nodrag); function canDrag (e) { console.log("god開始"); } function drag (e) { $("#touchDiv").css("left",e.touches[0].pageX-25); $("#touchDiv").css("top",e.touches[0].pageY-25); } function nodrag () { console.log("god結束"); } </script> </body> </html>
這個實例就是下拉刷新功能的實現,效果以下:
源碼以下:
<!DOCTYPE html> <html> <head> <title>下拉刷新</title> <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0"> <style> *{ margin:0; padding: 0; font-size:15px; } .header{ height: 50px; line-height: 50px; text-align: center; background-color: blue; color:white; font-size: 23px; } .drag_to_refresh{ align-items: center; padding-left: 155px; background-color: #bbb; color:yellow; display: none; } .refresh{ height: 50px; line-height: 50px; text-align: center; background-color: #bbb; color: green; display: none; } .drag{ text-align: center; background-color: lightgray; position: relative; padding:20px; text-indent: 1em; line-height: 30px; font-size:18px; } </style> </head> <body> <div class="header">政務雲</div> <div class="drag_to_refresh"></div> <div class="refresh">刷新中...</div> <div class="drag">電子政務雲(E-government cloud)屬於政府雲,結合了雲計算技術的特色,對政府管理和服務職能進行精簡、優化、整合,並經過信息化手段在政務上實現各類業務流程辦理和職能服務,爲政府各級部門提供可靠的基礎IT服務平臺。</div> <script>window.onload = function () { var initX; var drag_content = document.querySelector(".drag"); var drag_to_refresh = document.querySelector(".drag_to_refresh"); var refresh = document.querySelector(".refresh"); drag_content.addEventListener("touchmove",drag); drag_content.addEventListener("touchstart",dragStart); drag_content.addEventListener("touchend",dragEnd); function dragStart(e){ initY = e.touches[0].pageY; console.log(initX); } function drag (e){ drag_to_refresh.style.display = "block"; drag_to_refresh.style.height = (e.touches[0].pageY - initY) + "px"; console.log(drag_to_refresh.style.height); if(parseInt(drag_to_refresh.style.height)>=100){ // 注意:由於height獲得的值是px爲單位,因此用parseInt解析 drag_to_refresh.style.height = "100px"; if(parseInt(drag_to_refresh.style.height)>80){ drag_to_refresh.style.lineHeight = drag_to_refresh.style.height; drag_to_refresh.innerHTML = "鬆開刷新"; } } } function dragEnd (e){ if(parseInt(drag_to_refresh.style.height)>80){ refresh.style.display = "block"; setTimeout(reload,1000); } drag_to_refresh.style.display = "none"; } function reload () { location.reload(); } }</script> </body> </html>