Home Web Front-end JS Tutorial Overview of click events and commonly used events in js (PC and mobile)

Overview of click events and commonly used events in js (PC and mobile)

Aug 07, 2018 am 10:10 AM

1. Click event:

It is a click event on the PC side, but it is a click event on the mobile side. In mobile projects, we often distinguish clicks. What to do and what to do when double-clicking, so when the mobile browser recognizes the click, it will only execute it after confirming that it is a click. There will be a 300ms delay when using click on the mobile side: the browser will wait for the first click after the first click. , you still need to wait 300ms to see if the second click is triggered. If the second click is triggered, it does not belong to the click. If the second click is not triggered, it belongs to the click. However, in some scenarios, it is necessary

Cancel delay:

(1) Still zoom: To use this method, you must completely disable zoom to achieve the goal. Although most mobile terminals can solve this delay problem, some Apple phones still cannot; width - the width of the viewport; height - the height of the viewport; initial-scale - the initial scaling ratio; minimum-scale - the minimum ratio that the user is allowed to zoom to; maximum-scale - the maximum ratio that the user is allowed to zoom to; user-scalable - the user Is it possible to manually zoom

 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
Copy after login

(2) fastclick.js: FastClick is a lightweight library developed by FT Labs specifically to solve the 300 millisecond click delay problem of mobile browsers. In short, when FastClick detects the touchend event, it will immediately trigger a simulated click event through the DOM custom event, and block the click event that the browser actually triggers after 300 milliseconds

First Step: Introduce the fastclick.js file into the page.

Step 2: Add the following code to the js file: After the window load event, call FastClick.attach() on the body.

window.addEventListener(function(){   
    FastClick.attach( document.body );  
},false );
Copy after login

If your project uses JQuery, rewrite the above code as:

$(function() {    
    FastClick.attach(document.body);    
});
Copy after login

Replace the click event on the mobile side:

(1) Use touchstart: touch events include touchstart, touchend, touchmove, etc. Simply use touchstart to replace click. But the problem is, what if I want to bind a click event and a sliding event to the same object? At this time, conflicts will occur. ;On the mobile side, when your finger clicks on an element, it will go through: touchstart --> touchmove -> touchend -->click

Touch event model:

Event nametouchstarttouchmovetouchendtouchcancleTriggered when closing/exiting under special circumstancesAttributes of touch event:
Function
Press your finger on the screen
Finger sliding on the screen
Finger off the screen

Attribute nametypetargetpreventDefault(returnValue)stopPropagation(cancleBubble)touches[0].clientXchangedTouches

(2) tap event: There is no tap event in the standard event. The tap event is encapsulated by some libraries, such as zepto, using touch. The time and finger position are recorded at touchstart and touchend, and compared at touchend. If the finger If the position is the same position and the time interval is short, and the touchmove event is not triggered during the process, the callback function is called; the click event will still be generated after 300ms, but there is no monitoring on the object, resulting in the "click through" phenomenon. : 1) A modal box pops up on the page. There is a button on the modal box (close the modal box), and there is an input box directly below the button (on the main page); 2) When the close button on the modal box is clicked, The modal box disappears immediately, but the click event is triggered after 300ms, and the input box happens to listen to the click event, so the input box will get focus

Commonly used events on the PC side:

Function
Event type
Event source
Block default behavior
Stop propagation of events
x value of touch position
Current value and leaving value
Event name Function
click Triggered when the mouse clicks
mouseover Triggered when the mouse pointer moves over the element
mouseout Triggered when the mouse pointer moves out of the element
mouseenter Triggered when the mouse pointer moves over the element (bubbles are not supported)
mouseleave Triggered when the mouse pointer moves out of the element (bubbling is not supported)
mousemove Triggered when the mouse pointer moves over the element
mousedown Fired when the mouse button is pressed on the element
mouseup Fired when the mouse button is released on the element
mousewheel Script that runs when the mouse wheel is being scrolled
keydown Triggered when the user presses a key
keyup Triggered when the user releases the key
load Triggered after the page has finished loading
scroll Script that runs when the element scroll bar is scrolled
blur Script that runs when the element loses focus
focus Script that runs when an element gets focus
change Script that runs when the element value is changed

Commonly used events on mobile terminals:

Event name Function
click Fires when clicked (click)
load Triggered after the page has finished loading
scroll Script that runs when the element scroll bar is scrolled
blur Script that runs when the element loses focus
focus Script that runs when an element gets focus
change Script that runs when the element value is changed
input Replace keyup, keydown
touch event model Handling single-finger operations
gesture event model Handling multi-finger operations
Related recommendations:

Use native JS to encapsulate Tap events to solve the 300ms delay on the mobile terminal

Detailed introduction to JavaScript mobile terminal The basis of events and summary of commonly used event libraries

The above is the detailed content of Overview of click events and commonly used events in js (PC and mobile). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles