Add int() helper instead of parseInt.

parseInt is a good option for string-int conversion,
but none for float-int conversion because of performance issue.

This is also to make the code clean.
This commit is contained in:
Hyunje Alex Jun 2014-10-13 22:27:35 +01:00
parent 36c988faba
commit 337c0be9e2
2 changed files with 25 additions and 16 deletions

View File

@ -27,7 +27,7 @@
"asi" : false, "asi" : false,
"laxbreak" : false, "laxbreak" : false,
"bitwise" : true, "bitwise" : false,
"boss" : false, "boss" : false,
"curly" : true, "curly" : true,
"eqeqeq" : true, "eqeqeq" : true,

View File

@ -17,6 +17,15 @@
}(function ($) { }(function ($) {
'use strict'; 'use strict';
// helper functions
function int(x) {
if (typeof x === 'string') {
return parseInt(x, 10);
} else {
return ~~x;
}
}
// The default settings for the plugin // The default settings for the plugin
var defaultSettings = { var defaultSettings = {
wheelSpeed: 1, wheelSpeed: 1,
@ -92,18 +101,18 @@
var contentHeight; var contentHeight;
var scrollbarXWidth; var scrollbarXWidth;
var scrollbarXLeft; var scrollbarXLeft;
var scrollbarXBottom = parseInt($scrollbarXRail.css('bottom'), 10); var scrollbarXBottom = int($scrollbarXRail.css('bottom'));
var isScrollbarXUsingBottom = scrollbarXBottom === scrollbarXBottom; // !isNaN var isScrollbarXUsingBottom = scrollbarXBottom === scrollbarXBottom; // !isNaN
var scrollbarXTop = isScrollbarXUsingBottom ? null : parseInt($scrollbarXRail.css('top'), 10); var scrollbarXTop = isScrollbarXUsingBottom ? null : int($scrollbarXRail.css('top'));
var scrollbarYHeight; var scrollbarYHeight;
var scrollbarYTop; var scrollbarYTop;
var scrollbarYRight = parseInt($scrollbarYRail.css('right'), 10); var scrollbarYRight = int($scrollbarYRail.css('right'));
var isScrollbarYUsingRight = scrollbarYRight === scrollbarYRight; // !isNaN var isScrollbarYUsingRight = scrollbarYRight === scrollbarYRight; // !isNaN
var scrollbarYLeft = isScrollbarYUsingRight ? null : parseInt($scrollbarYRail.css('left'), 10); var scrollbarYLeft = isScrollbarYUsingRight ? null : int($scrollbarYRail.css('left'));
var isRtl = $this.css('direction') === "rtl"; var isRtl = $this.css('direction') === "rtl";
var eventClassName = getEventClassName(); var eventClassName = getEventClassName();
var railBorderXWidth = parseInt($scrollbarXRail.css('borderLeftWidth'), 10) + parseInt($scrollbarXRail.css('borderRightWidth'), 10); var railBorderXWidth = int($scrollbarXRail.css('borderLeftWidth')) + int($scrollbarXRail.css('borderRightWidth'));
var railBorderYWidth = parseInt($scrollbarXRail.css('borderTopWidth'), 10) + parseInt($scrollbarXRail.css('borderBottomWidth'), 10); var railBorderYWidth = int($scrollbarXRail.css('borderTopWidth')) + int($scrollbarXRail.css('borderBottomWidth'));
var updateContentScrollTop = function (currentTop, deltaY) { var updateContentScrollTop = function (currentTop, deltaY) {
var newTop = currentTop + deltaY; var newTop = currentTop + deltaY;
@ -119,7 +128,7 @@
scrollbarYTop = newTop; scrollbarYTop = newTop;
} }
var scrollTop = parseInt(scrollbarYTop * (contentHeight - containerHeight) / (containerHeight - scrollbarYHeight), 10); var scrollTop = int(scrollbarYTop * (contentHeight - containerHeight) / (containerHeight - scrollbarYHeight));
$this.scrollTop(scrollTop); $this.scrollTop(scrollTop);
}; };
@ -137,7 +146,7 @@
scrollbarXLeft = newLeft; scrollbarXLeft = newLeft;
} }
var scrollLeft = parseInt(scrollbarXLeft * (contentWidth - containerWidth) / (containerWidth - scrollbarXWidth), 10); var scrollLeft = int(scrollbarXLeft * (contentWidth - containerWidth) / (containerWidth - scrollbarXWidth));
$this.scrollLeft(scrollLeft); $this.scrollLeft(scrollLeft);
}; };
@ -210,8 +219,8 @@
if (!settings.suppressScrollX && containerWidth + settings.scrollXMarginOffset < contentWidth) { if (!settings.suppressScrollX && containerWidth + settings.scrollXMarginOffset < contentWidth) {
scrollbarXActive = true; scrollbarXActive = true;
scrollbarXWidth = getSettingsAdjustedThumbSize(parseInt(containerWidth * containerWidth / contentWidth, 10)); scrollbarXWidth = getSettingsAdjustedThumbSize(int(containerWidth * containerWidth / contentWidth));
scrollbarXLeft = parseInt($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth), 10); scrollbarXLeft = int($this.scrollLeft() * (containerWidth - scrollbarXWidth) / (contentWidth - containerWidth));
} }
else { else {
scrollbarXActive = false; scrollbarXActive = false;
@ -222,8 +231,8 @@
if (!settings.suppressScrollY && containerHeight + settings.scrollYMarginOffset < contentHeight) { if (!settings.suppressScrollY && containerHeight + settings.scrollYMarginOffset < contentHeight) {
scrollbarYActive = true; scrollbarYActive = true;
scrollbarYHeight = getSettingsAdjustedThumbSize(parseInt(containerHeight * containerHeight / contentHeight, 10)); scrollbarYHeight = getSettingsAdjustedThumbSize(int(containerHeight * containerHeight / contentHeight));
scrollbarYTop = parseInt($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight), 10); scrollbarYTop = int($this.scrollTop() * (containerHeight - scrollbarYHeight) / (contentHeight - containerHeight));
} }
else { else {
scrollbarYActive = false; scrollbarYActive = false;
@ -481,7 +490,7 @@
$scrollbarY.bind('click' + eventClassName, stopPropagation); $scrollbarY.bind('click' + eventClassName, stopPropagation);
$scrollbarYRail.bind('click' + eventClassName, function (e) { $scrollbarYRail.bind('click' + eventClassName, function (e) {
var halfOfScrollbarLength = parseInt(scrollbarYHeight / 2, 10); var halfOfScrollbarLength = int(scrollbarYHeight / 2);
var positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength; var positionTop = e.pageY - $scrollbarYRail.offset().top - halfOfScrollbarLength;
var maxPositionTop = containerHeight - scrollbarYHeight; var maxPositionTop = containerHeight - scrollbarYHeight;
var positionRatio = positionTop / maxPositionTop; var positionRatio = positionTop / maxPositionTop;
@ -497,7 +506,7 @@
$scrollbarX.bind('click' + eventClassName, stopPropagation); $scrollbarX.bind('click' + eventClassName, stopPropagation);
$scrollbarXRail.bind('click' + eventClassName, function (e) { $scrollbarXRail.bind('click' + eventClassName, function (e) {
var halfOfScrollbarLength = parseInt(scrollbarXWidth / 2, 10); var halfOfScrollbarLength = int(scrollbarXWidth / 2);
var positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength; var positionLeft = e.pageX - $scrollbarXRail.offset().left - halfOfScrollbarLength;
var maxPositionLeft = containerWidth - scrollbarXWidth; var maxPositionLeft = containerWidth - scrollbarXWidth;
var positionRatio = positionLeft / maxPositionLeft; var positionRatio = positionLeft / maxPositionLeft;
@ -686,7 +695,7 @@
var ieMatch = navigator.userAgent.toLowerCase().match(/(msie) ([\w.]+)/); var ieMatch = navigator.userAgent.toLowerCase().match(/(msie) ([\w.]+)/);
if (ieMatch && ieMatch[1] === 'msie') { if (ieMatch && ieMatch[1] === 'msie') {
// must be executed at first, because 'ieSupport' may addClass to the container // must be executed at first, because 'ieSupport' may addClass to the container
ieSupport(parseInt(ieMatch[2], 10)); ieSupport(int(ieMatch[2]));
} }
updateBarSizeAndPosition(); updateBarSizeAndPosition();