Skip to content

Commit 848c45e

Browse files
committed
Added some significant speed-ups to height/width checks, thanks to some code and investigation by Mike Helgeson. Fixes jquery#3082.
1 parent 7f1eb1c commit 848c45e

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/core.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,26 +734,32 @@ jQuery.extend({
734734
elem.style[ name ] = old[ name ];
735735
},
736736

737-
css: function( elem, name, force ) {
737+
css: function( elem, name, force, extra ) {
738738
if ( name == "width" || name == "height" ) {
739739
var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ];
740740

741741
function getWH() {
742742
val = name == "width" ? elem.offsetWidth : elem.offsetHeight;
743-
var padding = 0, border = 0;
743+
744+
if ( extra === "border" )
745+
return;
746+
744747
jQuery.each( which, function() {
745-
padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
746-
border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
748+
if ( !extra )
749+
val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0;
750+
if ( extra === "margin" )
751+
val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0;
752+
else
753+
val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0;
747754
});
748-
val -= Math.round(padding + border);
749755
}
750756

751-
if ( jQuery(elem).is(":visible") )
757+
if ( elem.offsetWidth !== 0 )
752758
getWH();
753759
else
754760
jQuery.swap( elem, props, getWH );
755761

756-
return Math.max(0, val);
762+
return Math.max(0, Math.round(val));
757763
}
758764

759765
return jQuery.curCSS( elem, name, force );

src/dimensions.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
jQuery.each([ "Height", "Width" ], function(i, name){
33

44
var tl = i ? "Left" : "Top", // top or left
5-
br = i ? "Right" : "Bottom"; // bottom or right
5+
br = i ? "Right" : "Bottom", // bottom or right
6+
lower = name.toLowerCase();
67

78
// innerHeight and innerWidth
89
jQuery.fn["inner" + name] = function(){
9-
return this[ name.toLowerCase() ]() +
10-
num(this, "padding" + tl) +
11-
num(this, "padding" + br);
10+
return this[0] ?
11+
jQuery.css( this[0], lower, false, "padding" ) :
12+
null;
1213
};
1314

1415
// outerHeight and outerWidth
1516
jQuery.fn["outer" + name] = function(margin) {
16-
return this["inner" + name]() +
17-
num(this, "border" + tl + "Width") +
18-
num(this, "border" + br + "Width") +
19-
(margin ?
20-
num(this, "margin" + tl) + num(this, "margin" + br) : 0);
17+
return this[0] ?
18+
jQuery.css( this[0], lower, false, margin ? "margin" : "border" ) :
19+
null;
2120
};
2221

2322
var type = name.toLowerCase();
@@ -47,4 +46,4 @@ jQuery.each([ "Height", "Width" ], function(i, name){
4746
this.css( type, typeof size === "string" ? size : size + "px" );
4847
};
4948

50-
});
49+
});

0 commit comments

Comments
 (0)