对的,这是可能的。
demo http://codepen.io/thebabydino/pen/AtzpB
.boo {
position: relative;
width: 20em; min-height: 10em;
background: rgba(0,0,0,0) url(http://placekitten.com/320/160);
transition: background-color 1s;
}
.boo:hover {
background-color: rgba(0,0,0,.5);
}
.boo:before {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
background-color: inherit;
content: ' ';
}
我在这是要干嘛?
我在这里所做的是设置 RGBabackground-color
on the div
,其背后background-image
并过渡这个background-color
(其阿尔法):hover
。这一切的发生behind the background-image
。不过,我也在使用background-color: inherit
在伪元素上,这意味着在任何给定时刻,位于其父元素之上的伪元素div
(因此高于background-image
of the div
)将会有相同的background-color
(这意味着background-color
伪元素的将从rgba(0,0,0,0)
to rgba(0,0,0,.5)
on :hover
).
为什么要这样做呢?
我不直接过渡的原因background-color
伪元素的一个问题是对伪元素转换的支持还不是很好。
支持伪元素的转换
✓ 火狐浏览器支持伪元素的转换并且已经支持它们很长一段时间了,让我们先解决这个问题。
✗当前版本Safari and Opera不支持伪元素的转换。
Chrome仅从版本 26 开始支持伪元素的转换。
IE10以一种有点奇怪的方式支持它们,这意味着:
.boo:before { color: blue; transition: 1s; }
.boo:hover:before { color: red; }
不起作用,您还必须指定元素本身的悬停状态。像这样:
.boo:hover {}
.boo:before { color: blue; transition: 1s; }
.boo:hover:before { color: red; }
有关如何使用此转换伪元素的各种属性的更多信息和示例inherit
技术:http://vimeo.com/51897358 http://vimeo.com/51897358
EDIT
自切换到 Blink 以来,Opera 和 Safari 自 6.1 以来现在支持直接在伪元素上进行转换。