做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

javascript移動設備Web開發中對touch事件的封裝實例_javascript技巧

來源:懂視網 責編:小采 時間:2020-11-27 21:24:01
文檔

javascript移動設備Web開發中對touch事件的封裝實例_javascript技巧

javascript移動設備Web開發中對touch事件的封裝實例_javascript技巧:在觸屏設備上,一些比較基礎的手勢都需要通過對 touch 事件進行二次封裝才能實現。zepto 是移動端上使用率比較高的一個類庫,但是其 touch 模塊模擬出來的一些事件存在一些兼容性問題,如 tap 事件在某些安卓設備上存在事件穿透的 bug,其他類型的事件也或
推薦度:
導讀javascript移動設備Web開發中對touch事件的封裝實例_javascript技巧:在觸屏設備上,一些比較基礎的手勢都需要通過對 touch 事件進行二次封裝才能實現。zepto 是移動端上使用率比較高的一個類庫,但是其 touch 模塊模擬出來的一些事件存在一些兼容性問題,如 tap 事件在某些安卓設備上存在事件穿透的 bug,其他類型的事件也或

在觸屏設備上,一些比較基礎的手勢都需要通過對 touch 事件進行二次封裝才能實現。
zepto 是移動端上使用率比較高的一個類庫,但是其 touch 模塊模擬出來的一些事件存在一些兼容性問題,如 tap 事件在某些安卓設備上存在事件穿透的 bug,其他類型的事件也或多或少的存在一些兼容性問題。

于是乎,干脆自己動手對這些常用的手勢事件進行了封裝,由于沒有太多真實的設備來進行測試,可能存在一些兼容性問題,下面的代碼也只是在 iOS 7、Andorid 4 上的一些比較常見的瀏覽器中測試通過。

tap事件

tap 事件相當于 pc 瀏覽器中的 click 效果,雖然在觸屏設備上 click 事件仍然可用,但是在很多設備上,click 會存在一些延遲,如果想要快速響應的 “click” 事件,需要借助 touch 事件來實現。

代碼如下:
var startTx, startTy;

element.addEventListener( 'touchstart', function( e ){
var touches = e.touches[0];

startTx = touches.clientX;
startTy = touches.clientY;
}, false );

element.addEventListener( 'touchend', function( e ){
var touches = e.changedTouches[0],
endTx = touches.clientX,
endTy = touches.clientY;

// 在部分設備上 touch 事件比較靈敏,導致按下和松開手指時的事件坐標會出現一點點變化
if( Math.abs(startTx - endTx) < 6 && Math.abs(startTy - endTy) < 6 ){
console.log( 'fire tap event' );
}
}, false );

doubleTap事件

doubleTap 事件是當手指在相同位置范圍內和極短的時間內兩次敲擊屏幕時觸發的事件。在部分瀏覽器下,doubleTap 事件會選中文本,如果不希望選中文本,可以給元素添加 user-select:none 的 css 屬性。
代碼如下:
var isTouchEnd = false,
lastTime = 0,
lastTx = null,
lastTy = null,
firstTouchEnd = true,
body = document.body,
dTapTimer, startTx, startTy, startTime;

element.addEventListener( 'touchstart', function( e ){
if( dTapTimer ){
clearTimeout( dTapTimer );
dTapTimer = null;
}

var touches = e.touches[0];

startTx = touches.clientX;
startTy = touches.clientY;
}, false );

element.addEventListener( 'touchend', function( e ){
var touches = e.changedTouches[0],
endTx = touches.clientX,
endTy = touches.clientY,
now = Date.now(),
duration = now - lastTime;

// 首先要確保能觸發單次的 tap 事件
if( Math.abs(startTx - endTx) < 6 && Math.abs(startTx - endTx) < 6 ){
// 兩次 tap 的間隔確保在 500 毫秒以內
if( duration < 301 ){
// 本次的 tap 位置和上一次的 tap 的位置允許一定范圍內的誤差
if( lastTx !== null &&
Math.abs(lastTx - endTx) < 45 &&
Math.abs(lastTy - endTy) < 45 ){

firstTouchEnd = true;
lastTx = lastTy = null;
console.log( 'fire double tap event' );
}
}
else{
lastTx = endTx;
lastTy = endTy;
}
}
else{
firstTouchEnd = true;
lastTx = lastTy = null;
}

lastTime = now;
}, false );

// 在 iOS 的 safari 上手指敲擊屏幕的速度過快,
// 有一定的幾率會導致第二次不會響應 touchstart 和 touchend 事件
// 同時手指長時間的touch不會觸發click

if( ~navigator.userAgent.toLowerCase().indexOf('iphone os') ){

body.addEventListener( 'touchstart', function( e ){
startTime = Date.now();
}, true );

body.addEventListener( 'touchend', function( e ){
var noLongTap = Date.now() - startTime < 501;

if( firstTouchEnd ){
firstTouchEnd = false;
if( noLongTap && e.target === element ){
dTapTimer = setTimeout(function(){
firstTouchEnd = true;
lastTx = lastTy = null;
console.log( 'fire double tap event' );
}, 400 );
}
}
else{
firstTouchEnd = 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 );
lTapTimer = null;
}

var touches = e.touches[0];

startTx = touches.clientX;
startTy = touches.clientY;

lTapTimer = setTimeout(function(){
console.log( 'fire long tap event' );
}, 1000 );

e.preventDefault();
}, false );

element.addEventListener( 'touchmove', function( e ){
var touches = e.touches[0],
endTx = touches.clientX,
endTy = touches.clientY;

if( lTapTimer && (Math.abs(endTx - startTx) > 5 || Math.abs(endTy - startTy) > 5) ){
clearTimeout( lTapTimer );
lTapTimer = null;
}
}, false );

element.addEventListener( 'touchend', function( e ){
if( lTapTimer ){
clearTimeout( lTapTimer );
lTapTimer = null;
}
}, false );

swipe事件

swipe 事件是當手指在屏幕上滑動后觸發的事件,根據手指滑動的方向又分為 swipeLeft (向左)、swipeRight (向右)、swipeUp (向上)、swipeDown (向下)。

代碼如下:
var isTouchMove, startTx, startTy;

element.addEventListener( 'touchstart', function( e ){
var touches = e.touches[0];

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 touches = e.changedTouches[0],
endTx = touches.clientX,
endTy = touches.clientY,
distanceX = startTx - endTx
distanceY = startTy - endTy,
isSwipe = false;

if( Math.abs(distanceX) >= Math.abs(distanceY) ){
if( distanceX > 20 ){
console.log( 'fire swipe left event' );
isSwipe = true;
}
else if( distanceX < -20 ){
console.log( 'fire swipe right event' );
isSwipe = true;
}
}
else{
if( distanceY > 20 ){
console.log( 'fire swipe up event' );
isSwipe = true;
}
else if( distanceY < -20 ){
console.log( 'fire swipe down event' );
isSwipe = true;
}
}

if( isSwipe ){
console.log( 'fire swipe event' );
}
}, false );

上面模擬的事件都封裝在 MonoEvent 中了。完整代碼地址:https://github.com/chenmnkken/monoevent,需要的朋友看看吧~

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

javascript移動設備Web開發中對touch事件的封裝實例_javascript技巧

javascript移動設備Web開發中對touch事件的封裝實例_javascript技巧:在觸屏設備上,一些比較基礎的手勢都需要通過對 touch 事件進行二次封裝才能實現。zepto 是移動端上使用率比較高的一個類庫,但是其 touch 模塊模擬出來的一些事件存在一些兼容性問題,如 tap 事件在某些安卓設備上存在事件穿透的 bug,其他類型的事件也或
推薦度:
標簽: 移動 事件 touch
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 寻乌县| 城固县| 盐城市| 泌阳县| 神池县| 巴塘县| 宁海县| 金乡县| 寻甸| 清苑县| 上蔡县| 简阳市| 迁安市| 江西省| 南丹县| 理塘县| 芒康县| 大安市| 常山县| 旌德县| 靖江市| 廉江市| 成都市| 松潘县| 怀集县| 沈丘县| 辉南县| 抚宁县| 天峻县| 赫章县| 漳浦县| 新民市| 东丽区| 明星| 定边县| 武鸣县| 寿光市| 阳东县| 宣武区| 兰州市| 文昌市|