1.文字自己的大小不兼容。一樣是font-size:14px的宋體文字,在不一樣瀏覽器下佔的空間是不同的,ie下實際佔高16px,下留白3px,ff下實際佔高17px,上留白1px,下留白3px。解決方案:給文字設定 line-height 。確保全部文字都有默認的 line-height 值。javascript
2.ff下容器高度限定.即容器定義了height以後,容器邊框的外形就肯定了,不會被內容撐大,而ie下是會被內容撐大,高度限定失效。因此不要輕易給容器定義height。css
3. 橫向上的內容撐破容器問題。若是float 容器未定義寬度,ff下內容會盡量撐開容器寬度,ie下則會優先考慮內容折行。故,內容可能撐破的浮動容器須要定義width。html
4.浮動的清除,ff下必須清除浮動clear:both。java
5. double-margin bug。ie6下給浮動容器定義margin-left 或者margin-right 實際效果是數值的2倍。解決方案,給浮動容器定義display:inline。node
6.margin bug.當外層元素內有float元素時,外層元素如定義margin-top:14px,將自動生成margin-bottom:14px。padding也會出現相似問題,都是ie6下的特產,該類bug 出現的狀況較爲複雜,遠不僅這一種出現條件。解決方案:外層元素設定border 或設定float。web
7.吞吃現象.仍是ie6,上下兩個div,上面的div設置背景,卻發現下面沒有設置背景的div 也有了背景,這就是吞吃現象。對應上面的背景吞吃現象,還有滾動下邊框缺失的現象。解決方案:使用zoom:1。express
8.註釋也能產生bug~~~「多出來的一隻豬。」這是前人總結這個bug使用的文案,ie6的這個bug 下,你們會在頁面看到豬字出現兩遍,重複的內容量因註釋的多少而變。解決方案:用「<!–[if !IE]> picRotate start <![endif]–>」方法寫註釋。瀏覽器
9.img下的留白,你們看這段代碼有啥問題:app
把div的border打開,你發現圖片底部不是緊貼着容器底部的,是img後面的空白字符形成,給img設定 display:block能夠解決。asp.net
10去line-height。<div style=」line-height:20px」><img />文字</div>,很遺憾,在ie6下單行文字 line-height 效果消失了。。。,緣由是<img />這個inline-block元素和inline元素寫在一塊兒了。解決方案:讓img 和文字都 float起來。
11連接的hover狀態。a:hover img{width:300px} 咱們想讓鼠標hover時,連接裏包含的圖片寬度變化,惋惜在ie6下無效,ie七、ff下有效。
12連接的hover狀態。div:hover{} 這樣的樣式ie6是不認的,在ie七、ff下才有效果。
13block化的a連接,其內套absolute層,absolute層內放置img,ie下,鼠標點擊img不會有連接效果,ff下正常。
14沒法完全清除的float。解決方案:給ul 屬性zoom:1 (給li 加zoom:1 沒用)
15ie下overflow:hidden對其下的絕對層position:absolute或者相對層position:relative無效。解決方案:給overflow:hidden加position:relative或者position:absolute。另,ie6支持overflow-x或者overflow-y的特性,ie七、ff不支持。
16.ie6下嚴重的bug,float元素如沒定義寬度,內部若有div定義了height或zoom:1,這個div就會佔滿一整行,即便你給了寬度。float元素若是做爲佈局用或複雜的容器,都要給個寬度的。
17.ie6下的bug,絕對定位的div下包含相對定位的div,若是給內層相對定位的div高度height具體值,內層相對層將具備100%的width值,外層絕對層將被撐大。解決方案給內層相對層float屬性。
18.ie下的bug,<head></head>內有<base target=」_blank」/>的狀況下,position:relative層下的float層內文字沒法選中。這個bug迫使我修公用樣式庫。
19.ff的缺點。width:100%在ie裏用很方便,會向上逐層搜索width值,忽視浮動層的影響,ff下搜索至浮動層結束,如此,只能給中間的全部浮動層加width:100%才行,累啊。
20.iE6的問題。當層的高度小於20px時,IE6下要定義font-size:0px;不然高度爲20px;
21.ff下,只有body和html同時定義height:100%,高度才爲100%。IE下只須要定義body.
22.div的垂直居中問題:
vertical-align:middle; 將行距增長到和整個DIV同樣高 line-height:200px; 而後插入文字,就垂直居中了。缺點是要控制內容不要換行.
23. IE捉迷藏的問題
當div應用複雜的時候每一個欄中又有一些連接,DIV等這個時候容易發生捉迷藏的問題。
有些內容顯示不出來,當鼠標選擇這個區域是發現內容確實在頁面。 解決辦法:對#layout使用line-height屬性 或者給#layout使用固定高和寬。頁面結構儘可能簡單。
24.麼web標準中IE沒法設置滾動條顏色了
解決辦法是將body換成html
25.連接(a標籤)的邊框與背景
a連接加邊框和背景色,需設置 display: block,
26..ul標籤在ff中默認是有padding值的,而在IE中只有margin有值
因此先定義ul{margin:0;padding:0;}就能解決大部分問題
27.ff: div 設置 margin-left, margin-right 爲 auto 時已經居中, IE 不行
ff: body 設置 text-align 時, div 須要設置 margin: auto(主要是 margin-left,margin-right) 方可居中
28.tu片居中,文字居左的寫法。
.abc{ text-align:left }
abc img{ display:block ; text-align:center;margin:o auto; }
29.層垂直居中的樣式:
div{
position:absolute;
top:50%;
left:50%;
margin:-117px 0 0 -187px;
width:374px;
height:234px;
background-image:url(images/beijing.jpg);
}
30.form在IE6裏面底下會多出來一行,加上style="padding:0; margin:0;"
32.空td顯示邊框 table { border-collapse:collapse; }
33.鼠標通過圖片變化圖片
function imagechange(img)
{
document.images[0].src=img;
document.images[0].width=400;
document.images[0].height=300;
}
</script>
</head>
<body>
<a href="#" onmouseout="imagechange('images1.jpg')" onmousemove="imagechange('images2.jpg')"><img src="images2.jpg" width="400" height="300" /></a>
</body>
34.表單輸入框默認文字的設置
function cleanUp(id){
if(document.getElementById("userid").value == "請輸入搜索關鍵字")
{
document.getElementById(id).value = "";
}
}
<input class="inp" id="userid" name="userid" type="text" value="請輸入搜索關鍵字" onclick="cleanUp('userid');" />
35.透明兼容寫法:
filter:alpha(opacity=70);-moz-opacity:0.7;
36 文字和圖片在同一行垂直居中問題:
一個關鍵的屬性: margin-bottom 經過修改圖片的外邊框的底部高度就可讓文字移動上來. 作法是把這個值設置爲負數.例如咱們把下面的16X15的圖片設置它的style="margin-bottom:-3px" 這樣文字和圖片就居中了
兼容性一直都是個使人頭痛的問題,下面簡單總結火狐與IE的兼容性問題。
37. 超連接訪問事後hover樣式就不出現的問題
被點擊訪問過的超連接樣式不在具備hover和active了,不少人應該都遇到過這個問題,解決方法是改變CSS屬性的排列順序: L-V-H-A
1. <style type="text/css">
2. <!--
3. a:link {}
4. a:visited {}
5. a:hover {}
6. a:active {}
7. -->
8. </style>
38. FireFox下如何使連續長字段自動換行
衆所周知IE中直接使用 word-wrap:break-word 就能夠了, FF中咱們使用JS插入的方法來解決
1. <style type="text/css">
2. <!--
3. div {
4. width:300px;
5. word-wrap:break-word;
6. border:1px solid red;
7. }
8. -->
9. </style>
10.<div id="ff">這裏是文字</div>
Js代碼
1. <scrīpt type="text/javascrīpt">
2. /* <![CDATA[ */
3. function toBreakWord(el, intLen){
4. var ōbj=document.getElementById(el);
5. var strContent=obj.innerHTML;
6. var strTemp="";
7. while(strContent.length>intLen){
8. strTemp+=strContent.substr(0,intLen)+"
9. ";
10. strContent=strContent.substr(intLen,strContent.length);
11. }
12. strTemp+="
13."+strContent;
14. obj.innerHTML=strTemp;
15. }
16. if(document.getElementById && !document.all) toBreakWord("ff", 37);
17. /* ]]> */
18. </script>
39.ff下爲何父容器的高度不能自適應
在子容器加了浮動屬性後,該容器將不能自動撐開,解決方法是在標籤結束後加上一個清除浮動的元素。
1. clear:both;
2. 樣式中加入
.clearfix:after {content:"."; display: block; height: 0; clear: both; visibility: hidden; }
.clearfix {display:inline-block;}
* html .clearfix {height: 1%;}
.clearfix {display: block;}
而後在有浮動的層的外邊加入這個clearfix的樣式,以下:
<div class="mainbox clearfix">
<!--float-left爲左浮動的樣式,float-right爲右浮動的樣式,這裏style我就再也不寫了-->
<div class="float-left"></div>
<div class="float-right"></div>
</div>
40.IE6的雙倍邊距BUG
浮動後原本外邊距10px,但IE解釋爲20px,解決辦法是加上
1. display: inline
41. IE6下絕對定位的容器內文本沒法正常選擇
此問題在IE六、7中存在,解決問題的辦法是讓IE進入到qurks mode。關於qurks mode的相關知識,請參考:
http://www.microsoft.com/..
42. IE6下圖片下方有空隙產生
解決這個BUG的方法也有不少,能夠是改變html的排版,或者設置img 爲display:block
或者設置vertical-align 屬性爲vertical-align:top bottom middle text-bottom均可以解決.
43. IE6下兩個層中間有間隙
這個IE的3PX BUG也是常常出現的,解決的辦法是給.right也一樣浮動 float:left 或者相對IE6定義.left margin-right:-3px;
44. list-style-image沒法準肯定位
list-style-image的定位問題也是常常有人問的,解決的辦法通常是用li的背景模擬,這裏採用相對定位的方法也能夠解決。
45. LI中內容超過長度後以省略號顯示的方法
此方法適用與IE與OP瀏覽器
1. <style type="text/css">
2. <! --
3. li {
4. width: 200px;
5. white-space:nowrap;
6. text-overflow:ellipsis;
7. -o-text-overflow:ellipsis;
8. overflow: hidden;
9. }
10. -->
11.</style>
46.web標準中定義id與class有什麼區別嗎
一.web標準中是不允許重複ID的,好比 div id="aa" 不允許重複2次,而class 定義的是類,理論上能夠無限重複, 這樣須要屢次引用的定義即可以 使用他.
二.屬性的優先級問題
ID 的優先級要高於class,看上面的例子
三.方便JS等客戶端腳本,若是在頁面中要對某個對象進行腳本操做,那麼能夠給他定義一個ID,不然只能利用遍歷頁面元素加上指定特定屬性來找到它,這是相對浪費時間資源,遠遠不如一個ID來得簡單.
47.如何垂直居中文本
將元素高度和行高設爲一致。
1. <style type="text/css">
2. <!--
3. div{
4. height:30px;
5. line-height:30px;
6. border:1px solid red
7. }
8. -->
9. </style>
48.如何對齊文本與文本輸入框
加上 vertical-align:middle;
1. <style type="text/css">
2. <!--
3. input {
4. width:200px;
5. height:30px;
6. border:1px solid red;
7. vertical-align:middle;
8. }
9. -->
10.</style>
49.FF下面不能水平居中呢
FF下面設置容器的左右外補丁爲auto就能夠了
1. <style type="text/css">
2. <!--
3. div{
4. margin:0 auto;
5. }
6. -->
7. </style>
50.FF下文本沒法撐開容器的高度
標準瀏覽器中固定高度值的容器是不會象IE6裏那樣被撐開的,那我又想固定高度,又想能被撐開須要怎樣設置呢?辦法就是去掉height設置min-height:200px; 這裏爲了照顧不認識min-height的IE6 能夠這樣定義:
1. {
2. height:auto!important;
3. height:200px;
4. min-height:200px;
5. }
51.IE6下容器的寬度和FF解釋不一樣呢
1. <?xml version="1.0" encoding="gb2312"?>
2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
4. <style type="text/css">
5. <!--
6. div{
7. cursor:pointer;
8. width:200px;
9. height:200px;
10. border:10px solid red
11. }
12. -->
13. </style>
14.<div onclick="alert(this.offsetWidth)">web標準常見問題大全</div>
問題的差異在於容器的總體寬度有沒有將邊框(border)的寬度算在其內,這裏IE6解釋爲200PX ,而FF則解釋爲220PX,那到底是怎麼致使的問題呢?你們把容器頂部的xml去掉就會發現原來問題出在這,頂部的申明觸發了IE的qurks mode,關於qurks mode、standards mode的相關知識,請參考:
http://www.microsoft.com/..
52. 爲何web標準中IE沒法設置滾動條顏色了
解決辦法是將body換成html
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
3. <style type="text/css">
4. <!--
5. html {
6. scrollbar-face-color:#f6f6f6;
7. scrollbar-highlight-color:#fff;
8. scrollbar-shadow-color:#eeeeee;
9. scrollbar-3dlight-color:#eeeeee;
10. scrollbar-arrow-color:#000;
11. scrollbar-track-color:#fff;
12. scrollbar-darkshadow-color:#fff;
13. }
14.-->
15.</style>
53. 爲何我定義的樣式沒有做用呢
這裏你沒法用.aa定義到li 遇到這種狀況怎麼解決呢?答案是提升.aa 的優先權 好比#aa ul li.aa
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
3. <style type="text/css">
4. <!--
5. #aa ul li {
6. color:red
7. }
8. .aa {
9. color:blue
10. }
11. -->
12.</style>
13.<div id="aa">
14.<ul>
15.<li class="aa">
16. web標準常見問題大全
17.</li>
18.</ul>
19.</div>
54. IE6沒法定義1px左右高度的容器
IE6下這個問題是由於默認的行高形成的,解決的方法也有不少,例如:
1. overflow:hidden zoom:0.08 line-height:1px
55. 背景顏色沒法顯示
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
3. <style type="text/css">
4. <!--
5. ul {
6. background:red
7. }
8. li {
9. float:left;
10. width:180px;
11. }
12.-->
13.</style>
14.<!--[if lte IE 6]>
15.<style>
16..gainlayout { height: 1px; }
17.</style>
18.<![endif]-->
19.<ul class="gainlayout">
20.<li>web標準常見問題大全</li>
21.<li>web標準常見問題大全</li>
22.<li>web標準常見問題大全</li>
23.<li>web標準常見問題大全</li>
24.<li>web標準常見問題大全</li>
25.<div style="clear:both"></div>
26.</ul>
IE中設置有背景色的ul並無顯示出來,這個屬於haslayout問題,解決的辦法也不少參考 http://www.satzansatz.de/cssd/onhavinglayout.htm
解決方法之一:
1. <!--[if lte IE 6]>
2. <style>
3. .gainlayout { height: 1px; }
4. </style>
5. <![endif]-->
6. <!--[if lte IE 6]>
7. <style>
8. .gainlayout { height: 1px; }
9. </style>
10.<![endif]-->
56. 怎麼樣才能讓層顯示在FLASH之上呢
解決的辦法是給FLASH設置透明
1. <param name="wmode" value="transparent" />
57. 怎樣使一個層垂直居中於瀏覽器中
這裏咱們使用百分比絕對定位,與外補丁負值的方法,負值的大小爲其自身寬度高度除以二
1. <style type="text/css">
2. <!--
3. div {
4. position:absolute;
5. top:50%;
6. left:50%;
7. margin:-100px 0 0 -100px;
8. width:200px;
9. height:200px;
10. border:1px solid red;
11. }
12.-->
13.</style>
58. 圖片垂直與容器內
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. <style type="text/css">
3. <!--
4. * {margin:0;padding:0}
5. div {
6. width:500px;
7. height:500px;
8. border:1px solid #ccc;
9. overflow:hidden;
10. position:relative;
11. display:table-cell;
12. text-align:center;
13. vertical-align:middle
14. }
15.div p {
16. position:static;
17. +position:absolute;
18. top:50%
19. }
20.img {
21. position:static;
22. +position:relative;
23. top:-50%;left:-50%;
24. width:276px;
25. height:110px
26. }
27.-->
28.</style>
29.<div><p><img src="logo.gif" /></p></div>
59.或者使用背景圖的辦法:
1. background:url("logo.gif") center no-repeat;
60. 如何讓div橫向排列
橫向排列DIV可使用浮動的方式好比float:left,或者設置對象爲內聯,還能夠絕對定位對象等等.
1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
3. <style type="text/css">
4. <!--
5. div {
6. float:left;
7. width:200px;
8. height:200px;
9. border:1px solid red
10. }
11.-->
12.</style>
13.<div>web標準常見問題大全</div>
14.<div>web標準常見問題大全</div>
15.<div>web標準常見問題大全</div>
61.Firefox 關於DIV高度沒法自適應的兩種解決
若是設置了一個DIV的高度,當DIV裏實際內容大於所設高度,ie會自動拉伸以適應DIV容器大小,ff會固定DIV的高度,超過部分超出DIV底線之外,
出現和下面的內容重疊的現象。若是不給DIV設置高度,在Firefox中將不回由於裏面的內容而撐開,而IE中就會自動根據內容撐開
解決方案:
第一種方法:在DIV內部的最後追加clear:both樣式
1. <div style="">
2. <div style="float:left; height:200px">Jmedia Design</div>
3. <div style="float:right; height:800px">www.jmedia.cn</div>
4. <div style="clear:both"></div>
5. </div>
第二種方法:對DIV使用overflow:auto;
1. <div style="overflow:auto;">
2. <div style="float:left; height:200px">1111111111</div>
3. <div style="float:right; height:300px"">2222222222</div>
4. </div>
62.兼容ie的圖片背景透明的方法:
第一種方法:將圖片格式定義爲png8,設置精確,抖動100%
第二種方法:利用js(js源碼以下,但圖片過多的頁面不推薦使用)
<script type="text/javascript">
var DD_belatedPNG = {
ns: 'DD_belatedPNG',
imgSize: {},
delay: 10,
nodesFixed: 0,
createVmlNameSpace: function () { /* enable VML */
if (document.namespaces && !document.namespaces[this.ns]) {
document.namespaces.add(this.ns, 'urn:schemas-microsoft-com:vml');
}
},
createVmlStyleSheet: function () { /* style VML, enable behaviors */
/*
Just in case lots of other developers have added
lots of other stylesheets using document.createStyleSheet
and hit the 31-limit mark, let's not use that method!
further reading: http://msdn.microsoft.com/en-us/library/ms531194(VS.85).aspx
*/
var screenStyleSheet, printStyleSheet;
screenStyleSheet = document.createElement('style');
screenStyleSheet.setAttribute('media', 'screen');
document.documentElement.firstChild.insertBefore(screenStyleSheet, document.documentElement.firstChild.firstChild);
if (screenStyleSheet.styleSheet) {
screenStyleSheet = screenStyleSheet.styleSheet;
screenStyleSheet.addRule(this.ns + '\\:*', '{behavior:url(#default#VML)}');
screenStyleSheet.addRule(this.ns + '\\:shape', 'position:absolute;');
screenStyleSheet.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/ */
this.screenStyleSheet = screenStyleSheet;
/* Add a print-media stylesheet, for preventing VML artifacts from showing up in print (including preview). */
/* Thanks to R閙i Pr関ost for automating this! */
printStyleSheet = document.createElement('style');
printStyleSheet.setAttribute('media', 'print');
document.documentElement.firstChild.insertBefore(printStyleSheet, document.documentElement.firstChild.firstChild);
printStyleSheet = printStyleSheet.styleSheet;
printStyleSheet.addRule(this.ns + '\\:*', '{display: none !important;}');
printStyleSheet.addRule('img.' + 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') ? 'none' : 'block';
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) {
var trans = el.currentStyle.filter;
trans = parseInt(trans.substring(trans.lastIndexOf('=')+1, trans.lastIndexOf(')')), 10)/100;
el.vml.color.shape.style.filter = el.currentStyle.filter; /* complete guesswork */
el.vml.image.fill.opacity = trans; /* complete guesswork */
}
},
handlePseudoHover: function (el) {
setTimeout(function () { /* wouldn't work as intended without setTimeout */
DD_belatedPNG.applyVML(el);
}, 1);
},
/**
* This is the method to use in a document.
* @param {String} selector - REQUIRED - a CSS selector, such as '#doc .container'
**/
fix: function (selector) {
if (this.screenStyleSheet) {
var selectors, i;
selectors = selector.split(','); /* multiple selectors supported, no need for multiple calls to this anymore */
for (i=0; i<selectors.length; i++) {
this.screenStyleSheet.addRule(selectors[i], 'behavior:expression(DD_belatedPNG.fixPng(this))'); /* seems to execute the function without adding it to the stylesheet - interesting... */
}
}
},
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;
self = this;
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 == 'static') {
el.style.position = 'relative';
}
},
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]) { /* determine size of loaded image */
img = document.createElement('img');
lib.imgSize[el.vmlBg] = img;
img.className = lib.ns + '_sizeFinder';
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! */
imgLoaded = function () {
this.width = this.offsetWidth; /* weird cache-busting requirement! */
this.height = this.offsetHeight;
lib.vmlOffsets(el);
};
img.attachEvent('onload', imgLoaded);
img.src = el.vmlBg;
img.removeAttribute('width');
img.removeAttribute('height');
document.body.insertBefore(img, document.body.firstChild);
}
el.vml.image.fill.src = el.vmlBg;
noImg = false;
}
el.vml.image.fill.on = !noImg;
el.vml.image.fill.color = 'none';
el.vml.color.shape.style.backgroundColor = elStyle.backgroundColor;
el.runtimeStyle.backgroundImage = 'none';
el.runtimeStyle.backgroundColor = 'transparent';
},
/* 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 */
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};
fudge = (size.L + size.bLW == 1) ? 1 : 0;
/* vml shape, left, top, width, height, origin */
makeVisible = function (vml, l, t, w, h, o) {
vml.coordsize = w+','+h;
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)), (size.W-1), (size.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+fudge, 'B':size.H, 'L':1+fudge}; /* these are defaults for repeat of any kind */
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+size.h), 'L':(bg.X)}; /* these are defaults for no-repeat - clips down to the image location */
if (bgR.search('repeat-') != -1) { /* now let's revert to dC for repeat-x or repeat-y */
v = bgR.split('repeat-')[1].toUpperCase();
c[altC[v].b1] = 1;
c[altC[v].b2] = size[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: function (bg, size, axis, position) {
var horizontal, fraction;
fraction = true;
horizontal = (axis == 'X');
switch(position) {
case 'left':
case 'top':
bg[axis] = 0;
break;
case 'center':
bg[axis] = 0.5;
break;
case 'right':
case 'bottom':
bg[axis] = 1;
break;
default:
if (position.search('%') != -1) {
bg[axis] = parseInt(position, 10) / 100;
}
else {
fraction = false;
}
}
bg[axis] = Math.ceil( fraction ? ( (size[horizontal?'W': 'H'] * bg[axis]) - (size[horizontal?'w': 'h'] * bg[axis]) ) : parseInt(position, 10) );
if (bg[axis] % 1 === 0) {
bg[axis]++;
}
return bg[axis];
},
fixPng: function (el) {
el.style.behavior = 'none';
var lib, els, nodeStr, v, e;
if (el.nodeName == 'BODY' || el.nodeName == 'TD' || el.nodeName == 'TR') { /* elements not supported yet */
return;
}
el.isImg = false;
if (el.nodeName == 'IMG') {
if(el.src.toLowerCase().search(/\.png$/) != -1) {
el.isImg = true;
el.style.visibility = 'hidden';
}
else {
return;
}
}
else if (el.currentStyle.backgroundImage.toLowerCase().search('.png') == -1) {
return;
}
lib = DD_belatedPNG;
el.vml = {color: {}, image: {}};
els = {shape: {}, fill: {}};
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.stroked = false;
el.vml[v].shape.appendChild(el.vml[v].fill);
el.parentNode.insertBefore(el.vml[v].shape, el);
}
}
el.vml.image.shape.fillcolor = 'none'; /* Don't show blank white shapeangle when waiting for image to load. */
el.vml.image.fill.type = 'tile'; /* Makes image show up. */
el.vml.color.fill.on = false; /* Actually going to apply vml element's style.backgroundColor, so hide the whiteness. */
lib.attachHandlers(el);
lib.giveLayout(el);
lib.giveLayout(el.offsetParent);
el.vmlInitiated = true;
lib.applyVML(el); /* Render! */
}
};
try {
document.execCommand("BackgroundImageCache", false, true); /* TredoSoft Multiple IE doesn't like this, so try{} it */
} catch(r) {}
DD_belatedPNG.createVmlNameSpace();
DD_belatedPNG.createVmlStyleSheet();
</script>
第三種方法:利用濾鏡(須要注意的是,background和*html中src裏邊圖片的路徑必須是絕對的url,並且class名不能夠有包含樣式,如ul li.class{}這樣的寫法是不行的。)
.imgbg{position:absolute;left:0;top:0;background:url(http://icon.cnmo-img.com.cn/app/ie6_img_app_11.png) no-repeat;width:60px;height:60px;z-index:1;zoom:1;cursor:pointer;}
*html .imgbg{background:none;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=scale,src='http://icon.cnmo-img.com.cn/app/ie6_img_app_11.png');}