@@ -340,17 +340,9 @@ $.widget("ui.slider", $.ui.mouse, {
340
340
percentMouse = 1 - percentMouse ;
341
341
342
342
var valueTotal = this . _valueMax ( ) - this . _valueMin ( ) ,
343
- valueMouse = percentMouse * valueTotal ,
344
- valueMouseModStep = valueMouse % this . options . step ,
345
- normValue = this . _valueMin ( ) + valueMouse - valueMouseModStep ;
346
-
347
- if ( valueMouseModStep > ( this . options . step / 2 ) )
348
- normValue += this . options . step ;
349
-
350
- // Since JavaScript has problems with large floats, round
351
- // the final value to 5 digits after the decimal point (see #4124)
352
- return parseFloat ( normValue . toFixed ( 5 ) ) ;
343
+ valueMouse = percentMouse * valueTotal ;
353
344
345
+ return this . _trimAlignValue ( valueMouse ) ;
354
346
} ,
355
347
356
348
_start : function ( event , index ) {
@@ -440,7 +432,7 @@ $.widget("ui.slider", $.ui.mouse, {
440
432
value : function ( newValue ) {
441
433
442
434
if ( arguments . length ) {
443
- this . options . value = this . _trimValue ( newValue ) ;
435
+ this . options . value = this . _trimAlignValue ( newValue ) ;
444
436
this . _refreshValue ( ) ;
445
437
this . _change ( null , 0 ) ;
446
438
}
@@ -452,7 +444,7 @@ $.widget("ui.slider", $.ui.mouse, {
452
444
values : function ( index , newValue ) {
453
445
454
446
if ( arguments . length > 1 ) {
455
- this . options . values [ index ] = this . _trimValue ( newValue ) ;
447
+ this . options . values [ index ] = this . _trimAlignValue ( newValue ) ;
456
448
this . _refreshValue ( ) ;
457
449
this . _change ( null , index ) ;
458
450
}
@@ -461,7 +453,7 @@ $.widget("ui.slider", $.ui.mouse, {
461
453
if ( $ . isArray ( arguments [ 0 ] ) ) {
462
454
var vals = this . options . values , newValues = arguments [ 0 ] ;
463
455
for ( var i = 0 , l = vals . length ; i < l ; i ++ ) {
464
- vals [ i ] = this . _trimValue ( newValues [ i ] ) ;
456
+ vals [ i ] = this . _trimAlignValue ( newValues [ i ] ) ;
465
457
this . _change ( null , i ) ;
466
458
}
467
459
this . _refreshValue ( ) ;
@@ -528,9 +520,9 @@ $.widget("ui.slider", $.ui.mouse, {
528
520
529
521
_value : function ( ) {
530
522
//internal value getter
531
- // _value() returns value trimmed by min and max
523
+ // _value() returns value trimmed by min and max, aligned by step
532
524
var val = this . options . value ;
533
- val = this . _trimValue ( val ) ;
525
+ val = this . _trimAlignValue ( val ) ;
534
526
535
527
return val ;
536
528
} ,
@@ -542,30 +534,41 @@ $.widget("ui.slider", $.ui.mouse, {
542
534
543
535
if ( arguments . length ) {
544
536
var val = this . options . values [ index ] ;
545
- val = this . _trimValue ( val ) ;
537
+ val = this . _trimAlignValue ( val ) ;
546
538
547
539
return val ;
548
540
} else {
549
541
// .slice() creates a copy of the array
550
542
// this copy gets trimmed by min and max and then returned
551
543
var vals = this . options . values . slice ( ) ;
552
544
for ( var i = 0 , l = vals . length ; i < l ; i ++ ) {
553
- vals [ i ] = this . _trimValue ( vals [ i ] ) ;
545
+ vals [ i ] = this . _trimAlignValue ( vals [ i ] ) ;
554
546
}
555
547
556
548
return vals ;
557
549
}
558
550
559
551
} ,
560
552
561
- _trimValue : function ( val ) {
553
+ // returns the step-aligned value that val is closest to, between (inclusive) min and max
554
+ _trimAlignValue : function ( val ) {
562
555
if ( val < this . _valueMin ( ) ) {
563
556
return this . _valueMin ( ) ;
564
557
}
565
558
if ( val > this . _valueMax ( ) ) {
566
559
return this . _valueMax ( ) ;
567
560
}
568
- return val ;
561
+ var step = this . options . step ,
562
+ valModStep = val % step ,
563
+ alignValue = this . _valueMin ( ) + val - valModStep ;
564
+
565
+ if ( valModStep >= ( step / 2 ) ) {
566
+ alignValue += step ;
567
+ }
568
+
569
+ // Since JavaScript has problems with large floats, round
570
+ // the final value to 5 digits after the decimal point (see #4124)
571
+ return parseFloat ( alignValue . toFixed ( 5 ) ) ;
569
572
} ,
570
573
571
574
_valueMin : function ( ) {
0 commit comments