Home > Web Front-end > JS Tutorial > Detailed introduction to the basics of JavaScript mobile events and a summary of commonly used event libraries

Detailed introduction to the basics of JavaScript mobile events and a summary of commonly used event libraries

巴扎黑
Release: 2017-08-17 16:06:50
Original
1401 people have browsed it

This article mainly introduces the basics of js mobile events and common event libraries in detail. It has certain reference value. Interested friends can refer to it

1. Event Basics

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

Mobile terminal: click (click), load, scroll, blur, focus, change, input (replacing keyup, keydown)...TOUCH event model (processing single-finger operations), GESTURE event model (processing multi-finger operations)

TOUCH: touchstart, touchmove, touchend, touchcancel

GESTURE: gesturestart, gesturechange, gestureend

1. Click: On the mobile side, click is a click event, not a click event. In mobile projects, we often distinguish between what a click does and what a double-click does. Therefore, when the mobile browser recognizes a click, it will only execute it after confirming that it is a click:

On the mobile side There will be a 300ms delay when using click: after the first click is completed, the browser needs to wait 300ms to see whether the second click is triggered. If the second click is triggered, it is not a click, and the second click is not triggered. It belongs to click

The following code is the code to simulate the click time on the mobile side


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';
   }
  })
Copy after login

At the same time, fastclick.js can also be used to solve the 300ms of the click event on the mobile side. Delay (github address https://github.com/zhouxiaotian/fastclick)

2. Click, click, double-click, long press, slide, slide left, slide right, slide up, slide down

Click and double-click (300MS)

Click and long press (750MS)

Click and slide (whether the X/Y axis offset distance is within 30PX, if it exceeds 30PX, it is sliding)

Swipe left and right and slide up and down (X-axis offset distance > Y-axis offset distance = Left and right slides, the opposite is up and down)

Left slide and right slide (Offset distance >0 = Swipe right (the opposite is swipe left)

2. Commonly used event libraries

FastClick.js: Solve the 300MS delay of CLICK event

TOUCH.js: Baidu Cloud Mobile Gesture Library GitHub address https://github.com/Clouda-team/touch.code.baidu.com

The examples are as follows:


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';
  })
Copy after login

HAMMER.js

Zepto.js: Known as the small JQ on the mobile side, JQ is used on the PC side, so the code contains a lot of compatibility with low-version IE browsers Processing, and ZEPTO is only used in mobile development, so there is no support for lower versions of IE on the basis of JQ

JQ provides a lot of selector types and DOM operation methods, but ZEPTO only implements Some commonly used selectors and methods. For example: the animation methods in JQ include animate, hide, show, fadeIn, fadeOut, fadeToggle, slideDown, slideUp, slideToggle... but in ZEPTO there is only animate

The source code size of ZEPTO is much smaller than that of JQ

ZEPTO was specially developed for mobile terminals, so it is more suitable for mobile terminals than JQ:

ZEPTO’s animate animation method supports the operation of CSS3 animations

ZEPTO is specialized for mobile terminals We have prepared commonly used event operations on mobile terminals: tap, singleTap, doubleTap, longTap, swipe, swipeUp, swipeDown, swipeLeft, swipeRight

The example code is as follows:


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

The above is the detailed content of Detailed introduction to the basics of JavaScript mobile events and a summary of commonly used event libraries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template