Skip to content

Commit ae1d6d5

Browse files
dekajpscottgonzalez
authored andcommitted
Slider: Fix max calculation, when step is float
Fixes #10721 Closes jquerygh-1398
1 parent a3b43ee commit ae1d6d5

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

tests/unit/slider/slider_options.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test( "disabled", function(){
4040
});
4141

4242
test( "max", function() {
43-
expect( 4 );
43+
expect( 5 );
4444
element = $( "<div></div>" );
4545

4646
options = {
@@ -72,6 +72,18 @@ test( "max", function() {
7272
ok( element.slider( "value" ) === options.max, "value method will max, step is changed" );
7373
element.slider( "destroy" );
7474

75+
options = {
76+
max: 60,
77+
min: 50,
78+
orientation: "horizontal",
79+
step: 0.1,
80+
value: 60
81+
};
82+
83+
element.slider( options );
84+
ok( element.slider( "value" ) === options.max, "value method will max, step is changed and step is float" );
85+
element.slider( "destroy" );
86+
7587
});
7688

7789
test( "min", function() {

ui/slider.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,26 @@ return $.widget( "ui.slider", $.ui.mouse, {
552552
},
553553

554554
_calculateNewMax: function() {
555-
var remainder = ( this.options.max - this._valueMin() ) % this.options.step;
556-
this.max = this.options.max - remainder;
555+
var max = this.options.max,
556+
min = this._valueMin(),
557+
step = this.options.step,
558+
aboveMin = Math.floor( ( max - min ) / step ) * step;
559+
max = aboveMin + min;
560+
this.max = parseFloat( max.toFixed( this._precision() ) );
561+
},
562+
563+
_precision: function() {
564+
var precision = this._precisionOf( this.options.step );
565+
if ( this.options.min !== null ) {
566+
precision = Math.max( precision, this._precisionOf( this.options.min ) );
567+
}
568+
return precision;
569+
},
570+
571+
_precisionOf: function( num ) {
572+
var str = num.toString(),
573+
decimal = str.indexOf( "." );
574+
return decimal === -1 ? 0 : str.length - decimal - 1;
557575
},
558576

559577
_valueMin: function() {

0 commit comments

Comments
 (0)