-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathie7-jsincss.html
176 lines (127 loc) · 4.64 KB
/
ie7-jsincss.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>JSinCSS in IE7</title>
<!-- IE Polyfills for addEventListener() and querySelectorAll() -->
<script>
/*
* addEventListener polyfill 1.0 / Eirik Backer / MIT Licence
* Forked from http://css-tricks.com/snippets/javascript/addeventlistner-polyfill/
* Adds the native DOM2 function addEventListener on IE6 - 8.
*/
(function (win, doc) {
// If the function already exists, no need to polyfill
if (win.addEventListener) return;
function docHijack(p) {
var old = doc[p];
doc[p] = function (v) {
return addListen(old(v))
}
}
function addEvent(on, fn, self) {
return (self = this).attachEvent('on' + on, function(e) {
var e = e || win.event;
e.preventDefault = e.preventDefault || function(){e.returnValue = false}
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true}
try {
fn.call(self, e);
}
catch (e) {}
});
}
function addListen(obj, i) {
if (i = obj.length) while (i--) obj[i].addEventListener = addEvent;
else obj.addEventListener = addEvent;
return obj;
}
addListen([doc, win]);
if ('Element' in win) win.Element.prototype.addEventListener = addEvent; // IE8
else{ // IE < 8
doc.attachEvent('onreadystatechange', function(){addListen(doc.all)}); // Make sure we also init at domReady
docHijack('getElementsByTagName');
docHijack('getElementById');
docHijack('createElement');
addListen(doc.all);
}
})(window, document);
/*
* document.querySelector and querySelectorAll polyfill / Maxime Euzière / public domain
* Forked from: http://xem.github.io/Lazy/
* Adds basic DOM selection on IE6-8 (selection by tag, class or id only)
*/
(function(doc) {
if (doc.querySelectorAll) return;
doc.querySelectorAll = function(a) {
if ("#" == a.charAt(0)) return [doc.getElementById(a.substr(1))];
if ("." == a.charAt(0)) return doc.getElementsByClassName(a.substr(1));
return doc.getElementsByTagName(a);
}
doc.querySelector = function(a) {
return querySelectorAll(a)[0];
}
})(document);
</script>
<!-- JS-in-CSS with Container Queries Mixin -->
<script>
window.addEventListener('load', JSinCSS)
window.addEventListener('resize', JSinCSS)
document.documentElement.addEventListener('keydown', JSinCSS)
document.documentElement.addEventListener('keyup', JSinCSS)
document.documentElement.addEventListener('mousedown', JSinCSS)
document.documentElement.addEventListener('mouseup', JSinCSS)
function JSinCSS(e) {
var styleTag = document.getElementsByTagName('style')
var tag
for (var i=0; i<styleTag.length; i++) {
if (styleTag[i].id == 'JSinCSS') {
tag = styleTag[i]
}
}
if (!tag) {
tag = document.createElement('style')
tag.id = 'JSinCSS'
document.getElementsByTagName('head')[0].appendChild(tag)
}
var css = ''
css += containerQuery('input', 'this.value.length > 5', '$this { background: lime; }')
css += containerQuery('div', 'this.offsetWidth > 500', '$this { background: blue; margin: 1em 0; }')
if (tag.styleSheet) {
tag.styleSheet.cssText = css
} else {
tag.innerHTML = css
}
return true
}
function containerQuery(selector, test, stylesheet) {
var tag = document.querySelectorAll(selector)
var style = ''
var count = 0
for (var i=0; i<tag.length; i++) {
var attr = (selector+test).replace(/[= "'<>+.]/g, '')
var func = new Function('return ' + test)
if (func.call(tag[i])) {
var element = '[data-' + attr + '="' + count + '"]'
var css = stylesheet.replace(/\$this/g, element)
tag[i].setAttribute('data-' + attr, count)
style += css
count++
} else {
tag[i].setAttribute('data-' + attr, '')
}
}
return style
}
</script>
</head>
<body>
<p>Type more than 5 characters into the INPUT below:</p>
<input>
<input>
<p>DIVs below blue if wider than 500px</p>
<div style="width: 100%">100% wide</div>
<div style="width: 66.66%">66.6% wide</div>
<div style="width: 33.33%">33.33% wide</div>
</body>
</html>