Skip to content

Instantly share code, notes, and snippets.

@amasad
Created March 19, 2011 00:28
Show Gist options
  • Save amasad/877078 to your computer and use it in GitHub Desktop.
Save amasad/877078 to your computer and use it in GitHub Desktop.
Dwell click jQuery plugin
//dwell click plugin
//features:
//1-Adds new dwellClick event using a defined time delay
//2-optionally make the dwell click act like a regular click, by firing the browser click event
//3-Shortcut method $.dwell() well instantiate dwell on body element using
//usage:
// $(selector).dwell([delay = 500, click = false])
// delay : time to wait with the mouse idle to trigger dwell click
// click : trigger original click event
//
// examples: $('body').dwell(true) //dwellify elements with in the body
// $('#main-container').dwell(1000, true) //dwellify elements and make them trigger click events]
//
//element events usage:
// $(selector).bind('dwellCick', function(){
// //do some great stuff for less fortunate people ^^
// });
//OR
// $(selector).dwellClick(function(){
// //do some great stuff for less fortunate people ^^
// });
(function($){
var timeout;
$.fn.dwell = function(delay, click){
if (typeof delay === "boolean"){
click = delay;
}
if (typeof delay !== "number")
delay = 500;
//when we want to act like regular clicks make links respond on dwellclick events
if (click)
$('a').bind('dwellClick', function(){window.location.href = $(this).attr('href')});
return this.each(function(){
$(this).mousemove(function(e){
if (timeout){
clearTimeout(timeout);
}
timeout = setTimeout(function(){
$target = $(e.target);
$target.trigger('dwellClick');
if (click){
$target.trigger('click');
}
},delay);
});
});
}
//add some sugar
$.extend({
'dwell' : function(delay, click){
$('body').dwell(delay, click);
}
});
$.fn.dwellClick = function(fn){
return this.each(function(){
$(this).bind('dwellClick', fn);
})
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment