-
Notifications
You must be signed in to change notification settings - Fork 17
/
borderBoxModel.js
71 lines (54 loc) · 2.4 KB
/
borderBoxModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Add support fo CSS3 box-sizing: border-box model for IE6 and IE7
*
* @author Alberto Gasparin http://albertogasparin.it/
* @version 1.1, License MIT
*
**/
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++) {
element = elements[i];
s = element.style;
cs = element.currentStyle;
// check if box-sizing is specified and is equal to border-box
if(s.boxSizing == value || s["box-sizing"] == value
|| cs.boxSizing == value || cs["box-sizing"] == value) {
// change width and height
try {
apply();
} catch(e) {}
}
}
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(width) {
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),
horizSum = borderLeft + paddingLeft + paddingRight + borderRight;
// 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(height) {
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),
vertSum = borderTop + paddingTop + paddingBottom + borderBottom;
// 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');