Support for events / plugins Longpress / longclick in jQuery

I am working on a website that requires a cursor menu. I would not recommend the navigation menu in terms of accessibility, but it is pretty easy to implement using jQuery.

Problem: We also need to support touch devices (tablets). You don’t have a mouse on such a device, and therefore the mouseover event does not work. I was hoping jQuery would have a longpress event, but that is not the case. I found the jQuery longclick plugin using google, but that was for jQuery 1.4, so I don't want to use this. Also, the jQuery plugin site is currently under maintenance, so this is not very useful.

So the question is: is there elegant jQuery 1.7 / 1.8 to support longpress / longclick events?

+6
source share
3 answers

Turns out you can just use the existing longclick plugin for jQuery 1.4 with jQuery 1.8.

$("#area").mousedown(function(){ $("#result").html("Waiting for it..."); }); $("#area").longclick(500, function(){ $("#result").html("You longclicked. Nice!"); }); $("#area").click(function(){ $("#result").html("You clicked. Bummer."); }); 
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script src="http://rawgit.com/pisi/Longclick/master/jquery.longclick-min.js"></script> <p id="area">Click me!</p> <p id="result">You didn't click yet.</p> 
+6
source

Something you could do is use delayed checks using setTimeout during various mouse events. Enabling jQuery $.data() to store a timeout between events (for each element) should help you do all this. Here is an example:

HTML:

 <ul id="menu"> <li><a href="#" onclick="return false;" class="test"></a></li> <li><a href="#" onclick="return false;" class="test"></a></li> </ul> 

JS:

 var mousepress_time = 1000; // Maybe hardcode this value in setTimeout var orig_message = "Click Here"; // Remove this line var held_message = "Down"; // Remove this line $(document).ready(function () { $(".test") .html(orig_message) // Remove this line .on("mousedown", function () { console.log("#########mousedown"); // Remove this line var $this = $(this); $(this).data("checkdown", setTimeout(function () { // Add your code to run $this.html(held_message); // Remove this line }, mousepress_time)); }).on("mouseup", function () { clearTimeout($(this).data("checkdown")); console.log("#######mouseup"); // Remove this line $(this).html(orig_message); // Remove this line }).on("mouseout", function () { clearTimeout($(this).data("checkdown")); console.log("#######mouseout"); // Remove this line $(this).html(orig_message); // Remove this line }); }); 

DEMO : http://jsfiddle.net/7jKYa/10/

There is a lot more to do with this, since you also turn on the hang, but for the most part I think it does what you want.

It can be easily converted to a plugin if necessary, otherwise I think that it can work alone. Hope this helps!

+4
source

You could have time.

 function onImageMouseDown(e){ var d = new Date(); md_time = d.getTime(); // Milliseconds since 1 Apr 1970 } function onImageMouseUp(e){ var d = new Date(); var long_click = (d.getTime()-md_time)>1000; if (long_click){ // Click lasted longer than 1s (1000ms) }else{ // Click lasted less than 1s } md_time = 0; } 
+1
source

Source: https://habr.com/ru/post/923873/


All Articles