开发者

Having both mousedown/mouseup and dblclick in jQuery

开发者 https://www.devze.com 2022-12-18 06:21 出处:网络
Whenever a mousedown or mouseup handler is attached to an element the dblclick cannot be attached (won\'t work if attached), though this seems fair enough is t开发者_如何转开发here any way to reinstat

Whenever a mousedown or mouseup handler is attached to an element the dblclick cannot be attached (won't work if attached), though this seems fair enough is t开发者_如何转开发here any way to reinstate a dblclick functionality without rewriting it from scratch (sigh...) Or am I missing something about events propagation?


It works - place this code in Firebug on this very page and you'll see it working (try double clicking on the text of your question):

($('.post-text')
    .mousedown(function () { console.log('down'); })
    .mouseup(function () { console.log('up'); })
    .dblclick(function () { console.log('dbclick'); }));

Don't have Firebug? Go grab it, I'll wait!


The event.detail parameter will count how many consecutive clicks you had so its possible to use that to detect that a double click has happened.

$('#mybutton').on('click',function(e) {
  console.log("User left clicked!"); // do select code
  if (e.button == 0 && e.detail == 2) {
    console.log("User left doubleclicked!"); // do action code
  }
});


Though this question is really old I want to show how I managed with this.

This block won't work:

        $(element).on('mousedown', function(event) {
            ...
        });

        $(element).on('dblclick', function(event) {
            ...
        });

But you can use this snippet to create the same behavior handler:

        var clicks = 0, delay = 400;
        $(element).on('mousedown', function(event) {
            event.preventDefault();
            clicks++;

            setTimeout(function() {
                clicks = 0;
            }, delay);

            if (clicks === 2) {
                // double click event handler should be here
                clicks = 0;
                return;
            } else {
                // mousedown event handler should be here
            }
        });


Your mousedown and mouseup cannot:

  1. do e.stopPropagate etc.
  2. return false

and it should work if Your code doesn't have a conflict between them methods

0

精彩评论

暂无评论...
验证码 换一张
取 消