Добрый вечер!
Иногда необходимо выполнять какие либо функции при нажатии и удерживании клавиши мыши. Вот решение этого дополнение к jQuery.
jQuery.fn.mousehold = function(timeout, f) {
if (f && typeof f == 'function') {
var timer = 0; var fireStep = 0;
return this.each(function() {
jQuery(this).mousedown(function() {
fireStep = 1;
var ctr = 0;
var t = this;
timer = setInterval(function() {
ctr++; f.call(t, ctr); fireStep = 2;
}, timeout);
});
clearMousehold = function() {
clearInterval(timer);
if (fireStep == 1)
f.call(this, 1); fireStep = 0;
}
jQuery(this).mouseleave(clearMousehold);
jQuery(this).mouseout(clearMousehold);
jQuery(this).mouseup(clearMousehold);
});
}
}
использовать легко и просто
$("input").mousehold(10, function() { alert("Нажата и удерживается клавиша мыши!") });