亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js移動端事件基礎(chǔ)及常用事件庫詳解

 更新時間:2017年08月15日 15:25:55   作者:diasa  
這篇文章主要為大家詳細(xì)介紹了js移動端事件基礎(chǔ)及常用事件庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、事件基礎(chǔ)

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

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

TOUCH:touchstart、touchmove、touchend、touchcancel

GESTURE:gesturestart、gesturechange、gestureend

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

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

下面代碼是移動端模擬click時間的代碼

function on(curEle,type,fn){
   curEle.addEventListener(type,fn,false);
  }
  var oBox = document.querySelector('.box');
  //移動端采用click存在300ms延遲
  // oBox.addEventListener('click',function(){
  //  this.style.webkitTransform = 'rotate(360deg)'
  // },false)
  //使用TOUCH事件模型實現(xiàn)點擊操作(單擊&&雙擊)
  on(oBox,'touchstart',function(ev){
   //ev:TouchEvent事件 屬性 type、target、preventDefault(returnValue)、stopPropagation、changedTouches、touches
   //changedTouches和touches都是手指信息的集合(touchList),touches獲取到值的必要條件只有手指還在屏幕上才可以獲取,所以在touchend事件中如果想獲取手指離開的瞬間坐標(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ā)生滑動,我們需要判斷偏移的值是否在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){
    //沒有發(fā)生移動 點擊
    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{
    //滑動
    this.style.background = 'red';
   }
  })

同時也可以使用fastclick.js來解決移動端click事件的300ms延遲 (github地址https://github.com/zhouxiaotian/fastclick

2、點擊、單擊、雙擊、長按、滑動、左滑、右滑、上滑、下滑

單擊和雙擊(300MS)

點擊和長按(750MS)

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

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

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

二、常用的事件庫

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

TOUCH.js:百度云移動手勢庫  GitHub地址  https://github.com/Clouda-team/touch.code.baidu.com

實例如下:  

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)
  })
  //長按
  touch.on(oBox,'hold',function(ev){
   this.style.backgroundColor = 'red';
  })

HAMMER.js

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

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

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

  ZEPTO專門為移動端開發(fā)而誕生,所以相對于JQ來說更適合移動端:

  ZEPTO的animate動畫方法支持了CSS3動畫的操作

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

實例代碼如下:  

$('.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(){
    
   }
  })

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論