国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

js移動(dòng)端事件基礎(chǔ)及常用事件庫(kù)詳解

2019-11-19 15:47:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一、事件基礎(chǔ)

PC:click、mouseover、mouseout、mouseenter、mouseleave、mousemove、mousedown、mouseup、mousewheel、keydown、keyup、load、scroll、blur、focus、change...

移動(dòng)端:click(單擊)、load、scroll、blur、focus、change、input(代替keyup、keydown)...TOUCH事件模型(處理單手指操作)、GESTURE事件模型(處理多手指操作)

TOUCH:touchstart、touchmove、touchend、touchcancel

GESTURE:gesturestart、gesturechange、gestureend

1、click:在移動(dòng)端click屬于單擊事件,不是點(diǎn)擊事件,在移動(dòng)端的項(xiàng)目中我們經(jīng)常會(huì)區(qū)分單擊做什么和雙擊做什么,所以移動(dòng)端的瀏覽器在識(shí)別click的時(shí)候,只有確定是單擊后才會(huì)把它執(zhí)行:

在移動(dòng)端使用click會(huì)存在300ms的延遲:瀏覽器在第一次點(diǎn)擊結(jié)束后,還需要等到300ms看是否觸發(fā)了第二次點(diǎn)擊,如果觸發(fā)了第二次點(diǎn)擊就不屬于click了,沒(méi)有觸發(fā)第二次點(diǎn)擊才屬于click

下面代碼是移動(dòng)端模擬click時(shí)間的代碼

function on(curEle,type,fn){   curEle.addEventListener(type,fn,false);  }  var oBox = document.querySelector('.box');  //移動(dòng)端采用click存在300ms延遲  // oBox.addEventListener('click',function(){  //  this.style.webkitTransform = 'rotate(360deg)'  // },false)  //使用TOUCH事件模型實(shí)現(xiàn)點(diǎn)擊操作(單擊&&雙擊)  on(oBox,'touchstart',function(ev){   //ev:TouchEvent事件 屬性 type、target、preventDefault(returnValue)、stopPropagation、changedTouches、touches   //changedTouches和touches都是手指信息的集合(touchList),touches獲取到值的必要條件只有手指還在屏幕上才可以獲取,所以在touchend事件中如果想獲取手指離開(kāi)的瞬間坐標(biāo)只能使用changedTouches獲取   var point = ev.touches[0];   this['strX'] = point.clientX;   this['strY'] = point.clientY;   this['isMove'] = false;  })  on(oBox,'touchmove',function(ev){   var point = ev.touches[0];   var newX = point.clientX,    newY = point.clientY;   //判斷是否發(fā)生滑動(dòng),我們需要判斷偏移的值是否在30PX以內(nèi)   if(Math.abs(newX-this['strX'])>30 || Math.abs(newY-this['strY'])>30){    this['isMove'] = true;   }  })  on(oBox,'touchend',function(ev){   if(this['isMove'] === false){    //沒(méi)有發(fā)生移動(dòng) 點(diǎn)擊    this.style.webkitTransitionDuration = '1s';    this.style.webkitTransform = 'rotate(360deg)';    var delayTimer = window.setTimeout(function(){     this.style.webkitTransitionDuration = '0s';     this.style.webkitTransform = 'rotate(0deg)';    }.bind(this),1000);   }else{    //滑動(dòng)    this.style.background = 'red';   }  })

同時(shí)也可以使用fastclick.js來(lái)解決移動(dòng)端click事件的300ms延遲 (github地址https://github.com/zhouxiaotian/fastclick

2、點(diǎn)擊、單擊、雙擊、長(zhǎng)按、滑動(dòng)、左滑、右滑、上滑、下滑

單擊和雙擊(300MS)

點(diǎn)擊和長(zhǎng)按(750MS)

點(diǎn)擊和滑動(dòng)(X/Y軸偏移的距離是否在30PX以內(nèi),超過(guò)30PX就是滑動(dòng))

左右滑動(dòng)和上下滑動(dòng)(X軸偏移的距離 > Y軸偏移的距離 = 左右滑 相反就是上下滑)

左滑和右滑(偏移的距離 >0 = 右滑  相反就是左滑)

二、常用的事件庫(kù)

FastClick.js :解決CLICK事件300MS的延遲

TOUCH.js:百度云移動(dòng)手勢(shì)庫(kù)  GitHub地址  https://github.com/Clouda-team/touch.code.baidu.com

實(shí)例如下:  

var oBox = document.querySelector('.box');  //單擊  touch.on(oBox,'tap',function(ev){   this.style.webkitTransitionDuration = '1s';   this.style.webkitTransform = 'rotate(360deg)';   var delayTimer = window.setTimeout(function(){    this.style.webkitTransitionDuration = '0s';    this.style.webkitTransform = 'rotate(0deg)';    window.clearTimeout(delayTimer)   }.bind(this),1000)  })  //雙擊  touch.on(oBox,'doubletap',function(ev){   this.style.webkitTransitionDuration = '1s';   this.style.webkitTransform = 'rotate(-360deg)';   var delayTimer = window.setTimeout(function(){    this.style.webkitTransitionDuration = '0s';    this.style.webkitTransform = 'rotate(0deg)';    window.clearTimeout(delayTimer)   }.bind(this),1000)  })  //長(zhǎng)按  touch.on(oBox,'hold',function(ev){   this.style.backgroundColor = 'red';  })

HAMMER.js

Zepto.js:被譽(yù)為移動(dòng)端的小型JQ,JQ由于是在PC端使用的,所以代碼中包含了大量對(duì)于ie低版本瀏覽器的兼容處理,而ZEPTO只應(yīng)用在移動(dòng)端開(kāi)發(fā),所以在JQ的基礎(chǔ)上沒(méi)有對(duì)低版本的ie進(jìn)行支持

  JQ中提供了很多的選擇器類型及DOM操作方法,但是ZEPTO只是實(shí)現(xiàn)了部分常用的選擇器和方法。例如:JQ中的動(dòng)畫(huà)方法有animate、hide、show、fadeIn、fadeOut、fadeToggle、slideDown、slideUp、slideToggle...但是在ZEPTO中只有animate

  ZEPTO的源代碼大小比JQ小很多

  ZEPTO專門(mén)為移動(dòng)端開(kāi)發(fā)而誕生,所以相對(duì)于JQ來(lái)說(shuō)更適合移動(dòng)端:

  ZEPTO的animate動(dòng)畫(huà)方法支持了CSS3動(dòng)畫(huà)的操作

  ZEPTO專門(mén)的準(zhǔn)備了移動(dòng)端常用 的事件操作:tap、singleTap、doubleTap、longTap、swipe、swipeUp、swipeDown、swipeLeft、swipeRight

實(shí)例代碼如下:  

$('.box').singleTap(function(ev){   $(this).animate({    rotate:'360deg'   },1000,'linear',function(){    this.style.webkitTransform = 'rotate(0)'   })  })  $('.box').on('touchstart',function(){   $(this).css('background','red')  })  $.ajax({   url:'',   type:'get',   dataType:'json',   cache:false,   success:function(){       }  })

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青龙| 宁安市| 勐海县| 资溪县| 桂平市| 浠水县| 石门县| 旬阳县| 中江县| 正蓝旗| 栖霞市| 内黄县| 那坡县| 太康县| 清水河县| 巴马| 永吉县| 鲜城| 屯留县| 江华| 冕宁县| 闸北区| 弥勒县| 陆丰市| 安龙县| 思南县| 莎车县| 平凉市| 攀枝花市| 麟游县| 寿宁县| 渝中区| 大名县| 大安市| 鄂托克旗| 惠州市| 金沙县| 两当县| 时尚| 宁武县| 清水河县|