Skip to content

Instantly share code, notes, and snippets.

@filippomangione
Created August 18, 2014 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filippomangione/6160e0d27974a6908f41 to your computer and use it in GitHub Desktop.
Save filippomangione/6160e0d27974a6908f41 to your computer and use it in GitHub Desktop.
Mousehold jQuery Event
(function($) {
function startTrigger(e,data) {
var $elem = $(this);
$elem.data('mouseheld_timeout', setTimeout(function() {
$elem.trigger('mouseheld');
}, e.data));
}
function stopTrigger() {
var $elem = $(this);
clearTimeout($elem.data('mouseheld_timeout'));
}
var mouseheld = $.event.special.mouseheld = {
setup: function(data) {
var $this = $(this);
$this.bind('mousedown', +data || mouseheld.time, startTrigger);
$this.bind('mouseleave mouseup', stopTrigger);
},
teardown: function() {
var $this = $(this);
$this.unbind('mousedown', startTrigger);
$this.unbind('mouseleave mouseup', stopTrigger);
},
time: 750 // default to 750ms
};
})(jQuery);
$("div").bind('mouseheld', function(e) {
console.log('Held', e);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment