在 :after/:before 伪类中对 CSS 过渡属性进行动画处理
是否可以对 CSS 伪类进行动画处理?
假设我有:
#foo:after {
content: '';
width: 200px;
height: 200px;
background: red;
display: block;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
}
#foo:hover:after {
width: 250px;
height: 250px;
}
这可能吗?我一直在测试,到目前为止我似乎找不到解决方案。我正在尝试通过使用 Modernizr 来减少我需要的 JavaScript 支持量。
我已经有一个 JavaScript 方法,所以请不要使用 JavaScript 替代方案。
Is it possible to animate CSS pseudo-classes?
Say I have:
#foo:after {
content: '';
width: 200px;
height: 200px;
background: red;
display: block;
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
}
#foo:hover:after {
width: 250px;
height: 250px;
}
Is this even possible? I've been testing and so far I can't seem to find a solution. I'm trying to trim down the amount of JavaScript support I need by using Modernizr.
I already have a JavaScript method, so please no JavaScript alternatives.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的小提琴在 Firefox 中对我有用。据我所知,如果 这篇文章< /a> 是最新的,这是唯一可以为伪元素设置动画的浏览器。
编辑:截至 2016 年,文章链接已损坏,相关的 WebKit 错误已修复 4 年以前。继续阅读以查看其他答案,这个答案已过时。
Your fiddle does work for me in Firefox. And as far as I know, and if this article is up to date this is the only browser that can animate pseudo-elements.
EDIT: As of 2016, the link to article is broken and the relevant WebKit bug was fixed 4 years ago. Read on to see other answers, this one is obsolete.
Google Chrome 在版本 27.0.1453.110 m 中添加了 webkit 错误修复
此 WebKit 错误已修复:http://trac.webkit.org/changeset/138632
可以制作伪动画
:before
和:after
,这里有几个为伪元素设置动画:before
和:after
的示例>。这是对上面示例的修改,进行了一些编辑,使其动画进出更加流畅,而不会在悬停时模拟
mouseleave
/mouseout
延迟:http://jsfiddle.net/MxTvw/234/
尝试添加主选择器
#foo
与第二个:hover
伪类规则中的分组:hover
伪类。还要添加transition
属性,而不仅仅是transition
的供应商特定前缀属性:请注意,前进任何伪元素,例如
:before
和 < code>:after 应使用双冒号语法::before
和::after
。此示例使用悬停时的覆盖background-color
和background-image
模拟淡入和淡出:http://jsfiddle.net/MxTvw/233/
此示例模拟悬停时的旋转动画:
http://jsfiddle.net/Jm54S/28/
当然,随着 css3 标准的发展,我们可以使用 <代码>::之前和<代码>::之后。
这适用于最新的 Firefox、Chrome、Safari、Opera 18+、IE10、IE11(IE9 及更低版本不支持 css3 过渡或动画。)
Google Chrome added the webkit bug fix to Version 27.0.1453.110 m
This WebKit bug was fixed: http://trac.webkit.org/changeset/138632
IT IS POSSIBLE to animate pseudo
:before
and:after
, here are a couple of examples for animating the psuedo-elements:before
and:after
.Here is a modification of your example above with some edits, that make it animate in and out more smoothly without the simulated
mouseleave
/mouseout
delay on hover:http://jsfiddle.net/MxTvw/234/
Try adding the main selector
#foo
with the grouped:hover
pseudo-class in your second:hover
pseudo-class rule. Also add thetransition
property, and not just the vendor specific prefixed properties fortransition
:Note that going forward any Pseudo Elements like
:before
and:after
should use the double colon syntax::before
and::after
. This example simulates a fade in and out using a overlaybackground-color
and abackground-image
on hover:http://jsfiddle.net/MxTvw/233/
This example simulates a animate of rotation on hover:
http://jsfiddle.net/Jm54S/28/
Of course moving forward with css3 standards we could use
::before
and::after
.This works in latest Firefox, Chrome, Safari, Opera 18+, IE10, IE11 (IE9 and below do not support css3 transitions or animate.)
它现在似乎不起作用,但是根据W3 规范 您可以将过渡应用于伪元素。也许在未来的某个时候……
It does not seem to work now, however according to the W3 specs you can apply transitions to pseudo-elements. Maybe some time in future…
我刚刚发现你可以为 :after 和 :before (:
代码示例 -
我有 div .model-item 制作动画,我需要为他的 :after 制作动画。
所以我添加了另一个改变背景的类,并将转换代码添加到 main :after (动画之前)
I just found out that you can animate :after and :before (:
Code example-
I have the div .model-item and i needed to animate his :after.
So i've added another class which changes background and i added the transition code to the main :after (before animation)