欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
    currentColor顾名思意就是“当前颜色”,准确讲应该是“当前的文字颜色”,例如:
    .xxx{border:1pxsolidcurrentColor;}
    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:00;}
    .icon2{background-position:-20px0;}
    .icon3{background-position:-40px0;}
    .icon4{background-position:-60px0;}
    .link{margin-right:15px;}
    html代码:
    更改颜色:<inputid="colorInput"type="color"value="#34538b"autocomplete="off">
    <p>
    <iclass="iconicon1"></i>返回
    <iclass="iconicon2"></i>刷新
    <iclass="iconicon3"></i>收藏
    <iclass="iconicon4"></i>展开图片
    </p>
    js代码:
    vareleInput=document.getElementById("colorInput"),
    eleIcons=document.getElementsByTagName("i");
    eleInput.onchange=function(){vari=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;
    在iOSSafari浏览器下(iOS8)下,currentColor还是有一些bug的,例如伪元素hover时候,background:currentColor的背景色不会跟着变化,怎么办呢?等升级,或者使用border来模拟。
    currentColor浏览器兼容情况:
    支持的浏览器:谷歌,火狐,QQ浏览器,IE9+
    不支持的浏览器:360,IE低版本浏览器








本文转载自中文网
 

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/css3-style/c57188.shtml