最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Longpresslongclick event supportplugin in jQuery - Stack Overflow

programmeradmin2浏览0评论

I'm working on a website which requires mouseover menu's. I would not recommend mouseover menu's from an accessibility point of view, but it's pretty easy to implement using jQuery.

The problem: we also need to support touchscreen devices (tablets). On such a device you don't have a mouse and, so the mouseover event is not working. I was hoping for jQuery to have a longpress event, but it doesn't. I did find a jQuery longclick plugin using Google, but it was for jQuery 1.4, so I'm not keen on using that. Also the jQuery plugin site is under maintenance at the moment, so that is not very helpful.

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

I'm working on a website which requires mouseover menu's. I would not recommend mouseover menu's from an accessibility point of view, but it's pretty easy to implement using jQuery.

The problem: we also need to support touchscreen devices (tablets). On such a device you don't have a mouse and, so the mouseover event is not working. I was hoping for jQuery to have a longpress event, but it doesn't. I did find a jQuery longclick plugin using Google, but it was for jQuery 1.4, so I'm not keen on using that. Also the jQuery plugin site is under maintenance at the moment, so that is not very helpful.

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

Share Improve this question edited Sep 12, 2012 at 6:46 Jasper de Vries asked Aug 27, 2012 at 14:51 Jasper de VriesJasper de Vries 20.2k6 gold badges67 silver badges107 bronze badges 4
  • 1 using a combination of mousedown, mouseup, setTimeout and clearTimeout, it should be relatively straightforward to create a custom longpress event. have you tried anything yourself? – jackwanders Commented Aug 27, 2012 at 15:11
  • Not yet. I was hoping for a good, tested plugin. If there aren't any I think I'll write one. – Jasper de Vries Commented Aug 27, 2012 at 15:17
  • 1 you could check if user accesses site via mobile using "User-Agent" (php ex: $_SERVER['HTTP_USER_AGENT']) and if it's true add jquerymobile – Andrei R Commented Aug 27, 2012 at 15:23
  • Thanks for all the feedback. I think I'll take a look at the longclick plugin for jQuery 1.4 first and see if I get it working in jQuery 1.8. – Jasper de Vries Commented Aug 28, 2012 at 7:23
Add a comment  | 

3 Answers 3

Reset to default 7

It turns out that 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>

Something you could do is use delayed checks with setTimeout during the various mouse events. Incorporating jQuery's $.data() to store the timeout across events (on each element) should help you accomplish it all. Here's 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's a lot more to do with this, since you're also incorporating hovering, but for the most part, I think this does what you want.

It could easily be converted to a plugin if necessary, otherwise I think it could work fine alone. I hope this helps though!

You could time it.

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;
}
发布评论

评论列表(0)

  1. 暂无评论