Skip to content
Browse files

Dev: Tidying up scrollbar width calcuation function

- Using jQuery we can make the code a bit smaller. Not much, just a 97
  byte saving, but every little helps, as they say....
  • Loading branch information...
1 parent 4c4bfb0 commit 7c53a1824c62ece4290647e645d45ec1e3b97bd4 @DataTables committed Feb 18, 2013
Showing with 52 additions and 48 deletions.
  1. +26 −24 media/js/jquery.dataTables.js
  2. +26 −24 media/src/core/core.sizing.js
View
50 media/js/jquery.dataTables.js
@@ -3926,35 +3926,37 @@
*/
function _fnScrollBarWidth ()
{
- var inner = document.createElement('p');
- var style = inner.style;
- style.width = "100%";
- style.height = "200px";
- style.padding = "0px";
-
- var outer = document.createElement('div');
- style = outer.style;
- style.position = "absolute";
- style.top = "0px";
- style.left = "0px";
- style.visibility = "hidden";
- style.width = "200px";
- style.height = "150px";
- style.padding = "0px";
- style.overflow = "hidden";
- outer.appendChild(inner);
-
- document.body.appendChild(outer);
+ var inner = $('<p/>').css( {
+ width: '100%',
+ height: 200,
+ padding: 0
+ } )[0];
+
+ var outer = $('<div/>')
+ .css( {
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ width: 200,
+ height: 150,
+ padding: 0,
+ overflow: 'hidden',
+ visibility: 'hidden'
+ } )
+ .append( inner )
+ .appendTo( 'body' );
+
var w1 = inner.offsetWidth;
- outer.style.overflow = 'scroll';
+ outer.css( 'overflow', 'scroll' );
var w2 = inner.offsetWidth;
- if ( w1 == w2 )
+ if ( w1 === w2 )
{
- w2 = outer.clientWidth;
+ w2 = outer[0].clientWidth;
}
- document.body.removeChild(outer);
- return (w1 - w2);
+ outer.remove();
+
+ return w1 - w2;
}
/**
View
50 media/src/core/core.sizing.js
@@ -370,34 +370,36 @@ function _fnStringToCss( s )
*/
function _fnScrollBarWidth ()
{
- var inner = document.createElement('p');
- var style = inner.style;
- style.width = "100%";
- style.height = "200px";
- style.padding = "0px";
-
- var outer = document.createElement('div');
- style = outer.style;
- style.position = "absolute";
- style.top = "0px";
- style.left = "0px";
- style.visibility = "hidden";
- style.width = "200px";
- style.height = "150px";
- style.padding = "0px";
- style.overflow = "hidden";
- outer.appendChild(inner);
-
- document.body.appendChild(outer);
+ var inner = $('<p/>').css( {
+ width: '100%',
+ height: 200,
+ padding: 0
+ } )[0];
+
+ var outer = $('<div/>')
+ .css( {
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ width: 200,
+ height: 150,
+ padding: 0,
+ overflow: 'hidden',
+ visibility: 'hidden'
+ } )
+ .append( inner )
+ .appendTo( 'body' );
+
var w1 = inner.offsetWidth;
- outer.style.overflow = 'scroll';
+ outer.css( 'overflow', 'scroll' );
var w2 = inner.offsetWidth;
- if ( w1 == w2 )
+ if ( w1 === w2 )
{
- w2 = outer.clientWidth;
+ w2 = outer[0].clientWidth;
}
- document.body.removeChild(outer);
- return (w1 - w2);
+ outer.remove();
+
+ return w1 - w2;
}

0 comments on commit 7c53a18

Please sign in to comment.
Something went wrong with that request. Please try again.