Home Web Front-end JS Tutorial jQuery implements long press button trigger event_jquery

jQuery implements long press button trigger event_jquery

May 16, 2016 pm 04:16 PM
jquery button method trigger event Press

The example in this article describes how jQuery implements long-press button triggering events. Share it with everyone for your reference. The specific analysis is as follows:

The rapid development of mobile phones now requires many mobile phone gestures to be made into mobile web pages

There are many long-press button plug-ins on the Internet, and even jQuery Mobile has long-press events

But due to various compatibility issues,

Only use jquery to implement the long press action, which can maintain strong compatibility between mobile phones and computers

1. Basic goals

Create a button, which is essentially a layer with a gray background of 100x100px. If you press and hold it for 2 seconds, the text in the layer will change from in to out. As shown below:

2. Production process

The code is as follows:

Copy code The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>

</head>

<body>
<div id="mydiv" style="width:100px; height:100px; background:#ddd;">out</div>
</body>
</html>

<script>
/*Set a long press timer. If this layer is clicked for more than 2 seconds, it will be triggered. The text in mydiv will change from out to in*/
var timeout;

$("#mydiv").mousedown(function() {
Timeout = setTimeout(function() {
         $("#mydiv").text("in");
}, 2000);
});

$("#mydiv").mouseup(function() {
ClearTimeout(timeout);
$("#mydiv").text("out");
});

$("#mydiv").mouseout(function() {
ClearTimeout(timeout);
$("#mydiv").text("out");
});

</script>

In essence, the long press time should not be too long, because this may conflict with some long press gestures of the mobile phone system, but it should not be too short, because there is no difference between a long press time and a click,

Theoretically, to determine the end of a long press, just set the mouseup action on the mobile phone,

However, only setting mouseup on PC will have the following bugs:

Dark the text on the layer while long pressing, and then drag the mouse out of the layer, you can avoid the system mouseup judgment. Of course, this action cannot be realized on the mobile phone. If it is completely written to For web pages on mobile phones, this step can be completely ignored. However, for better compatibility, the mouseout action is added to correct this bug.

I hope this article will be helpful to everyone’s jQuery programming.

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 Article Tags

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)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

How to enter bios on Colorful motherboard? Teach you two methods

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Summary of methods to obtain administrator rights in Win11 Summary of methods to obtain administrator rights in Win11 Mar 09, 2024 am 08:45 AM

Summary of methods to obtain administrator rights in Win11

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

Detailed explanation of Oracle version query method Detailed explanation of Oracle version query method Mar 07, 2024 pm 09:21 PM

Detailed explanation of Oracle version query method

Why won't my laptop start up after pressing the power button? Why won't my laptop start up after pressing the power button? Mar 10, 2024 am 09:31 AM

Why won't my laptop start up after pressing the power button?

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

See all articles