JavaScript

超轻量级php框架startmvc

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

更新时间:2020-05-30 01:12 作者:startmvc
一、事件基础PC:click、mouseover、mouseout、mouseenter、mouseleave、mousemove、mousedown、mouseup、mousewhe

一、事件基础

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地址https://github.com/zhouxiaotian/fastclick)

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

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