Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Various improvements

Fixed a bug with display:none elements (outerHeight was zero)
Inline styles are not set if borders and padding are null
  • Loading branch information...
commit 1682b0dc4772db3c862821ce620a5be18a275c8c 1 parent 880c326
@albertogasparin authored
Showing with 27 additions and 22 deletions.
  1. +2 −2 borderBoxModel-min.js
  2. +25 −20 borderBoxModel.js
View
4 borderBoxModel-min.js
@@ -2,8 +2,8 @@
* Add support fo CSS3 box-sizing: border-box model for IE6 and IE7
*
* @author Alberto Gasparin http://albertogasparin.it/
- * @version 1.0, License MIT
+ * @version 1.1, License MIT
*
**/
-var borderBoxModel=(function(){var h=document.getElementsByTagName("*"),d,f,e,g="border-box";for(var c=0,a=h.length;c<a;c++){d=h[c];e=d.style;f=d.currentStyle;if(e.boxSizing==g||e["box-sizing"]==g||f.boxSizing==g||f["box-sizing"]==g){b()}}function b(){if(parseInt(f.width,10)||parseInt(e.width,10)){var i=d.offsetWidth,n=parseInt(f.borderLeftWidth||e.borderLeftWidth,10)||0,p=parseInt(f.borderRightWidth||e.borderRightWidth,10)||0,l=parseInt(f.paddingLeft||e.paddingLeft,10),k=parseInt(f.paddingRight||e.paddingRight,10);e.width=i-(n+l+k+p)*2}if(parseInt(f.height)||parseInt(e.height)){var q=d.offsetHeight,m=parseInt(f.borderTopWidth||e.borderTopWidth,10)||0,o=parseInt(f.borderBottomWidth||e.borderBottomWidth,10)||0,r=parseInt(f.paddingTop||e.paddingTop,10),j=parseInt(f.paddingBottom||e.paddingBottom,10);e.height=q-(m+r+j+o)*2}}})();
+var borderBoxModel=function(l,d){function m(){e=parseInt(b.width,10)||parseInt(a.width,10);f=parseInt(b.height,10)||parseInt(a.height,10);if(e){var c=parseInt(b.borderLeftWidth||a.borderLeftWidth,10)||0,g=parseInt(b.borderRightWidth||a.borderRightWidth,10)||0,h=parseInt(b.paddingLeft||a.paddingLeft,10),i=parseInt(b.paddingRight||a.paddingRight,10);if(c=c+h+i+g)a.width=e-c}if(f){c=parseInt(b.borderTopWidth||a.borderTopWidth,10)||0;g=parseInt(b.borderBottomWidth||a.borderBottomWidth,10)||0;h=parseInt(b.paddingTop||a.paddingTop,10);i=parseInt(b.paddingBottom||a.paddingBottom,10);if(c=c+h+i+g)a.height=f-c}}for(var j,b,a,e,f,k=0,n=l.length;k<n;k++){j=l[k];a=j.style;b=j.currentStyle;if(a.boxSizing==d||a["box-sizing"]==d||b.boxSizing==d||b["box-sizing"]==d)m()}}(document.getElementsByTagName("*"),"border-box");
View
45 borderBoxModel.js
@@ -2,14 +2,12 @@
* Add support fo CSS3 box-sizing: border-box model for IE6 and IE7
*
* @author Alberto Gasparin http://albertogasparin.it/
- * @version 1.0, License MIT
+ * @version 1.1, License MIT
*
**/
-var borderBoxModel = (function() {
- var elements = document.getElementsByTagName('*'),
- element, cs, s,
- value = 'border-box';
+var borderBoxModel = (function(elements, value) {
+ var element, cs, s, width, height;
// cicle through all DOM elements
for (var i=0, max=elements.length; i < max; i++) {
@@ -29,37 +27,44 @@ var borderBoxModel = (function() {
function apply() {
+ width = parseInt(cs.width, 10) || parseInt(s.width, 10);
+ height = parseInt(cs.height, 10) || parseInt(s.height, 10);
+
// if width is declared (and not 'auto','',...)
- if(parseInt(cs.width,10) || parseInt(s.width,10)) {
+ if(width) {
- var width = element.offsetWidth, // get width with border and padding
-
- // border value could be 'medium' so parseInt returns NaN
+ var // border value could be 'medium' so parseInt returns NaN
borderLeft = parseInt(cs.borderLeftWidth || s.borderLeftWidth, 10) || 0,
borderRight = parseInt(cs.borderRightWidth || s.borderRightWidth, 10) || 0,
paddingLeft = parseInt(cs.paddingLeft || s.paddingLeft, 10),
- paddingRight = parseInt(cs.paddingRight || s.paddingRight, 10);
+ paddingRight = parseInt(cs.paddingRight || s.paddingRight, 10),
+
+ horizSum = borderLeft + paddingLeft + paddingRight + borderRight;
- // remove from width padding and border two times
- s.width = width - (borderLeft + paddingLeft + paddingRight + borderRight) * 2;
+ // remove from width padding and border two times if != 0
+ if(horizSum) {
+ s.width = width - horizSum; //width ? width - horizSum * 2 : styleWidth - horizSum;
+ }
}
// if height is declared (and not 'auto','',...)
- if(parseInt(cs.height) || parseInt(s.height)) {
+ if(height) {
- var height = element.offsetHeight, // get height with border and padding
-
- // border value could be 'medium' so parseInt returns NaN
+ var // border value could be 'medium' so parseInt returns NaN
borderTop = parseInt(cs.borderTopWidth || s.borderTopWidth, 10) || 0,
borderBottom = parseInt(cs.borderBottomWidth || s.borderBottomWidth, 10) || 0,
paddingTop = parseInt(cs.paddingTop || s.paddingTop, 10),
- paddingBottom = parseInt(cs.paddingBottom || s.paddingBottom, 10);
+ paddingBottom = parseInt(cs.paddingBottom || s.paddingBottom, 10),
+
+ vertSum = borderTop + paddingTop + paddingBottom + borderBottom;
- // remove from height padding and border two times
- s.height = height - (borderTop + paddingTop + paddingBottom + borderBottom) * 2;
+ // remove from height padding and border two times if != 0
+ if(vertSum) {
+ s.height = height - vertSum; //height ? height - vertSum * 2 : styleHeight - vertSum;
+ }
}
}
-})();
+})(document.getElementsByTagName('*'), 'border-box');
Please sign in to comment.
Something went wrong with that request. Please try again.