Created
September 30, 2012 02:20
-
-
Save zx1986/3805666 to your computer and use it in GitHub Desktop.
jQuery mousehold plugin
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
/** | |
* jQuery mousehold plugin - fires an event while the mouse is clicked down. | |
* Additionally, the function, when executed, is passed a single | |
* argument representing the count of times the event has been fired during | |
* this session of the mouse hold. | |
* | |
* @author Remy Sharp (leftlogic.com) | |
* @date 2006-12-15 | |
* @example $("img").mousehold(200, function(i){ }) | |
* @desc Repeats firing the passed function while the mouse is clicked down | |
* | |
* @name mousehold | |
* @type jQuery | |
* @param Number timeout The frequency to repeat the event in milliseconds | |
* @param Function fn A function to execute | |
* @cat Plugin | |
*/ | |
jQuery.fn.mousehold = function(timeout, f) { | |
if (timeout && typeof timeout == 'function') { | |
f = timeout; | |
timeout = 100; | |
} | |
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).mouseout(clearMousehold); | |
jQuery(this).mouseup(clearMousehold); | |
}) | |
} | |
} |
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: remy sharp | |
@date: 2006-12-15 | |
@description: step increment object | |
@version: $Id$ | |
*/ | |
var stepper = function(s, dp) { | |
// sanity watchers | |
if (s && typeof s == 'string') s = parseFloat(s) | |
if (dp && typeof dp == 'string') dp = parseFloat(dp) | |
if (arguments.length == 1) dp = -1 | |
this.s = s // step increment (aka pip) | |
this.dp = dp // decimal places to keep | |
this.running = true; | |
this.validate() | |
return this | |
} | |
stepper.prototype = { | |
validate: function() { | |
if (parseFloat(this.setDP(this.s)) == 0) { | |
alert("The decimal places cannot be shorter than the PIP.\ndp = " + this.dp + ", pip = " + this.s) | |
} | |
}, | |
mul: function() { return Math.pow(10, this.dp == -1 ? 1 : this.dp) }, | |
upToInt: function(n) { | |
return Math.round(n * this.mul()) // handle dp recursion | |
}, | |
downToFloat: function(n) { | |
return (n / this.mul()) | |
}, | |
setDP: function(n) { | |
var r = n.toString() | |
// -1 on dp means leave as is | |
if (this.dp == -1) return r | |
// handle whole numbers | |
if (this.dpLen(r) == -1 && this.dp > 0) r += ".0" | |
else if (this.dpLen(r) == -1 && this.dp == 0) return r | |
// handle fractions | |
if (this.dpLen(r) > this.dp) { // strip | |
var i = r.indexOf('.') | |
r = r.substring(0, i) + '.' + r.substring(i + 1, i + 1 + this.dp) | |
} else { // add | |
while (this.dpLen(r) < this.dp) r += "0" | |
} | |
return r | |
}, | |
dpLen: function(n) { | |
if (n.indexOf('.') == -1) | |
return -1 | |
else | |
return (n.length) - (n.indexOf('.') + 1) | |
}, | |
step: function(n) { | |
if (arguments.length) { | |
if (arguments[0] && typeof arguments[0] == 'string') { | |
this.n = parseFloat(arguments[0]) | |
} else if (arguments[0] == NaN) { | |
this.n = 0 | |
} else { | |
this.n = arguments[0] | |
} | |
} else { | |
alert("stepper::step expects a float value") | |
return | |
} | |
var n = this.upToInt(this.n) | |
var s = this.upToInt(this.s) | |
var r = this.setDP(this.downToFloat(n - (n % s) + s)) | |
return r | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://remysharp.com/demo/pip_ticker.html
http://remysharp.com/2006/12/15/jquery-mousehold-event/