Skip to content

Commit 6833a31

Browse files
dekajptjvantoll
authored andcommitted
Slider: Don't allow a slider's value to exceed its max
Fixes #9376 Closes jquerygh-1016
1 parent d85016a commit 6833a31

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

tests/unit/slider/slider_options.js

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

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

4646
options = {
@@ -52,8 +52,24 @@ test( "max", function() {
5252
};
5353

5454
element.slider( options );
55-
ok(element.slider( "option", "value" ) === options.value, "value option is not contained by max" );
56-
ok(element.slider( "value" ) === options.max, "value method is contained by max" );
55+
ok( element.slider( "option", "value" ) === options.value, "value option is not contained by max" );
56+
ok( element.slider( "value" ) === options.max, "value method is contained by max" );
57+
58+
options = {
59+
max: 9,
60+
min: 1,
61+
orientation: "horizontal",
62+
step: 3,
63+
value: 8.75
64+
};
65+
66+
element.slider( options );
67+
ok( element.slider( "value" ) === 7, "value method is within max, edge Case" );
68+
69+
options.step = 2;
70+
71+
element.slider( options );
72+
ok( element.slider( "value" ) === options.max, "value method will max, step is changed" );
5773
element.slider( "destroy" );
5874

5975
});

ui/slider.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ return $.widget( "ui.slider", $.ui.mouse, {
5858
this._handleIndex = null;
5959
this._detectOrientation();
6060
this._mouseInit();
61+
this._calculateNewMax();
6162

6263
this.element
6364
.addClass( "ui-slider" +
@@ -472,9 +473,11 @@ return $.widget( "ui.slider", $.ui.mouse, {
472473
}
473474
this._animateOff = false;
474475
break;
476+
case "step":
475477
case "min":
476478
case "max":
477479
this._animateOff = true;
480+
this._calculateNewMax();
478481
this._refreshValue();
479482
this._animateOff = false;
480483
break;
@@ -543,12 +546,17 @@ return $.widget( "ui.slider", $.ui.mouse, {
543546
return parseFloat( alignValue.toFixed(5) );
544547
},
545548

549+
_calculateNewMax: function() {
550+
var remainder = ( this.options.max - this._valueMin() ) % this.options.step;
551+
this.max = this.options.max - remainder;
552+
},
553+
546554
_valueMin: function() {
547555
return this.options.min;
548556
},
549557

550558
_valueMax: function() {
551-
return this.options.max;
559+
return this.max;
552560
},
553561

554562
_refreshValue: function() {

0 commit comments

Comments
 (0)