在觸控螢幕裝置上,一些比較基礎的手勢都需要透過對 touch 事件進行二次封裝才能實現。
zepto 是行動端上使用率比較高的一個類別庫,但是其touch 模組模擬出來的一些事件存在一些相容性問題,如tap 事件在某些安卓裝置上存在事件穿透的bug,其他類型的事件也或多或少的存在一些相容性問題。
於是乎,乾脆自己動手對這些常用的手勢事件進行了封裝,由於沒有太多真實的設備來進行測試,可能存在一些兼容性問題,下面的代碼也只是在iOS 7、Andorid 4 上的一些比較常見的瀏覽器中測試通過。
tap事件
tap 事件相當於pc 瀏覽器中的click 效果,雖然在觸控螢幕裝置上click 事件仍然可用,但是在許多裝置上,click 會存在一些延遲,如果想要快速回應的「click」 事件,需要借助touch 事件來實現。
var startTx, startTy;
var startTx, startTy;
var touches = e.touches[0];
startTx = touches.clientX;
}, false );
element.addEventListener( 'touchend', function( e ){
var touches = e.changedTouches[0],
endTx = touches.clientX,
// 在部分裝置上touch 事件比較靈敏,導致按下和鬆開手指時的事件座標會出現一點點變化
if( Math.abs(startTx - endTx) console.log( 'fire tap event' );
}, false );
doubleTap事件
var isTouchEnd = false,
lastTime = 0,
lastTx = null,
lastTy = null,
End5pirstTouch > dTapTimer, startTx, startTy, startTime;
element.addEventListener( 'touchstart', function( e ){ if( dTapTimer ){
clearTimeout( dTapTimer );
clearTimeout( dTapTimer );
clearTimeout( dTapTimer );
}
var touches = e.touches[0];
startTx = touches.clientX;
startTy = touches.clientY;}, false );
element.addEventListener( 'touchend', function( e ){
endTx = touches.clientX,
endTx = touches.clientX,
now = Date.now(),
duration = now - lastTime;
// 首先確保能觸發單一的tap 事件
if( duration // 本次的tap 位置和上一次的tap 的位置允許一定範圍內的誤差
null &&
Math.abs(lastTx - endTx) Math.abs(lastTy - endTy)
firstTouchEnd = true; lastTx = lastTy = null;
🎜> }
else{
lastTx = endTx ;
lastTy = endTy;
}
}
else{
firstTouchEnd = true;
lastTx = lastTy = null;
}
lastTime = now;
}, false );
// 有一定的幾率會導致第二次不會響應touchstart 和touchend 事件
// 同時手指長時間的touch不會觸發click
if( ~navigator.userAgent.toLowerCase().indexOf('iphone os') ){
body.addEventListener( 'touchstart', function( e ){
}, true );
body.addEventListener( 'touchend', function( e ){
var noLongTap = Date.now() - startTime
firstTouchEnd = false;
dTapTimer = setTimeout(function(){
}, 400 );
}
true ;
}
}, true );
// iOS 上手指多次敲擊螢幕時的速度過快不會觸發click 事件
element.addEventListener( 'click', function( e ){
if( dTapTimer ){
clearTimeout( dTapTimer );
dTapTimer = null;
firstTouchEnd = true;
}
}, false );
}
longTap事件
longTap 事件是當手指長時間按住螢幕保持不動時觸發的事件。
複製程式碼
程式碼如下:var startTx, startTy, lTapTimer;
element.addEventListener( 'touchstart', function( e ){
if( lTapTimer ){
clearTimeout( lTapTimer );
clearTimeout( lTapTimer );
startTx =touches.clientX;
startTy =touches.clientY;
console.log( '觸發長按事件' );
}, 1000 );
}, false );
var Touches = e.touches[0],
endTx = Touches.clientX,
cli.
if( lTapTimer && (Math.abs(endTx - startTx) > 5 || Math.abs(endTy - startTy) > 5) ){
clearTimeout( lTapTimer ); }
},假 );
element.addEventListener( 'touchend', function( e ){
clearTimeout( lTapTimer );
clearTimeout( lTapTimer );
. ;
swipe 事件是當手指在螢幕上滑動後觸發的事件,根據手指滑動的方向又分為 swipeLeft (向左)、swipeRight (向右)、swipeUp (向上)、swipeDown (向下)。 >
複製程式碼
startTx = Touches.clientX; startTy = Touches.clientY;
isTouchMove = false;
}, false );
element.addEventListener( 'touchmove', function( e ){
isTouchMove = true;
e.preventDefault();
}, false );
element.addEventListener( 'touchend', function( e ){
if( !isTouchMove ){
return;
}
var attempts = e.changedTouches[0],
endTx = Touches.clientX,
endTy = Touches.clientY,
Ty,
if( Math.abs(distanceX) >= Math.abs(distanceY) ){
if( distanceX > 20 ){
console。 isSwipe = true;
}
else if( distanceX console.log( '觸發向右滑動事件' );
isSwipe = true;
}
}
else{
if( distanceY > 20 ){
if( distanceY > 20 ){
if( distanceY > 20 ){
if( distanceY > 20 ){
if( distanceY > 20 ){
isSwipe = true;
}
else if( distanceY consoleY console.log() '後續滑動事件;
isSwipe = true;
}
}
if( isSwipe ){
}
}, false );

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器