標籤:style blog http color os io 使用 ar for
網上有各種用CSS3畫表徵圖,畫漫畫的代碼,實在是有點吊!如果能為自己的網站也繪製一套CSS3表徵圖,那麼就省去了用小圖片的力氣。雖然說各大瀏覽器對CSS3的支援性還不盡相同,但是大勢所趨,寫寫更健康。
首先,我們要做到使用簡單,簡單到給元素加個類就能使用表徵圖,然後我們還要支援可定製,比如顏色,大小。看下面的代碼:
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>CSS3 ICONS</title></head><body><div class="container"><a class="css3-icon css3-icon-search" href="#"></a></div></body></html>
我們只要給a標籤加上兩個類就可以讓它變成一個表徵圖。之所以加兩個,是因為類css3-icon實現的表徵圖的基本樣式,而css3-icon-search則實現了具體的search表徵圖樣式:
/*global icon style*/.css3-icon{position:relative;display:block;/*custom the icon size*/width:100px;height:100px;font-size:10px;background:#ccc;}.css3-icon:before,.css3-icon:after{content:"";position:absolute;left:0;background:transparent;font-size:1em;}
虛擬元素肯定是要用上的,而且要相對於.css3-icon絕對位置。我們希望表徵圖的大小是可以自訂的,所以在:before和:after虛擬元素裡我們使用了相對單位em,為此要在.css3-icon中對font-size重新賦初始值。
接下來我們畫一個search表徵圖:
/*---------------------search icon----------------------*/.css3-icon-search:after {left:55%;top:70%; width:40%;height:12%;background:#456;transform:rotate(45deg);-webkit-transform:rotate(45deg);-moz-transform:rotate(45deg);-o-transform:rotate(45deg);}.css3-icon-search:before {left:10%;top:5%; width:50%;height:50%; border:.9em solid #456; border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%;--o-border-radius:50%;}.css3-icon-search:hover:before{border-color:#ff7;}.css3-icon-search:hover:after{background:#ff7;}在before虛擬元素中,利用border-radiuis畫一個圓,在after虛擬元素中,利用transform畫出放大鏡的手柄。為了支援表徵圖縮放,這裡都使用了百分比,效果如下:
要修改大小的話只要改變.css3-icon中的width,height,font-size即可(需要等比例改變大小)。但是還有一個問題,目前不支援自訂色彩。我們還需要改進代碼,在.css-icon中設定顏色,具體表徵圖的顏色則採用繼承的方式:
/*--------------------------------------pure css iconsauthor:liquanfengcreated:2014/9/4version:1.0blog:http://readit.sinaapp.com---------------------------------------*//*global icon style*/.css3-icon{position:relative;display:block;/*custom the icon size*/width:100px;height:100px;font-size:10px;background:#ccc;border-color:#456;}.css3-icon:hover:before,.css3-icon:hover:after{border-color:#ff7;/*custom your hover effect*/}.css3-icon:focus:before,.css3-icon:focus:after{border-color:#ff7;/*custom your focus effect*/}.css3-icon:active:before,.css3-icon:active:after{border-color:#ff7;/*custom your active effect*/}.css3-icon:visited:before,.css3-icon:visited:after{border-color:#ff7;/*custom your visited effect*/}.css3-icon:before,.css3-icon:after{content:"";position:absolute;left:0;background:transparent;font-size:1em;}/*global icon style end*//*---------------------search icon----------------------*/.css3-icon-search:before { left:10%;top:5%; width:50%;height:50%; border:.9em solid; border-color:inherit; border-radius:50%;-webkit-border-radius:50%;-moz-border-radius:50%;--o-border-radius:50%;}.css3-icon-search:after { left:55%;top:70%; width:40%;height:0; border-top:.9em solid; border-color:inherit; transform:rotate(45deg);-webkit-transform:rotate(45deg);-moz-transform:rotate(45deg);-o-transform:rotate(45deg);}以上就是所有代碼了,其他的表徵圖都可以按這個架構來實現,利用旋轉和border特性來畫表徵圖,然後在global style裡面自訂尺寸和顏色就ok了。
純CSS3實現支援自訂設定的表徵圖