简体   繁体   English

如何在伪元素上使用带有 AOS 的动画?

[英]how to use animation with AOS on pseudo element?

Normally in HTML file I use AOS like this通常在 HTML 文件中,我像这样使用 AOS

<li data-aos="fade-left" class="fields__box">

I try in different way use AOS with pseudo element in my CSS file but I don't do this in the right.我尝试以不同的方式在我的 CSS 文件中使用带有伪元素的 AOS,但我没有正确地这样做。 Do you have some idea how can I do that?你知道我该怎么做吗? Thanks for your help谢谢你的帮助

 &::before {
        content: "";
        position: absolute;        
        background-image: url(images/logoBig.png);
        z-index: -1;
        width: 100%;
        height: 100%;
        background-repeat: no-repeat;
        background-size: 70%;
        background-position: center bottom;
        // data-aos="fade-up";
    }

I haven't used Animate on Scroll (AOS) before, but based on a reading of the docs andCSS Tricks post , I think it can be done.我之前没有在 Scroll (AOS) 上使用过 Animate,但是根据对文档CSS Tricks 帖子的阅读,我认为可以做到。

Here's what AOS is doing, JavaScript-wise:这是 AOS 正在做的事情,JavaScript-wise:

The idea behind AOS is straightforward: watch all elements and their positions based on settings you provide them. AOS 背后的想法很简单:根据您提供的设置观察所有元素及其位置。 Then add/remove the class aos-animate .然后添加/删除类aos-animate Of course, in practice, it's not always that easy, but the idea behind AOS is as simple as that.当然,在实践中并不总是那么容易,但 AOS 背后的想法就是这么简单。 Every aspect of animation is handled by CSS.动画的每个方面都由 CSS 处理。

Based on the source code for the fade-up animation, you could try:根据fade-up动画的源代码,您可以尝试:

<li data-aos="fade-before-up" class="fields__box">
&::before {
    /* ... your properties to style the before element ... */
    transition-property: opacity, transform;
    opacity: 0    
    transform: translate3d(0, 100px, 0);
}

&.aos-animate::before {
    opacity: 1;
    transform: translate3d(0, 0, 0);
}

Key points:关键点:

  • I haven't tested this.我没有测试过这个。 Sorry.对不起。 If you'd like to create a CodePen or a JSFiddle that replicates the current behavior you're seeing, I'll see if I can tweak it.如果你想创建一个 CodePen 或一个 JSFiddle 来复制你所看到的当前行为,我会看看我是否可以调整它。
  • The data-aos attribute on the HTML element shouldn't match an actual AOS animation. HTML 元素上的data-aos属性不应与实际的 AOS 动画相匹配。 It's just to get the library to put the aos-animate class on the element.只是为了让库把aos-animate类放在元素上。
  • The ::before pseudo-element is initially styled as 0 opacity and 100px below. ::before伪元素最初的样式为 0 opacity 和 100px 下面。 When AOS adds the aos-animate class to the <li> , then the ::before element can be transitioned.当 AOS 将aos-animate类添加到<li> ,就可以转换::before元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM