-
-
Save asdf072/5032954 to your computer and use it in GitHub Desktop.
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
This fork of single_double_click is safe for .draggable()/.droppable()/.sortable(). It adds a second timer to keep track of drag time. | |
Code: | |
$(function(){ | |
$("button").singleDoubleClick({ | |
singleCallback: function(){ alert("You clicked once") }, | |
doubleCallback: function(){ alert("You clicked twice") } | |
}); | |
}); | |
Markup | |
<button>Click Me!</button> |
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
// Author: Jacek Becela | |
// Source: http://gist.github.com/399624 | |
// License: MIT | |
(function(jQuery){ | |
jQuery.fn.singleDoubleClick = function(options) { | |
return this.each(function(){ | |
var defaults = { | |
singleCallback: function(){}, | |
doubleCallback: function(){}, | |
dragTimeout: 600, | |
dblClickTimeout: 300 | |
}; | |
var opts = jQuery.extend(defaults, options); | |
var clicks = 0, self = this, ms = 0, timer; | |
jQuery(this).mousedown(function(event){ | |
ms = 0; | |
timer = setInterval(function(){ms += 50}, 50); | |
}); | |
jQuery(this).mouseup(function(){clearInterval(timer)}); | |
jQuery(this).click(function(event){ | |
clicks++; | |
if (clicks == 1) { | |
setTimeout(function(){ | |
if(ms > opts.dragTimeout){ | |
// add a drag timeout callback if needed | |
}else if(clicks == 1){ | |
opts.singleCallback.call(self, event); | |
} else { | |
opts.doubleCallback.call(self, event); | |
} | |
clicks = 0; | |
}, opts.dblClickTimeout); | |
} | |
}); | |
}); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment