向元素添加动画时,h1 元素上的数据文本属性不会显示

发布于 2025-01-11 12:35:09 字数 2290 浏览 0 评论 0原文

我有一个 h1 元素,我希望它不可见,然后在几秒钟后出现。该元素有一个 ::after 伪元素,它显示隐藏 h1 元素的数据文本属性。当我添加动画时,我只看到 h1 而不是伪元素。这是我的代码

编辑 将动画添加到伪元素使其出现,但现在数据文本出现在 h1 元素上方,而原本它应该位于 h1 元素后面。这是正在发生的事情的一些图片。第一个是它正在做什么,第二个是我想要什么。

编辑2 可以通过删除伪元素上的 z 索引来重现该问题。

错误

右

  <section class="wrapper">
        <div class="content-wrapper">
            <img class="flipInY" src="./img/neon-blue - flip.png" alt="logo-triangle">
            <h1 class="name-ani" data-text="My Name">My Name</h1>
            <h2 data-text="web-dev">Web Developer</h2>
        </div>
    </section>



.wrapper {
  .content-wrapper {
    z-index: 0;
    position: relative;
    display: grid;
    justify-items: center;
    margin-top: 20vh;
    img {
      width: 350px;
      max-width: 100%;
      padding: 0 32px;
      // transform: rotate(180deg);
      filter: brightness(85%);
      position: relative;
    }
    h1 {
      background: linear-gradient(
        to bottom,
        #ebf1f6 0%,
        #abd3ee 50%,
        #859ee2 51%,
        #d5ebfb 100%
      );
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
      font-family: kimberly;
      text-transform: uppercase;
      font-size: 2.25rem;
      transition: font-size 1s;
      position: absolute;
      top: 10%;
      opacity: 0;
      &::after {
        background: none;
        content: attr(data-text);
        left: 0;
        position: absolute;
        text-shadow: 1px -1px 0 rgba(255, 255, 255, 0.5),
          3px 1px 3px rgba(255, 0, 255, 0.85),
          -3px -2px 3px rgba(0, 0, 255, 0.85),
          1px -2px 0 rgba(255, 255, 255, 0.8);
        z-index: -10;
        opacity: 0;
      }
    }

.name-ani {
  animation-name: name-ani;
  animation-duration: 250ms;
  animation-delay: 4.5s;
  animation-fill-mode: forwards;
}

@keyframes name-ani {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

I have an h1 element that I want to be invisible and then appear after a few seconds. The element has an ::after pseudo-element that displays a data-text attribute that shadows the h1 element. When I add the animation, I only see the h1 and not the pseudo-element as well. Here is my code

EDIT
adding the animation to the pseudo-element makes it appear, but now the data-text appears over the h1 element when originally it is supposed to go behind it. Here are some pic of what is happening. The first is what it is doing and the second is what I want.

EDIT 2
The problem can be recreated by removing the z-index on the pseudo-element.

Wrong

Right

  <section class="wrapper">
        <div class="content-wrapper">
            <img class="flipInY" src="./img/neon-blue - flip.png" alt="logo-triangle">
            <h1 class="name-ani" data-text="My Name">My Name</h1>
            <h2 data-text="web-dev">Web Developer</h2>
        </div>
    </section>



.wrapper {
  .content-wrapper {
    z-index: 0;
    position: relative;
    display: grid;
    justify-items: center;
    margin-top: 20vh;
    img {
      width: 350px;
      max-width: 100%;
      padding: 0 32px;
      // transform: rotate(180deg);
      filter: brightness(85%);
      position: relative;
    }
    h1 {
      background: linear-gradient(
        to bottom,
        #ebf1f6 0%,
        #abd3ee 50%,
        #859ee2 51%,
        #d5ebfb 100%
      );
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
      font-family: kimberly;
      text-transform: uppercase;
      font-size: 2.25rem;
      transition: font-size 1s;
      position: absolute;
      top: 10%;
      opacity: 0;
      &::after {
        background: none;
        content: attr(data-text);
        left: 0;
        position: absolute;
        text-shadow: 1px -1px 0 rgba(255, 255, 255, 0.5),
          3px 1px 3px rgba(255, 0, 255, 0.85),
          -3px -2px 3px rgba(0, 0, 255, 0.85),
          1px -2px 0 rgba(255, 255, 255, 0.8);
        z-index: -10;
        opacity: 0;
      }
    }

.name-ani {
  animation-name: name-ani;
  animation-duration: 250ms;
  animation-delay: 4.5s;
  animation-fill-mode: forwards;
}

@keyframes name-ani {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

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

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

发布评论

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

评论(1

人间不值得 2025-01-18 12:35:09

您还需要将动画应用到伪元素。

.name-ani,
.name-ani::after {
    animation-name: name-ani;
    animation-duration: 250ms;
    animation-delay: 4.5s;
    animation-fill-mode: forwards;
}

编辑:通过包装它并给它一个 display: inline 来摆脱 h1 上的 position:absolute 也解决了我的堆叠顺序。

 <section class="wrapper">
   <div class="content-wrapper">
     <img class="flipInY" src="./img/neon-blue - flip.png" alt="logo-triangle">
     <div class="h1-wrapper">
       <h1 class="name-ani" data-text="My Name">My Name</h1>
     </div>
     <h2 data-text="web-dev">Web Developer</h2>
   </div>
 </section>


.wrapper {
  .content-wrapper {
    z-index: 0;
    position: relative;
    display: grid;
    justify-items: center;
    margin-top: 20vh;

    img {
      width: 350px;
      max-width: 100%;
      padding: 0 32px;
      filter: brightness(85%);
      position: relative;
    }

    .h1-wrapper {
      position: absolute;
      top: 10%;
    }

    h1 {
      background: linear-gradient(to bottom,
          #ebf1f6 0%,
          #abd3ee 50%,
          #859ee2 51%,
          #d5ebfb 100%);
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
      font-family: kimberly;
      text-transform: uppercase;
      font-size: 2.25rem;
      transition: font-size 1s;
      display: inline;
      opacity: 0;

      &::after {
        background: none;
        content: attr(data-text);
        left: 0;
        position: absolute;
        text-shadow: 1px -1px 0 rgba(255, 255, 255, 0.5),
          3px 1px 3px rgba(255, 0, 255, 0.85),
          -3px -2px 3px rgba(0, 0, 255, 0.85),
          1px -2px 0 rgba(255, 255, 255, 0.8);
        z-index: -10;
        opacity: 0;
      }
    }

    .name-ani,
    .name-ani::after {
      animation-name: name-ani;
      animation-duration: 250ms;
      animation-delay: 4.5s;
      animation-fill-mode: forwards;
    }

    @keyframes name-ani {
      0% {
        opacity: 0;
      }

      100% {
        opacity: 1;
      }
    }
  }
}

You need to apply the animation to the pseudo-element as well.

.name-ani,
.name-ani::after {
    animation-name: name-ani;
    animation-duration: 250ms;
    animation-delay: 4.5s;
    animation-fill-mode: forwards;
}

Edit: Getting rid of the position: absolute on the h1 by wrapping it and giving it an display: inline solved the stacking order for me as well.

 <section class="wrapper">
   <div class="content-wrapper">
     <img class="flipInY" src="./img/neon-blue - flip.png" alt="logo-triangle">
     <div class="h1-wrapper">
       <h1 class="name-ani" data-text="My Name">My Name</h1>
     </div>
     <h2 data-text="web-dev">Web Developer</h2>
   </div>
 </section>


.wrapper {
  .content-wrapper {
    z-index: 0;
    position: relative;
    display: grid;
    justify-items: center;
    margin-top: 20vh;

    img {
      width: 350px;
      max-width: 100%;
      padding: 0 32px;
      filter: brightness(85%);
      position: relative;
    }

    .h1-wrapper {
      position: absolute;
      top: 10%;
    }

    h1 {
      background: linear-gradient(to bottom,
          #ebf1f6 0%,
          #abd3ee 50%,
          #859ee2 51%,
          #d5ebfb 100%);
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
      font-family: kimberly;
      text-transform: uppercase;
      font-size: 2.25rem;
      transition: font-size 1s;
      display: inline;
      opacity: 0;

      &::after {
        background: none;
        content: attr(data-text);
        left: 0;
        position: absolute;
        text-shadow: 1px -1px 0 rgba(255, 255, 255, 0.5),
          3px 1px 3px rgba(255, 0, 255, 0.85),
          -3px -2px 3px rgba(0, 0, 255, 0.85),
          1px -2px 0 rgba(255, 255, 255, 0.8);
        z-index: -10;
        opacity: 0;
      }
    }

    .name-ani,
    .name-ani::after {
      animation-name: name-ani;
      animation-duration: 250ms;
      animation-delay: 4.5s;
      animation-fill-mode: forwards;
    }

    @keyframes name-ani {
      0% {
        opacity: 0;
      }

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