css - 如何在vue组件中让元素以动画方式出现和消失?

标签 css vuejs3 quasar-framework

我正在使用 quasar vue.js 框架并拥有此组件:

<template>
  <div class="landing-section">
    <div class="landing-section__text-box">
      <p class="landing-section__text-box__title">Albert Pike</p>
      <p class="landing-section__text-box__description">
        Albert Pike (December 29, 1809 – April 2, 1891) was an American author,
        poet, orator, editor, lawyer, jurist, and prominent member of the
        Freemasons. Pike was a senior officer of the Confederate States Army who
        commanded the District of Indian Territory in the Trans-Mississippi
        Theater of the American Civil War.
      </p>
      <q-btn
        class="q-mt-md"
        :to="{ name: 'AboutUs' }"
        padding="0.8rem 2.5rem"
        outline
        rounded
        color="white"
        label="Know More"
      />
    </div>
  </div>
</template>

<style scoped>
.landing-section {
  width: 100%;
  height: 37rem;
  background: url("https://getwallpapers.com/wallpaper/full/9/a/6/163230.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 20% -1.2rem;
  display: flex;
  align-items: center;
}
.landing-section__text-box {
  width: 33rem;
  margin-left: 5%;
  margin-right: 5%;
  margin-bottom: 5rem;
}
.landing-section__text-box__title {
  color: white;
  font-size: 2rem;
  font-weight: 700;
}
.landing-section__text-box__description {
  color: white;
  font-size: 1rem;
  font-weight: 500;
}
.landing-section__text-box__learn-more-button {
  color: white;
  font-size: 1rem;
}
::v-deep(.block) {
  font-weight: 700;
}
</style>

这是最终结果:

enter image description here

现在我想向以下元素添加动画: 登陆部分__text-box__title 登陆部分__文本框__描述 q-btn

  1. 这 3 个元素应更改一次,例如每 3 秒更改一次,以显示新内容,尤其是标题和说明。
  2. 此更改应无限循环。

关于如何执行此操作有任何帮助吗?

最佳答案

Transitioning Between Elements

const vm = new Vue({
  el: '#app',
  data() {
    return {
      sections: [{
          title: "Section A",
          description: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        },
        {
          title: "Section B",
          description: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        }
      ],
      activeSectionIndex: 0,
    }
  },
  computed: {
    activeSection() {
      return this.sections[this.activeSectionIndex]
    }
  },
  methods: {
    nextSection() {
      this.activeSectionIndex++
      if(this.activeSectionIndex >= this.sections.length) this.activeSectionIndex = 0
    }
  },
  mounted() {
    this.timer = setInterval(() => this.nextSection(), 3000)
  },
  destroyed() {
    clearInterval(this.timer)
  }  
})
.slide-fade-enter-active {
  transition: all .8s ease;
}
.slide-fade-leave-active {
  transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active below version 2.1.8 */ {
  transform: translateX(10px);
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.js"></script>
<div id="app">
  <transition name="slide-fade" mode="out-in">
    <div :key="activeSection.title">
      <p> {{ activeSection.title }} </p>
      <p> {{ activeSection.description }} </p>
    </div>
  </transition>
</div>

关于css - 如何在vue组件中让元素以动画方式出现和消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68623412/

相关文章:

c# - 如何在 MVC 中使用 GET 方法进行搜索

javascript - 如何在响应式设计中禁用此 JS 功能

javascript - 根据数据改变背景(Vue3)

javascript - Vue 3 使用异步 json 设置响应式获取

javascript - 如何测试 Quasar(作为 Vue CLI 插件)?

javascript - 为什么 Vue 应用程序中 Quasar 抽屉覆盖了 Quasar 工具栏?

javascript - ExtJS 6 图表 -- 更改标题颜色

css - 供应商前缀导致 css 失败

vue.js - vue3 ssr 不返回纯 html

vue.js - 如何安装旧版本的vite