Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix bug with min/max threshold and keyboard input #11

Merged
merged 7 commits into from
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 8 deletions.
  1. +1 −1  index.html
  2. +24 −7 js/jquery.knob-1.1.0.js
View
2  index.html
@@ -70,7 +70,7 @@
data-width="250"
data-min="-100"
</pre>
- <input class="knob"data-width="250" data-min="-100" value="44">
+ <input class="knob"data-width="250" data-min="-100" data-max="100"value="44">
</div>
<div style="float:left;width:320px;height:300px;padding:20px">
<pre>
View
31 js/jquery.knob-1.1.0.js
@@ -162,9 +162,10 @@ $(function() {
};
k.val( parseInt($this.val()) || 0 );
k.onChange = function(v) {
- $this.val(v);
- opt.change(v);
- };
+ var limitedValue = limitValue(v);
+ $this.val(limitedValue);
+ opt.change(limitedValue);
+ };
// bind change on input
$this.bind(
@@ -188,8 +189,18 @@ $(function() {
var keys={37: -1, 38:1, 39:1, 40: -1}
$this.keydown(function(event){
- setVal( keys[event.keyCode]);
- event.preventDefault();
+ var direction = keys[event.keyCode];
+ if(direction){
+ setVal(direction);
+ event.preventDefault();
+ }
+ });
+
+ $this.keyup(function(event){
+ var direction = keys[event.keyCode];
+ if(!direction) {
+ setVal();
+ }
});
$this.bind('mousewheel DOMMouseScroll', function(event){
@@ -201,10 +212,16 @@ $(function() {
});
function setVal(dir){
- if(dir){
- k.val( parseInt($this.val()) + dir );
+ if(!opt.readOnly) {
+ var value = limitValue(parseInt($this.val(), 10) + (dir ? dir: 0));
+ $this.val(value)
+ k.val( parseInt(value, 10) || 0);
}
}
+
+ function limitValue(v){
+ return limitedValue = Math.max(Math.min(v, opt.max), opt.min);
+ }
}
).parent();
}
Something went wrong with that request. Please try again.