首頁 web前端 js教程 ie6下png圖片背景不透明的解決方法使用js實作_javascript技巧

ie6下png圖片背景不透明的解決方法使用js實作_javascript技巧

May 16, 2016 pm 05:43 PM

我們時常在使用png圖片的時候,在ie6下發生背景不透明的問題,下面跟大家介紹下一個js解決的方式。
首先我們要用到一個js,程式碼如下:

複製程式碼 程式碼如下:

/**
* DD_belatedPNG:新增 IE6 支援:CSS 背景圖片和 HTML ie6下png圖片背景不透明的解決方法使用js實作_javascript技巧 的 PNG 映像。
* 作者:Drew Diller
* 電子郵件:drew.diller@gmail.com
* 網址:http://www.dillerdesign.com/experiment/DD_belatedPNG/
* 版本:0.0. 8a
* 根據MIT 許可證獲得許可:http://dillerdesign.com/experiment/DD_belatedPNG/#license
*
* 範例用法:
* DD_belatedPNG.fix('.png_bg') ; // 參數是CSS 選擇器
* DD_belatedPNG.fixPng( someNode ); // 參數是HTMLDomElement
**/
/*
請閱讀:
這個腳本中的所有內容絕對是愚蠢的。我知道這一點。 IE 對某些像素的渲染沒有意義,因此這段程式碼也沒有意義!
*/
/**rewrite by waitingbar 2012.1.12
為了解決IE6下透明的png圖片縮小時不能完全顯示問題
el.vml.image.fill.type = 'tile'; 改為:
el.vml. image.fill.type = 'frame';
*/

var DD_belatedPNG = {
ns: 'DD_belatedPNG',
imgSize: {},
延遲: 10,
nodesFixed: 0,
createVmlNameSpace: function () { /* 啟用VML */
if (document.namespaces && !document.namespaces[this.ns]) {
document.namespaces.addument. (this.ns, 'urn:schemas-microsoft-com:vml');
}
},
createVmlStyleSheet: function () { /* 樣式VML,啟用行為*/
/*
以防萬一許多其他開發人員添加了
許多其他使用document.createStyleSheet
建立樣式表並達到31 個限制標記,我們不要使用該方法!
進一步閱讀:http://msdn.microsoft.com/en-us/library/ms531194(VS.85).aspx
*/
var screenStyleSheet, printStyleSheet;
screenStyle. createElement('style');
screenStyleSheet.setAttribute('media', 'screen');
document.documentElement.firstChild.insertBefore(screenStyleSheet, document.documentElement.firstd. screenStyleSheet.styleSheet) {
screenStyleSheet = screenStyleSheet.styleSheet;
screenStyleSheet.addRule(this.ns '\:*', '{behavior:url(#defaultaultVML)}'); addRule(this.ns '\:shape', 'position:absolute;');
screenStyleSheet.addRule('img.' this.ns '_sizeFinder', '行為:無;邊框:無;位置:絕對; z-index:-1;上:-10000px;可見性:隱藏;'); /* 大的負頂部值,以避免大圖像的垂直滾動條,由James O'Brien 建議,http://www. thanatopsic.org/hendrik/ */
this.screenStyleSheet = screenStyleSheet;
/* 新增列印媒體樣式表,以防止VML 工件出現在列印中(包括預覽)。 */
/* 感謝 R閙i Pr關ost 自動化了這個! */
printStyleSheet = document.createElement('style');
printStyleSheet.setAttribute('media', 'print');
document.documentElement.firstChild.insertBefore;
document.documentElement.firstChild.insertBefore(print🎜>document.documentElement.firstChild.insertBefore(printltyle.first .firstChild);
printStyleSheet = printStyleSheet.styleSheet;
printStyleSheet.addRule(this.ns '\:*', '{display: none !important;}');
printStyleet. .' this.ns '_sizeFinder', '{display: none !important;}');
}
},
readPropertyChange: function () {
var el, display, v;
el = event.srcElement;
if (!el.vmlInitiated) {
return;
}
if (event.propertyName.search('background') != -1 || event .propertyName.search('border') != -1) {
DD_belatedPNG.applyVML(el) ;
}
if (event.propertyName == 'style.display') {
display = (el.currentStyle.display == 'none') ? '無' : '阻止';
for (v in el.vml) {
if (el.vml.hasOwnProperty(v)) {
el.vml[v].shape.style.display = display;
}
}
}
if (event.propertyName.search('filter') != -1) {
DD_belatedPNG.vmlOpacity(el);
}
},
vmlOpacity: function (el) {
if (el.currentStyle.filter.search('lpha') != -1') != -1 ) {
var trans = el.當前樣式.filter;
trans = parseInt(trans.substring(trans.lastIndexOf('=') 1, trans.lastIndexOf(')')), 10)/100 ;
el.vml.color.shape.style.filter = el.currentStyle.filter; /* 完成猜測*/
el.vml.image.fill.opacity = trans; /* 完成猜測*/
}
},
handlePseudoHover: function (el) {
setTimeout(function () { /* 如果沒有setTimeout,則無法如預期般運作*/
DD_belatedPNG .applyVML(el);
}, 1);
},
/**
* 這是在文件中使用的方法。
* @param {String} 選擇器 - 必要 - CSS 選擇器,例如 '#doc .container'
**/
修正:函數(選取器){
if (this.screenStyleSheet) {
var 選擇器,i;
選擇器=選擇器.split(','); /* 支援多個選擇器,不再需要多次呼叫*/
for (i=0; ithis.screenStyleSheet.addRule(selectors[i], 'behavior :表達式(DD_belatedPNG.fixPng(this))'); /* 似乎執行該函數而不將其添加到樣式表- 有趣... */
}
}
},
applyVML: function (el) {
el.runtimeStyle.cssText = '';
this.vmlFill(el);
this.vmlOffsets(el);
this.vmlOpacity(el);
if (el.isImg) {
this.copyImageBorders(el);
}
},
attachHandlers: function (el) {
var self, handlers, handler, moreForAs, a, h;
自我=這個;
handlers = {resize: 'vmlOffsets', move: 'vmlOffsets'};
if (el.nodeName == 'A') {
moreForAs = {mouseleave: 'handlePseudoHover', mouseenter: 'handlePseudoHover', focus: 'handlePseudoHover', Blur: 'handlePseudoHover'};
for (a in moreForAs) {
if (moreForAs.hasOwnProperty(a)) {
handlers[a] = moreForAs[a];
}
}
}
for (h in handlers) {
if (handlers.hasOwnProperty(h)) {
handler = function () {
self[handlers [h]](el);
};
el.attachEvent('on' h, handler);
}
}
el.attachEvent('onpropertychange', this.readPropertyChange);
},
giveLayout: function (el) {
el.style.zoom = 1;
if (el.currentStyle.position == '靜態') {
el.style.position = '相對';
}
},
copyImageBorders: function (el) {
var styles, s;
styles = {'borderStyle':true, 'borderWidth':true, 'borderColor':true};
for (s in styles) {
if (styles.hasOwnProperty(s)) {
el.vml.color.shape.style[s] = el.currentStyle[s];
}
}
},
vmlFill: function (el) {
if (!el.currentStyle) {
return;
} else {
var elStyle, noImg, lib, v, img, imgLoaded;
elStyle = el.currentStyle;
}
for (v in el.vml) {
if (el.vml.hasOwnProperty(v)) {
el.vml[v].shape.style.zIndex = elStyle.zIndex ;
}
}
el.runtimeStyle.backgroundColor = '';
el.runtimeStyle.backgroundImage = '';
noImg = true;
if (elStyle.backgroundImage != 'none' || el.isImg) {
if (!el.isImg) {
el.vmlBg = elStyle.backgroundImage;
el.vmlBg = el.vmlBg.substr(5, el.vmlBg.lastIndexOf('")')-5);
}
else {
el.vmlBg = el.src;
}
lib = this
if (!lib.imgSize[el.vmlBg]) { /* 決定載入圖片的大小*/
img = document.createElement('img')
lib.imgSize[el.vmlBg] = img;
img.className = lib.ns '_sizeFinder';
img.runtimeStyle.cssText = '行為:無位置:-10000px ; ; border:0; csding :0;'; /*確保將行為設為none 以防止意外匹配輔助元素! 🎜>this.width = this.offsetWidth; /* 奇怪的存儲破壞要求! vmlOffsets(el); .attachEvent('onload', imgLoaded);
img.src = el.vmlBg;
img.removeAttribute('width');
img.removeAttribute('height'); body.insertBefore(img, document.body.firstChild);
}
el.vml.image.fill.src = el.vmlBg; = false
}
el.vml.image.fillml.image.fillml.image.fill.v.image. .on = !noImg>el.vml.image.fill.color = '無';
el.vml.color.shape.style.backgroundColor = elStyle.backgroundColor;
el.runtimeStyle.backgroundImage = '無' ';
el.runtimeStyle.backgroundColor = '透明';
},
/* IE 無法弄清楚當offsetLeft 和clientLeft加起來為1時會做什麼,並且VML最終變化要模糊…所以我們必須將東西推/放大1個像素然後剪掉多餘的*/
vmlOffsets: function (el) {
var thisStyle, size, fudge, makeVisible, bg, bgR, dC, altC, b, c, v;
thisStyle = el.currentStyle;
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};
軟糖 = (size.L size.bLW == 1) ? 1:0;
/* vml 形狀、左、上、寬、高、原點 */
makeVisible = function (vml, l, t, w, h, o) {
vml.coordsize = w ','小時;
vml.coordorigin = o ',' o;
vml.path = 'm0,0l' w ',0l' w ',' h 'l0,' h ' xe';
vml.style.width = w 'px';
vml.style.height = h 'px';
vml.style.left = l 'px';
vml.style.top = t 'px';
};
makeVisible(el.vml.color.shape, (size.L (el.isImg ? 0 : size.bLW)), (size.T (el.isImg ? 0 : size.bTW) ), (尺寸.W-1), (尺寸.H-1), 0);
makeVisible(el.vml.image.shape, (size.L size.bLW), (size.T size.bTW), (size.W), (size.H), 1 );
bg = {'X':0, 'Y':0};
if (el.isImg) {
bg.X = parseInt(thisStyle.paddingLeft, 10) 1;
bg.Y = parseInt(thisStyle.paddingTop, 10) 1;
}
else {
for (b in bg) {
if (bg.hasOwnProperty(b)) {
this.figurePercentage(bg, size, b, thisStyle['backgroundPosition' b]);
}
}
}
el.vml.image.fill.position = (bg.X/size.W) ',' (bg.Y/size.H);
bgR = thisStyle.backgroundRepeat;
dC = {'T':1, 'R':size.W 軟糖, 'B':size.H, 'L':1 軟糖}; /* 這些是任何型別重複的預設值 */
altC = { 'X': {'b1': 'L', 'b2': 'R', 'd': 'W'}, 'Y ': {'b1': 'T', 'b2': 'B', 'd': 'H'} };
if (bgR != 'repeat' || el.isImg) {
c = {'T':(bg.Y), 'R':(bg.X size.w), 'B' :(bg.Y 尺寸.h), 'L':(bg.X)}; /* 這些是無重複的預設值- 剪輯到影像位置*/
if (bgR.search('repeat-') != -1) { /* 現在讓我們恢復到dC 進行重複x 或重複-y */
v = bgR.split('repeat-')[1].toUpperCase();
c[altC[v].b1] = 1;
c[altC[v] .b2] = 大小[altC[v].d];
}
if (c.B > size.H) {
c.B = size.H;
}
el.vml. image.shape.style.clip = 'rect(' c.T 'px ' (c.R fudge) 'px ' c.B 'px ' (c.L fudge) 'px)';
}
else {
el. vml.image.shape.style.clip = 'rect(' dC.T 'px ' dC.R 'px ' dC.B 'px ' dC.L ' px)';
}
},
figurePercentage:函數(背景、大小、軸、位置){
var 水平、分數;
分數=真;
水平= (軸== 'X');
switch(位置) {
case '左':
case '上':
bg[axis] = 0;
休息;
案例「中心」:
bg[軸] = 0.5 ;
休息;
case '右':
case '下':
bg[axis] = 1;
休息;
預設值:
if (position. search('%') != -1) {
bg[axis] = parseInt(position, 10) / 100;
}
else {
分數= false;
}
}
bg[軸] = Math.ceil( 分數? ( (尺寸[水平?'W': 'H'] * bg[軸]) - (尺寸[水平?'w ': 'h' ] * bg[axis]) ) : parseInt(position, 10) );
if (bg[軸] % 2 === 0) {
bg[軸] ;
}
返回bg[軸];
},
fixPng: function (el) {
el.style.behavior = 'none';
var lib, els, nodeStr, v, e;
if (el.nodeName == 'BODY' || el.nodeName == 'TD' || el.nodeName == 'TR') { /* 尚未支援的元素*/
return;
}
el.isImg = false;
if (el.nodeName == 'IMG') {
if(el.src.toLowerCase().search(/.png$/) != -1) {
el.isImg = true ;
el.style.visibility = '隱藏';
}
其他{
回傳;
}
}
else if (el.currentStyle.backgroundImage.toLowerCase().search('.png') == -1) {
return;
}
lib = DD_belatedPNG;
el.vml = {顏色: {},圖片:{}};
els = {形狀:{},填入:{}};
for (v in el.vml) {
if (el.vml.hasOwnProperty(v )) {
for (e in els) {
if (els.hasOwnProperty(e)) {
nodeStr = lib.ns ':' e;
el.vml[v][e ] = document.createElement(nodeStr);
}
}
el.vml[v].shape.lines = false;
el.vml[v].shape.appendChild(el.vml [v].fill);
el.parentNode.insertBefore(el.vml[v].shape, el);
}
}
el.vml.image.shape.fillcolor = '無'; /* 等待圖片載入時不顯示空白的白色shapeangle。 */
//el.vml.image.fill.type = 'tile'; /* 使影像顯示出來。 */
el.vml.image.fill.type = 'frame'; /* 2012.1.12 */
el.vml.color.fill.on = false; /* 實際上要套用vml 元素的style.backgroundColor,因此隱藏白色。 */
lib.attachHandlers(el);
lib.giveLayout(el);
lib.giveLayout(el.offsetParent);
el.vmlInitiated = true;
lib.applyVML(lib.applyVML( el); /* 使成為! */
}
};
嘗試{
document.execCommand("BackgroundImageCache", false, true); /* TredoSoft Multiple IE 不喜歡這樣,所以試試看*/
} catch(r) {}
DD_belatedPNG.createVmlNameSpace();
DD_belatedPNG.createVmlStyleSheet();

我們看怎麼使用這個> DD_belatedPNG是一款完美解決IE6下的PNG透明JS插件,DD_belatedPNG使用了微軟的VML語言對PNG圖片進行重複,以達到半透明的效果,並且能夠支援background-position和background-repeat屬性,支援a類。在人群的ie6下png透明問題上,DD_belatedPNG是最好的解決方案,其他的都有

使用方法

複製程式碼程式碼如下:
<script></script> DD_belatedPNG.fix('.png_bg');


引用函數是DD_belatedPNG.fix(),事實上裡面的*表示所有css選擇器.png_bg改成你的css選擇器名稱。
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1276
29
C# 教程
1256
24
Python vs. JavaScript:學習曲線和易用性 Python vs. JavaScript:學習曲線和易用性 Apr 16, 2025 am 12:12 AM

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

從C/C到JavaScript:所有工作方式 從C/C到JavaScript:所有工作方式 Apr 14, 2025 am 12:05 AM

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

JavaScript和Web:核心功能和用例 JavaScript和Web:核心功能和用例 Apr 18, 2025 am 12:19 AM

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

JavaScript在行動中:現實世界中的示例和項目 JavaScript在行動中:現實世界中的示例和項目 Apr 19, 2025 am 12:13 AM

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

了解JavaScript引擎:實施詳細信息 了解JavaScript引擎:實施詳細信息 Apr 17, 2025 am 12:05 AM

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。

Python vs. JavaScript:社區,圖書館和資源 Python vs. JavaScript:社區,圖書館和資源 Apr 15, 2025 am 12:16 AM

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

Python vs. JavaScript:開發環境和工具 Python vs. JavaScript:開發環境和工具 Apr 26, 2025 am 12:09 AM

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

C/C在JavaScript口譯員和編譯器中的作用 C/C在JavaScript口譯員和編譯器中的作用 Apr 20, 2025 am 12:01 AM

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

See all articles