


Detailed introduction to the basics of JavaScript mobile events and a summary of commonly used event libraries
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'; } })
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'; })
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(){ } })
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

In-depth understanding of the close button event in jQuery During the front-end development process, we often encounter situations where we need to implement the close button function, such as closing pop-up windows, closing prompt boxes, etc. When using jQuery, a popular JavaScript library, it becomes extremely simple and convenient to implement the close button event. This article will delve into how to use jQuery to implement close button events, and provide specific code examples to help readers better understand and master this technology. First, we need to understand how to define

Methods for building event-based applications in PHP include using the EventSourceAPI to create an event source and using the EventSource object to listen for events on the client side. Send events using Server Sent Events (SSE) and listen for events on the client side using an XMLHttpRequest object. A practical example is to use EventSource to update inventory counts in real time in an e-commerce website. This is achieved on the server side by randomly changing the inventory and sending updates, and the client listens for inventory updates through EventSource and displays them in real time.
