当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js移动端事件基础及常用事件库详解

js移动端事件基础及常用事件库详解

2017年12月12日  | 移动技术网IT编程  | 我要评论
一、事件基础 pc:click、mouseover、mouseout、mouseenter、mouseleave、mousemove、mousedown、mouseup、

一、事件基础

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属于单击事件,不是点击事件,在移动端的项目中我们经常会区分单击做什么和双击做什么,所以移动端的浏览器在识别click的时候,只有确定是单击后才会把它执行:

在移动端使用click会存在300ms的延迟:浏览器在第一次点击结束后,还需要等到300ms看是否触发了第二次点击,如果触发了第二次点击就不属于click了,没有触发第二次点击才属于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事件模型实现点击操作(单击&&双击)
  on(obox,'touchstart',function(ev){
   //ev:touchevent事件 属性 type、target、preventdefault(returnvalue)、stoppropagation、changedtouches、touches
   //changedtouches和touches都是手指信息的集合(touchlist),touches获取到值的必要条件只有手指还在屏幕上才可以获取,所以在touchend事件中如果想获取手指离开的瞬间坐标只能使用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;
   //判断是否发生滑动,我们需要判断偏移的值是否在30px以内
   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){
    //没有发生移动 点击
    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地址)

2、点击、单击、双击、长按、滑动、左滑、右滑、上滑、下滑

单击和双击(300ms)

点击和长按(750ms)

点击和滑动(x/y轴偏移的距离是否在30px以内,超过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只应用在移动端开发,所以在jq的基础上没有对低版本的ie进行支持

  jq中提供了很多的选择器类型及dom操作方法,但是zepto只是实现了部分常用的选择器和方法。例如:jq中的动画方法有animate、hide、show、fadein、fadeout、fadetoggle、slidedown、slideup、slidetoggle...但是在zepto中只有animate

  zepto的源代码大小比jq小很多

  zepto专门为移动端开发而诞生,所以相对于jq来说更适合移动端:

  zepto的animate动画方法支持了css3动画的操作

  zepto专门的准备了移动端常用 的事件操作: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(){
    
   }
  })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网