Skip to content

Instantly share code, notes, and snippets.

@jpmckinney
Created September 26, 2014 17:31
Show Gist options
  • Save jpmckinney/0d82bf62683f25f5c448 to your computer and use it in GitHub Desktop.
Save jpmckinney/0d82bf62683f25f5c448 to your computer and use it in GitHub Desktop.
rangeslider.js is not accessible (https://github.com/andreruffert/rangeslider.js/issues/47), its math is wrong in getPositionFromValue, and it treats 0 as falsy.
diff --git a/app/assets/javascripts/rangeslider.js b/app/assets/javascripts/rangeslider.js
index 1531b77..9637194 100644
--- a/app/assets/javascripts/rangeslider.js
+++ b/app/assets/javascripts/rangeslider.js
@@ -107,7 +107,7 @@
this.identifier = 'js-' + pluginName + '-' +(pluginIdentifier++);
this.min = parseFloat(this.$element[0].getAttribute('min') || 0);
this.max = parseFloat(this.$element[0].getAttribute('max') || 100);
- this.value = parseFloat(this.$element[0].value || this.min + (this.max-this.min)/2);
+ this.value = parseFloat(this.$element[0].getAttribute('value') || this.min + (this.max - this.min) / 2);
this.step = parseFloat(this.$element[0].getAttribute('step') || 1);
this.$fill = $('<div class="' + this.options.fillClass + '" />');
this.$handle = $('<div class="' + this.options.handleClass + '" />');
@@ -221,7 +221,7 @@
var value, left;
// Snapping steps
- value = (this.getValueFromPosition(this.cap(pos, 0, this.maxHandleX)) / this.step) * this.step;
+ value = this.getValueFromPosition(this.cap(pos, 0, this.maxHandleX));
left = this.getPositionFromValue(value);
// Update ui
@@ -256,16 +256,16 @@
Plugin.prototype.getPositionFromValue = function(value) {
var percentage, pos;
- percentage = (value - this.min)/(this.max - this.min);
+ percentage = (value - this.min) / (this.max - this.min);
pos = percentage * this.maxHandleX;
return pos;
};
Plugin.prototype.getValueFromPosition = function(pos) {
var percentage, value;
- percentage = ((pos) / (this.maxHandleX || 1));
- value = this.step * Math.ceil((((percentage) * (this.max - this.min)) + this.min) / this.step);
- return Number((value).toFixed(2));
+ percentage = pos / (this.maxHandleX || 1);
+ value = Math.round(percentage * (this.max - this.min) / this.step) * this.step + this.min;
+ return Number(value.toFixed(2));
};
Plugin.prototype.setValue = function(value) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment