在 :after/:before 伪类中对 CSS 过渡属性进行动画处理

发布于 2024-12-10 08:07:53 字数 607 浏览 0 评论 0原文

是否可以对 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 替代方案。

演示http://jsfiddle.net/MxTvw/

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.

Demo: http://jsfiddle.net/MxTvw/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

要走就滚别墨迹 2024-12-17 08:07:53

你的小提琴在 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.

风吹雨成花 2024-12-17 08:07:53

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 的供应商特定前缀属性:

#foo:after{
   display: block;
   content: '';
   width: 200px;
   height: 200px;
   background: red;
   -webkit-transition: all .4s ease-in-out;
   -moz-transition: all .4s ease-in-out;
   -o-transition: all .4s ease-in-out;
   transition: all .4s ease-in-out;
} 

#foo,
#foo:hover:after,
#foo:hover:before{
   width: 250px;
   height: 250px;
}

请注意,前进任何伪元素,例如 :before 和 < code>:after 应使用双冒号语法 ::before::after。此示例使用悬停时的覆盖 background-colorbackground-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 the transition property, and not just the vendor specific prefixed properties for transition:

#foo:after{
   display: block;
   content: '';
   width: 200px;
   height: 200px;
   background: red;
   -webkit-transition: all .4s ease-in-out;
   -moz-transition: all .4s ease-in-out;
   -o-transition: all .4s ease-in-out;
   transition: all .4s ease-in-out;
} 

#foo,
#foo:hover:after,
#foo:hover:before{
   width: 250px;
   height: 250px;
}

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 overlay background-color and a background-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.)

夜灵血窟げ 2024-12-17 08:07:53

现在似乎不起作用,但是根据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…

千笙结 2024-12-17 08:07:53

我刚刚发现你可以为 :after 和 :before (:

代码示例 -

  .model-item {
    position: relative;

    &:after {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      right: 0;
      background: transparent;
      transition: all .3s;
    }
  }

  .opc {
    &:after {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      right: 0;
      background: rgba(0, 51, 92, .75);
    }
  }

我有 div .model-item 制作动画,我需要为他的 :after 制作动画。
所以我添加了另一个改变背景的类,并将转换代码添加到 main :after (动画之前)

I just found out that you can animate :after and :before (:

Code example-

  .model-item {
    position: relative;

    &:after {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      right: 0;
      background: transparent;
      transition: all .3s;
    }
  }

  .opc {
    &:after {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      right: 0;
      background: rgba(0, 51, 92, .75);
    }
  }

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文