你用過css3的這個currentColor新屬性嗎?使用與相容性

來源:互聯網
上載者:User

標籤:20px   .com   height   lan   ons   背景色   漸層   das   rdp   

currentColor顧名思意就是“使用中色彩”,準確講應該是“當前的文字顏色”,例如:

.xxx { border: 1px solid currentColor; }

currentColor表示“當前的標籤所繼承的文字顏色”,換種方式表示就是:currentColor = color的值。

凡事需要使用顏色值的地方,都可以使用currentColor替換,比方說背景色 – background-color, 漸層色 – gradient, 盒陰影 – box-shadow, SVG的填充色 – fill等等。很靈活,很好用!

當然可以使用css實現背景色鏤空,可以方便控製圖標的顏色。實現的原理是表徵圖形狀地區是透明鏤空的,而周邊是實色的。

CSS代碼:.icon {    display: inline-block;    width: 16px; height: 20px;    background-image: url(sprite_icons.png);    background-color: #34538b; /* 該顏色控製圖標的顏色 */}.icon1 { background-position: 0 0; }.icon2 { background-position: -20px 0; }.icon3 { background-position: -40px 0; }.icon4 { background-position: -60px 0; }.link { margin-right: 15px; }HTML代碼:更改顏色:<input id="colorInput" type="color" value="#34538b" autocomplete="off"><p>    <i class="icon icon1"></i><a href="##" class="link">返回</a>    <i class="icon icon2"></i><a href="##" class="link">重新整理</a>    <i class="icon icon3"></i><a href="##" class="link">收藏</a>    <i class="icon icon4"></i><a href="##" class="link">展開圖片</a></p>JS代碼:var eleInput = document.getElementById("colorInput"),    eleIcons = document.getElementsByTagName("i");eleInput.onchange = function() {    var i = 0, l = eleIcons.length;    for (; i<l; i+=1) {        eleIcons[i].style.backgroundColor = this.value;    }};

只需要改變背景圖片的color就更改變圖片的顏色。IE低版本也支援。

效果地址:http://www.zhangxinxu.com/study/201307/background-color-insert-background-image.html

那麼現在使用currentColor來實現這個效果:

.icon {    display: inline-block;    width: 16px; height: 20px;    background-image: url(../201307/sprite_icons.png);    background-color: currentColor; /* 該顏色控製圖標的顏色 */}

於是,我們想要滑鼠hover文字連結,其表徵圖顏色要跟著一起變化,只要改變文字顏色就可以了:

.link:hover { color: #333; }/* 雖然改變的是文字顏色,但是表徵圖顏色也一起變化了 */

說明:

border和box-shadow預設的顏色就是當前的文字顏色,也就是類似currentColor;

在iOS Safari瀏覽器下(iOS8)下,currentColor還是有一些bug的,例如虛擬元素hover時候,background:currentColor的背景色不會跟著變化,怎麼辦呢?等升級,或者使用border來類比。

currentColor瀏覽器安全色情況:

支援的瀏覽器:Google,Firefox,QQ瀏覽器,IE9+

不支援的瀏覽器:360,IE低版本瀏覽器

詳細介紹請查看:http://www.zhangxinxu.com/wordpress/2014/10/currentcolor-css3-powerful-css-keyword/

你用過css3的這個currentColor新屬性嗎?使用與相容性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.