首页 技术 正文
技术 2022年11月12日
0 收藏 979 点赞 3,559 浏览 1828 个字

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="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="link">返回</a>
<i class="icon icon2"></i><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="link">刷新</a>
<i class="icon icon3"></i><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="link">收藏</a>
<i class="icon icon4"></i><a href="##" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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; }/* 虽然改变的是文字颜色,但是图标颜色也一起变化了 */

说明:

borderbox-shadow默认的颜色就是当前的文字颜色,也就是类似currentColor

在iOS Safari浏览器下(iOS8)下,currentColor还是有一些bug的,例如伪元素hover时候,background:currentColor的背景色不会跟着变化,怎么办呢?等升级,或者使用border来模拟。

currentColor浏览器兼容情况:

支持的浏览器:谷歌,火狐,QQ浏览器,IE9+

不支持的浏览器:360,IE低版本浏览器

详细介绍请查看:http://www.zhangxinxu.com/wordpress/2014/10/currentcolor-css3-powerful-css-keyword/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297