<!--[if IE 6]> <script src="js/DD_belatedPNG_0.0.8a.js"></script> <script> DD_belatedPNG.fix('.ab,.ab1,.ab2,.ab3,.ab4,.ab5,.ab6,.ab7'); </script> <![endif]-->
专门解决ie6 png格式的问题,用法:粘贴上述代码,链接到DD_belatedPNG_0.0.8a.js, DD_belatedPNG.fix('.ab,.ab1,.ab2,.ab3,.ab4,.ab5,.ab6,.ab7');
.ab,.ab1,.ab2,.ab3,.ab4,.ab5,.ab6,.ab7是页面用到png的class名称
DD_belatedPNG_0.0.8a.js
当前路径:web/js/DD_belatedPNG_0.0.8a.js
2
/**
3
* DD_belatedPNG: Adds IE6 support: PNG images for CSS background-image and HTML <IMG/>.
4
* Author: Drew Diller
5
* Email: drew.diller@gmail.com
6
* URL: http://www.dillerdesign.com/experiment/DD_belatedPNG/
7
* Version: 0.0.8a
8
* Licensed under the MIT License: http://dillerdesign.com/experiment/DD_belatedPNG/#license
9
*
10
* Example usage:
11
* DD_belatedPNG.fix('.png_bg'); // argument is a CSS selector
12
* DD_belatedPNG.fixPng( someNode ); // argument is an HTMLDomElement
13
**/
14
15
/*
16
PLEASE READ:
17
Absolutely everything in this script is SILLY. I know this. IE's rendering of certain pixels doesn't make sense, so neither does this code!
18
*/
19
20
var DD_belatedPNG =
21
'DD_belatedPNG',
22
imgSize: {},
23
10,
24
0,
25
function () { /* enable VML */
26
if (document.namespaces && !document.namespaces[this.ns]) {
27
add(this.ns, 'urn:schemas-microsoft-com:vml');
28
}
29
},
30
function () { /* style VML, enable behaviors */
31
/*
32
Just in case lots of other developers have added
33
lots of other stylesheets using document.createStyleSheet
34
and hit the 31-limit mark, let's not use that method!
35
http://msdn.microsoft.com/en-us/library/ms531194(VS.85).aspx
36
*/
37
var
38
= document.createElement('style');
39
setAttribute('media', 'screen');
40
insertBefore(screenStyleSheet, document.documentElement.firstChild.firstChild);
41
if
42
=
43
addRule(this.ns + '\\:*', '{behavior:url(#default#VML)}');
44
addRule(this.ns + '\\:shape', 'position:absolute;');
45
addRule('img.' + this.ns + '_sizeFinder', 'behavior:none; border:none; position:absolute; z-index:-1; top:-10000px; visibility:hidden;'); /* large negative top value for avoiding vertical scrollbars for large images, suggested by James O'Brien, http://www.thanatopsic.org/hendrik/
46
this.screenStyleSheet =
47
48
/* Add a print-media stylesheet, for preventing VML artifacts from showing up in print (including preview). */
49
/* Thanks to R閙i Pr関ost for automating this! */
50
= document.createElement('style');
51
setAttribute('media', 'print');
52
insertBefore(printStyleSheet, document.documentElement.firstChild.firstChild);
53
=
54
addRule(this.ns + '\\:*', '{display: none !important;}');
55
addRule('img.' + this.ns + '_sizeFinder', '{display: none !important;}');
56
}
57
},
58
function
59
var
60
=
61
if (!el.vmlInitiated) {
62
return;
63
}
64
if (event.propertyName.search('background') != -1 || event.propertyName.search('border') != -1) {
65
DD_belatedPNG.applyVML(el);
66
}
67
if (event.propertyName == 'style.display') {
68
= (el.currentStyle.display == 'none') ? 'none' : 'block';
69
for (v in
70
if (el.vml.hasOwnProperty(v)) {
71
=
72
}
73
}
74
}
75
if (event.propertyName.search('filter') != -1) {
76
DD_belatedPNG.vmlOpacity(el);
77
}
78
},
79
function
80
if (el.currentStyle.filter.search('lpha') != -1) {
81
var trans =
82
= parseInt(trans.substring(trans.lastIndexOf('=')+1, trans.lastIndexOf(')')), 10)/100;
83
el.vml.color.shape.style.filter = el.currentStyle.filter; /* complete guesswork */
84
el.vml.image.fill.opacity = trans; /* complete guesswork */
85
}
86
},
87
handlePseudoHover: function (el) {
88
setTimeout(function () { /* wouldn't work as intended without setTimeout */
89
DD_belatedPNG.applyVML(el);
90
}, 1);
91
},
92
/**
93
* This is the method to use in
94
* @param {String} selector - REQUIRED - a CSS selector, such as '#doc .container'
95
**/
96
fix: function (selector) {
97
if (this.screenStyleSheet) {
98
var selectors, i;
99
selectors = selector.split(','); /* multiple selectors supported, no need for multiple calls to this anymore */
100
for (i=0; i<selectors.length; i++) {
101
this.screenStyleSheet.addRule(selectors[i], 'behavior:expression(DD_belatedPNG.fixPng(this))'); /* seems to execute the function without adding it to the stylesheet - interesting... */
102
}
103
}
104
},
105
applyVML: function (el) {
106
el.runtimeStyle.cssText = '';
107
this.vmlFill(el);
108
this.vmlOffsets(el);
109
this.vmlOpacity(el);
110
if (el.isImg) {
111
this.copyImageBorders(el);
112
}
113
},
114
attachHandlers: function (el) {
115
var self, handlers, handler, moreForAs, a, h;
116
self = this;
117
handlers = {resize: 'vmlOffsets', move: 'vmlOffsets'};
118
if (el.nodeName == 'A') {
119
moreForAs = {mouseleave: 'handlePseudoHover', mouseenter: 'handlePseudoHover', focus: 'handlePseudoHover', blur: 'handlePseudoHover'};
120
for (a in moreForAs) {
121
if (moreForAs.hasOwnProperty(a)) {
122
handlers[a] = moreForAs[a];
123
}
124
}
125
}
126
for (h in handlers) {
127
if (handlers.hasOwnProperty(h)) {
128
handler = function () {
129
self[handlers[h]](el);
130
};
131
el.attachEvent('on' + h, handler);
132
}
133
}
134
el.attachEvent('onpropertychange', this.readPropertyChange);
135
},
136
giveLayout: function (el) {
137
el.style.zoom = 1;
138
if (el.currentStyle.position == 'static') {
139
el.style.position = 'relative';
140
}
141
},
142
copyImageBorders: function (el) {
143
var styles, s;
144
styles = {'borderStyle':true, 'borderWidth':true, 'borderColor':true};
145
for (s in styles) {
146
if (styles.hasOwnProperty(s)) {
147
el.vml.color.shape.style[s] = el.currentStyle[s];
148
}
149
}
150
},
151
vmlFill: function (el) {
152
if (!el.currentStyle) {
153
return;
154
} else {
155
var elStyle, noImg, lib, v, img, imgLoaded;
156
elStyle = el.currentStyle;
157
}
158
for (v in el.vml) {
159
if (el.vml.hasOwnProperty(v)) {
160
el.vml[v].shape.style.zIndex = elStyle.zIndex;
161
}
162
}
163
el.runtimeStyle.backgroundColor = '';
164
el.runtimeStyle.backgroundImage = '';
165
noImg = true;
166
if (elStyle.backgroundImage != 'none' || el.isImg) {
167
if (!el.isImg) {
168
el.vmlBg = elStyle.backgroundImage;
169
el.vmlBg = el.vmlBg.substr(5, el.vmlBg.lastIndexOf('")')-5);
170
}
171
else {
172
el.vmlBg = el.src;
173
}
174
lib = this;
175
if (!lib.imgSize[el.vmlBg]) { /* determine size of loaded image */
176
img = document.createElement('img');
177
lib.imgSize[el.vmlBg] = img;
178
img.className = lib.ns + '_sizeFinder';
179
img.runtimeStyle.cssText = 'behavior:none; position:absolute; left:-10000px; top:-10000px; border:none; margin:0; padding:0;'; /* make sure to set behavior to none to prevent accidental matching of the helper elements! */
180
imgLoaded = function () {
181
this.width = this.offsetWidth; /* weird cache-busting requirement! */
182
this.height = this.offsetHeight;
183
lib.vmlOffsets(el);
184
};
185
img.attachEvent('onload', imgLoaded);
186
img.src = el.vmlBg;
187
img.removeAttribute('width');
188
img.removeAttribute('height');
189
document.body.insertBefore(img, document.body.firstChild);
190
}
191
el.vml.image.fill.src = el.vmlBg;
192
noImg = false;
193
}
194
el.vml.image.fill.on = !noImg;
195
el.vml.image.fill.color = 'none';
196
el.vml.color.shape.style.backgroundColor = elStyle.backgroundColor;
197
el.runtimeStyle.backgroundImage = 'none';
198
el.runtimeStyle.backgroundColor = 'transparent';
199
},
200
/* IE can't figure out what do when the offsetLeft and the clientLeft add up to 1, and the VML ends up getting fuzzy... so we have to push/enlarge things by 1 pixel and then clip off the excess */
201
vmlOffsets: function (el) {
202
var thisStyle, size, fudge, makeVisible, bg, bgR, dC, altC, b, c, v;
203
thisStyle = el.currentStyle;
204
size = {'W':el.clientWidth+1, 'H':el.clientHeight+1, 'w':this.imgSize[el.vmlBg].width, 'h':this.imgSize[el.vmlBg].height, 'L':el.offsetLeft, 'T':el.offsetTop, 'bLW':el.clientLeft, 'bTW':el.clientTop};
205
fudge = (size.L + size.bLW == 1) ? 1 : 0;
206
/* vml shape, left, top, width, height, origin */
207
makeVisible = function (vml, l, t, w, h, o) {
208
vml.coordsize = w+','+h;
209
vml.coordorigin = o+','+o;
210
vml.path = 'm0,0l'+w+',0l'+w+','+h+'l0,'+h+' xe';
211
vml.style.width = w + 'px';
212
vml.style.height = h + 'px';
213
vml.style.left = l + 'px';
214
vml.style.top = t + 'px';
215
};
216
makeVisible(el.vml.color.shape, (size.L + (el.isImg ? 0 : size.bLW)), (size.T + (el.isImg ? 0 : size.bTW)), (size.W-1), (size.H-1), 0);
217
makeVisible(el.vml.image.shape, (size.L + size.bLW), (size.T + size.bTW), (size.W), (size.H), 1 );
218
bg = {'X':0, 'Y':0};
219
if (el.isImg) {
220
bg.X = parseInt(thisStyle.paddingLeft, 10) + 1;
221
bg.Y = parseInt(thisStyle.paddingTop, 10) + 1;
222
}
223
else {
224
for (b in bg) {
225
if (bg.hasOwnProperty(b)) {
226
this.figurePercentage(bg, size, b, thisStyle['backgroundPosition'+b]);
227
}
228
}
229
}
230
el.vml.image.fill.position = (bg.X/size.W) + ',' + (bg.Y/size.H);
231
=
232
= {'T':1, 'R':size.W+fudge, 'B':size.H, 'L':1+fudge}; /* these are defaults for repeat of any kind */
233
= { 'X': {'b1': 'L', 'b2': 'R', 'd': 'W'}, 'Y': {'b1': 'T', 'b2': 'B', 'd': 'H'} };
234
if (bgR != 'repeat' ||
235
= {'T':(bg.Y), 'R':(bg.X+size.w), 'B':(bg.Y+size.h), 'L':(bg.X)}; /* these are defaults for no-repeat - clips down to the image location */
236
if (bgR.search('repeat-') != -1) { /* now let's revert to dC for repeat-x or repeat-y */
237
= bgR.split('repeat-')[1].toUpperCase();
238
= 1;
239
=
240
}
241
if (c.B > size.H) {
242
B = size.H;
243
}
244
= 'rect('+c.T+'px '+(c.R+fudge)+'px '+c.B+'px '+(c.L+fudge)+'px)';
245
}
246
else
247
= 'rect('+dC.T+'px '+dC.R+'px '+dC.B+'px '+dC.L+'px)';
248
}
249
},
250
function
251
var
252
= true;
253
= (axis == 'X');
254
switch(position) {
255
case 'left':
256
case 'top':
257
= 0;
258
break;
259
case 'center':
260
= 0.5;
261
break;
262
case 'right':
263
case 'bottom':
264
= 1;
265
break;
266
default:
267
if (position.search('%') != -1) {
268
= parseInt(position, 10) / 100;
269
}
270
else {
271
fraction = false;
272
}
273
}
274
bg[axis] = Math.ceil( fraction ? ( (size[horizontal?'W': 'H'] * bg[axis]) - (size[horizontal?'w': 'h'] * bg[axis]) ) : parseInt(position, 10) );
275
if (bg[axis] % 2 === 0) {
276
bg[axis]++;
277
}
278
return bg[axis];
279
},
280
fixPng: function (el) {
281
el.style.behavior = 'none';
282
var lib, els, nodeStr, v, e;
283
if (el.nodeName == 'BODY' || el.nodeName == 'TD' || el.nodeName == 'TR') { /* elements not supported yet */
284
return;
285
}
286
el.isImg = false;
287
if (el.nodeName == 'IMG') {
288
if(el.src.toLowerCase().search(/\.png$/) != -1) {
289
el.isImg = true;
290
el.style.visibility = 'hidden';
291
}
292
else {
293
return;
294
}
295
}
296
else if (el.currentStyle.backgroundImage.toLowerCase().search('.png') == -1) {
297
return;
298
}
299
lib = DD_belatedPNG;
300
el.vml = {color: {}, image: {}};
301
els = {shape: {}, fill: {}};
302
for (v in el.vml) {
303
if (el.vml.hasOwnProperty(v)) {
304
for (e in els) {
305
if (els.hasOwnProperty(e)) {
306
nodeStr = lib.ns + ':' + e;
307
el.vml[v][e] = document.createElement(nodeStr);
308
}
309
}
310
el.vml[v].shape.stroked = false;
311
el.vml[v].shape.appendChild(el.vml[v].fill);
312
el.parentNode.insertBefore(el.vml[v].shape, el);
313
}
314
}
315
el.vml.image.shape.fillcolor = 'none'; /* Don't show blank white shapeangle when waiting for image to load. */
316
el.vml.image.fill.type = 'tile'; /* Makes image show up. */
317
el.vml.color.fill.on = false; /* Actually going to apply vml element's style.backgroundColor, so hide the whiteness. */
318
lib.attachHandlers(el);
319
lib.giveLayout(el);
320
lib.giveLayout(el.offsetParent);
321
el.vmlInitiated = true;
322
lib.applyVML(el); /* Render! */
323
}
324
};
325
try {
326
document.execCommand("BackgroundImageCache", false, true); /* TredoSoft Multiple IE doesn't like this, so try{} it */
327
} catch(r) {}
328
DD_belatedPNG.createVmlNameSpace();
329
DD_belatedPNG.createVmlStyleSheet();