vue.js 完成后无法停止文本渐变动画重置

xuo3flqw  于 2023-06-24  发布在  Vue.js
关注(0)|答案(1)|浏览(147)

我需要复制this animation,我有麻烦,试图找出如何让文本保持滑动一旦动画完成。
这是我目前为止的CSS代码

.title{
    pointer-events: none;
    grid-area: 1/2/2/3;
    place-self: center;
    position: relative;
    background: linear-gradient(90deg,
      rgba(255,255,255,0) 0%,
      rgba(255,255,255,1) 5%,
      rgba(255,255,255,1) 100%);
    background-size: 130%;
    background-position-x: 50%;
    background-repeat: no-repeat;
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

@keyframes titleAnimationSequence{
    0% {
      background-position-x: 50%;
    }
    100% {
      background-position-x: -700%;
      opacity: 0;
    }
  }

  .titleAnimation-enter-active{
    animation: titleAnimationSequence .3s ease-out;
    animation-fill-mode: forwards;
    -webkit-animation-fill-mode: forwards;

我的Vue模板看起来像这样

<template>
    <!--stuff-->
    <h1 v-if="!plusButtonComplete" class="title align-middle sm:text-[length:var(--text-ultraJumbotron-2)] md:text-[length:var(--text-ultraJumbotron-3)] z-30">
        Explore
      </h1>
      <Transition name="titleAnimation">
        <h1 v-if="plusButtonComplete" class="title align-middle sm:text-[length:var(--text-ultraJumbotron-2)] md:text-[length:var(--text-ultraJumbotron-3)]">
            Explore
        </h1>
      </Transition>
    <!-- more stuff -->
</template>

我想animation-fill-mode: forwards部分会阻止它重置,但它只是在动画完成后回到初始状态。我如何阻止这种情况发生,直到我想切换回来?

dhxwm5r4

dhxwm5r41#

基本上,animation-fill-mode: forwards将保留由最后一个关键帧设置的样式值。但是当你使用vue Transition组件时,类titleAnimation-enter-active是:
在整个进入阶段。在插入元素之前添加,在过渡/动画结束时删除
见文档
当类被移除时,所有CSS属性(包括动画)都将从元素中移除。所以你的风格不会被应用。

解决方案

  • 将所有需要的样式添加到元素类(.title),过渡活动类
  • 向过渡类添加动画样式
方案一:使用CSS过渡
const {
  createApp,
  ref
} = Vue

createApp({
  setup() {
    const showHero = ref(false)
    return {
      showHero
    }
  }
}).mount('#app')
.hero {
  padding: 3em 5em;
  border: solid 1px black;
  text-align: center;
  transition: 3s transform;
  will-change: transform;
}

h1 {
  color: green;
  font-size: 5em;
}

/* for entering animation */

.hero-enter-from {
  transform: scale(0);
}

/* for leaving animation */

.hero-leave-to {
  transform: scale(0);
}
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>

<div id="app">
  <button @click="showHero = !showHero">Toggle</button>
  <Transition name="hero">
    <div class="hero" v-if="showHero">
      <h1>I'm growing</h1>
    </div>
  </Transition>
</div>
方案二:使用CSS动画
const {
  createApp,
  ref
} = Vue

createApp({
  setup() {
    const showHero = ref(false)
    return {
      showHero
    }
  }
}).mount('#app')
.hero {
  padding: 3em 5em;
  border: solid 1px black;
  text-align: center;
  will-change: transform;
}

h1 {
  color: green;
  font-size: 5em;
}

/* for entering animation */

.hero-enter-active {
  animation: upScale 3s;
}

/* for leaving animation */

.hero-leave-active {
  animation: downScale 3s;
}

@keyframes upScale {
  from {
    transform: scale(0);
  }
  to {
    transform: scale(1);
  }
}

@keyframes downScale {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(0);
  }
}
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<div id="app">
  <button @click="showHero = !showHero">Toggle</button>
  <Transition name="hero">
    <div class="hero" v-if="showHero">
      <h1>I'm growing</h1>
    </div>
  </Transition>
</div>

相关问题