Skip to content

Commit

Permalink
Add Math.round for positions and dimensions (#454)
Browse files Browse the repository at this point in the history
* Add Math.round for transform position
  • Loading branch information
Dmitry Krekota authored and ValentinH committed Nov 9, 2016
1 parent e2f720a commit 62d857d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
6 changes: 3 additions & 3 deletions dist/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@
var tick = {
selected: self.isTickSelected(value),
style: {
transform: translate + '(' + position + 'px)'
transform: translate + '(' + Math.round(position) + 'px)'
}
};
if (tick.selected && self.options.getSelectionBarColor) {
Expand Down Expand Up @@ -1414,7 +1414,7 @@
setPosition: function(elem, pos) {
elem.rzsp = pos;
var css = {};
css[this.positionProperty] = pos + 'px';
css[this.positionProperty] = Math.round(pos) + 'px';
elem.css(css);
return pos;
},
Expand Down Expand Up @@ -1444,7 +1444,7 @@
setDimension: function(elem, dim) {
elem.rzsd = dim;
var css = {};
css[this.dimensionProperty] = dim + 'px';
css[this.dimensionProperty] = Math.round(dim) + 'px';
elem.css(css);
return dim;
},
Expand Down
4 changes: 4 additions & 0 deletions dist/rzslider.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions dist/rzslider.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/rzslider.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@
var tick = {
selected: self.isTickSelected(value),
style: {
transform: translate + '(' + position + 'px)'
transform: translate + '(' + Math.round(position) + 'px)'
}
};
if (tick.selected && self.options.getSelectionBarColor) {
Expand Down Expand Up @@ -1418,7 +1418,7 @@
setPosition: function(elem, pos) {
elem.rzsp = pos;
var css = {};
css[this.positionProperty] = pos + 'px';
css[this.positionProperty] = Math.round(pos) + 'px';
elem.css(css);
return pos;
},
Expand Down Expand Up @@ -1448,7 +1448,7 @@
setDimension: function(elem, dim) {
elem.rzsd = dim;
var css = {};
css[this.dimensionProperty] = dim + 'px';
css[this.dimensionProperty] = Math.round(dim) + 'px';
elem.css(css);
return dim;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@
it('should a working positionTrackingBar', function() {
var newMin = 50,
newMax = 90,
minposition = helper.slider.valueToPosition(newMin),
maxposition = helper.slider.valueToPosition(newMax);
minposition = Math.round(helper.slider.valueToPosition(newMin)),
maxposition = Math.round(helper.slider.valueToPosition(newMax));
helper.slider.positionTrackingBar(newMin, newMax, minposition, maxposition);

expect(helper.scope.slider.min).to.equal(50);
Expand Down Expand Up @@ -411,8 +411,8 @@
it('should a working positionTrackingBar', function() {
var newMin = 50,
newMax = 90,
minposition = helper.slider.valueToPosition(newMin),
maxposition = helper.slider.valueToPosition(newMax);
minposition = Math.round(helper.slider.valueToPosition(newMin)),
maxposition = Math.round(helper.slider.valueToPosition(newMax));
helper.slider.positionTrackingBar(newMin, newMax, minposition, maxposition);

expect(helper.scope.slider.min).to.equal(50);
Expand Down
46 changes: 23 additions & 23 deletions tests/specs/options-handling-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@
}
};
helper.createSlider(sliderConf);
var expectedDimension = helper.slider.valueToPosition(2) + helper.slider.handleHalfDim;
var expectedDimension = Math.round(helper.slider.valueToPosition(2)) + helper.slider.handleHalfDim;
expect(helper.slider.selBar.css('width')).to.equal(expectedDimension + 'px');
expect(helper.slider.selBar.css('left')).to.equal('0px');
});
Expand All @@ -375,7 +375,7 @@
actualDimension = Math.floor(helper.slider.selBar[0].getBoundingClientRect().width);
expect(actualDimension).to.equal(expectedDimension);

var expectedPosition = helper.slider.valueToPosition(2) + helper.slider.handleHalfDim;
var expectedPosition = Math.round(helper.slider.valueToPosition(2)) + helper.slider.handleHalfDim;
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});

Expand All @@ -389,8 +389,8 @@
}
};
helper.createSlider(sliderConf);
var expectedDimension = helper.slider.valueToPosition(5),
expectedPosition = helper.slider.valueToPosition(10) + helper.slider.handleHalfDim;
var expectedDimension = Math.round(helper.slider.valueToPosition(5)),
expectedPosition = Math.round(helper.slider.valueToPosition(10)) + helper.slider.handleHalfDim;
expect(helper.slider.selBar.css('width')).to.equal(expectedDimension + 'px');
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});
Expand All @@ -405,11 +405,11 @@
}
};
helper.createSlider(sliderConf);
var expectedDimension = Math.floor(helper.slider.valueToPosition(7)),
actualDimension = Math.floor(helper.slider.selBar[0].getBoundingClientRect().width);
var expectedDimension = Math.round(helper.slider.valueToPosition(7)),
actualDimension = Math.round(helper.slider.selBar[0].getBoundingClientRect().width);
expect(actualDimension).to.equal(expectedDimension);

var expectedPosition = helper.slider.valueToPosition(3) + helper.slider.handleHalfDim;
var expectedPosition = Math.round(helper.slider.valueToPosition(3)) + helper.slider.handleHalfDim;
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});

Expand Down Expand Up @@ -512,11 +512,11 @@
};
helper.createRangeSlider(sliderConf);

var expectedDimension = Math.floor(helper.slider.valueToPosition(6)),
actualDimension = Math.floor(helper.slider.selBar[0].getBoundingClientRect().width);
var expectedDimension = Math.round(helper.slider.valueToPosition(6)),
actualDimension = helper.slider.selBar[0].getBoundingClientRect().width;
expect(actualDimension).to.equal(expectedDimension);

var expectedPosition = helper.slider.valueToPosition(2) + helper.slider.handleHalfDim;
var expectedPosition = Math.round(helper.slider.valueToPosition(2)) + helper.slider.handleHalfDim;
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});

Expand Down Expand Up @@ -752,8 +752,8 @@
};

helper.createRangeSlider(sliderConf);
expect(helper.slider.minLab.css('left')).to.equal('-' + (helper.slider.minLab.rzsd / 2 - helper.slider.handleHalfDim) + 'px');
expect(helper.slider.maxLab.css('left')).to.equal((helper.slider.barDimension - (helper.slider.maxLab.rzsd / 2 + helper.slider.handleHalfDim)) + 'px');
expect(helper.slider.minLab.css('left')).to.equal('-' + Math.round(helper.slider.minLab.rzsd / 2 - helper.slider.handleHalfDim) + 'px');
expect(helper.slider.maxLab.css('left')).to.equal(Math.round((helper.slider.barDimension - (helper.slider.maxLab.rzsd / 2 + helper.slider.handleHalfDim))) + 'px');

sliderConf.max = 100000001;
helper.createRangeSlider(sliderConf);
Expand Down Expand Up @@ -1124,10 +1124,10 @@
}
};
helper.createSlider(sliderConf);
var expectedDimension = Math.floor(helper.slider.valueToPosition(8) + helper.slider.handleHalfDim),
actualDimension = Math.floor(helper.slider.selBar[0].getBoundingClientRect().width);
var expectedDimension = Math.round(helper.slider.valueToPosition(8) + helper.slider.handleHalfDim),
actualDimension = Math.round(helper.slider.selBar[0].getBoundingClientRect().width);
expect(actualDimension).to.equal(expectedDimension);
expect(helper.slider.selBar.css('left')).to.equal(helper.slider.valueToPosition(2) + helper.slider.handleHalfDim + 'px');
expect(helper.slider.selBar.css('left')).to.equal(Math.round(helper.slider.valueToPosition(2)) + helper.slider.handleHalfDim + 'px');
});

it('should set the correct dimension/position for selection bar for single slider with showSelectionBarEnd=true', function() {
Expand Down Expand Up @@ -1158,8 +1158,8 @@
}
};
helper.createSlider(sliderConf);
var expectedDimension = helper.slider.valueToPosition(15),
expectedPosition = helper.slider.valueToPosition(15) + helper.slider.handleHalfDim;
var expectedDimension = Math.round(helper.slider.valueToPosition(15)),
expectedPosition = Math.round(helper.slider.valueToPosition(15)) + helper.slider.handleHalfDim;
expect(helper.slider.selBar.css('width')).to.equal(expectedDimension + 'px');
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});
Expand All @@ -1175,9 +1175,9 @@
}
};
helper.createSlider(sliderConf);
var expectedDimension = Math.floor(helper.slider.valueToPosition(13)),
actualDimension = Math.floor(helper.slider.selBar[0].getBoundingClientRect().width),
expectedPosition = helper.slider.valueToPosition(10) + helper.slider.handleHalfDim;
var expectedDimension = Math.round(helper.slider.valueToPosition(13)),
actualDimension = helper.slider.selBar[0].getBoundingClientRect().width,
expectedPosition = Math.round(helper.slider.valueToPosition(10)) + helper.slider.handleHalfDim;
expect(actualDimension).to.equal(expectedDimension);
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});
Expand Down Expand Up @@ -1217,11 +1217,11 @@
};
helper.createRangeSlider(sliderConf);

var expectedDimension = Math.floor(helper.slider.valueToPosition(6)),
actualDimension = Math.floor(helper.slider.selBar[0].getBoundingClientRect().width);
var expectedDimension = Math.round(helper.slider.valueToPosition(6)),
actualDimension = helper.slider.selBar[0].getBoundingClientRect().width;
expect(actualDimension).to.equal(expectedDimension);

var expectedPosition = helper.slider.valueToPosition(2) + helper.slider.handleHalfDim;
var expectedPosition = Math.round(helper.slider.valueToPosition(2)) + helper.slider.handleHalfDim;
expect(helper.slider.selBar.css('left')).to.equal(expectedPosition + 'px');
});

Expand Down

0 comments on commit 62d857d

Please sign in to comment.