Created
August 18, 2014 17:42
-
-
Save filippomangione/6160e0d27974a6908f41 to your computer and use it in GitHub Desktop.
Mousehold jQuery Event
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($) { | |
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