標籤:
css實現三角形,網上講了很多,但我發現一般都是三角向上或者向下的,向左向右這兩方向似乎講得很少,本人試了一下,發現原來在IE下很難搞~~(萬惡的IE)...
css實現三角形的原理是:當元素的寬高為0,邊框(border)不為0時,四個角邊框交界重疊處分45度角平分。講得很彆扭~~~上個圖吧:
.triangle {
border-color: red green blue pink;
border-style: solid;
border-width: 20px 20px 20px 20px;
width: 0;
height: 0;
}
<div class="triangle"/>
</div>
那麼,我們只有留下一條邊框的時候會發現什嗎??
.triangle {
border-color: red transparent transparent transparent;
border-style: solid;
border-width: 20px 20px 0px 20px;
width: 0;
height: 0;
}
怎樣?出來了吧~~
同樣道理,我們改成為向左向右的,
向左: .triangle {
border-color: transparent red transparent transparent;
border-style: solid;
border-width: 20px 20px 20px 0px;
width: 0;
height: 0;
}
向右: .triangle {
border-color: transparent transparent transparent red;
border-style: solid;
border-width: 20px 0px 20px 20px;
width: 0;
height: 0;
}
大功造成!? 慢,你用的是什麼瀏覽器?如果用的是非IE6的話,恭喜你!下面我們用IE6(咬牙切齒ing...),
汗~~~~
原來,IE6預設給了背景黑色~~
只有好用IE專有的東東了,解鈴還需系鈴人(~_~)
首先,css hack,用底線"_"!
_border-top-color: white;
_border-bottom-color: white;
然後用chroma filter : _filter: chroma( color =white);
其實就是把不要的邊過濾掉!
還要加上 font-size: 0; line-height: 0;
這樣就徹底的去掉了黑色背景:
.triangle {
border-color: transparent transparent transparent red;
border-style: solid;
border-width: 20px 0px 20px 20px;
font-size: 0;
line-height: 0;
width: 0;
height: 0;
_border-top-color: white;
_border-bottom-color: white;
_filter: chroma( color =white);
}
html+css:
Js代碼
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>css三角形</title>
- <style>
- .tipArrow {
- /*右邊有顏色,其他透明*/
- border-color: transparent #e00 transparent transparent;
- border-style: solid;
- border-width: 6px 6px 6px 0px;
- padding: 0;
- width: 0;
- height: 0;
- /* ie6 height fix */
- font-size: 0;
- line-height: 0;
- /* ie6 transparent fix */
- _border-top-color: #dddddd;
- _border-bottom-color: #dddddd;
- _filter: chroma( color = #dddddd);
- }
- </style>
- </head>
- <body>
- <div class="tipArrow"/>
- </div>
- </body>
- </html>
知道有些同學要直接看效果的,哈哈:看
下載
三角形是出來了,那我們看一個應用的例子(結合了之前寫的fadeIn,fadeOut):
http://kingkit.com.cn/KUI/Tip.html
完整打包
css實現三角形及應用樣本