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

jQuery implements long press button trigger event_jquery

WBOY
Release: 2016-05-16 16:16:14
Original
1690 people have browsed it

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:




Untitled Document





out




<script> <br> /*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*/ <br> var timeout; <br> <br> $("#mydiv").mousedown(function() { <br> Timeout = setTimeout(function() { <br>          $("#mydiv").text("in"); <br> }, 2000); <br> }); <br> <br> $("#mydiv").mouseup(function() { <br> ClearTimeout(timeout); <br> $("#mydiv").text("out"); <br> }); <br> <br> $("#mydiv").mouseout(function() { <br> ClearTimeout(timeout); <br> $("#mydiv").text("out"); <br> }); <br> <br> </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.

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