Last active
December 14, 2015 23:49
-
-
Save tx2z/5168797 to your computer and use it in GitHub Desktop.
Capture simple, double, triple, long and right clicks in the same element with jQuery - http://jesus.perezpaz.es/2013/01/capture-simple-double-triple-long-and-right-clicks-in-the-same-element-with-jquery/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
// Usefull variables | |
var clicks = 0; | |
var longclick = false; | |
// Change the long click duration (in ms): | |
//jQuery.longclick.duration = 500; | |
$(document).ready(function() { | |
// Event click handlers | |
$('a').bind({ | |
click: function(){ // Left (normal) click | |
if (longclick == true) { | |
longclick = false; | |
}else { | |
clicks++; | |
if (clicks == 1) { | |
setTimeout(function() { | |
if (clicks == 1) { // One click | |
$('.message').html('click'); | |
} else if (clicks == 2) { // Double click | |
$('.message').html('dblclick'); | |
} else { // Triple click | |
$('.message').html('tripleclick'); | |
} | |
clicks = 0; | |
}, 500); | |
} | |
} | |
}, | |
contextmenu: function(){ // Right click | |
$('.message').html('left-click'); | |
return false; // The retun false avoid show the context menu | |
}, | |
longclick: function(){ // Long click | |
$('.message').html('long click'); | |
longclick = true; // Stop the "normal" click to execute | |
} | |
}); | |
}); | |
})(window.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment