Longpress / longclick event support / plugin in jQuery

后端 未结 3 1037
离开以前
离开以前 2021-02-09 01:40

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

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-09 02:22

    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:

    
    

    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!

提交回复
热议问题