当前位置:首页 > VUE

vue不点击实现动画

2026-02-21 11:31:06VUE

Vue 实现非点击触发的动画方法

使用 v-ifv-show 结合生命周期钩子

通过组件的挂载和卸载自动触发动画,适合页面加载或条件渲染场景。

<template>
  <transition name="fade">
    <div v-if="show">内容</div>
  </transition>
</template>

<script>
export default {
  data() {
    return { show: false }
  },
  mounted() {
    this.show = true // 自动触发入场动画
  }
}
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

使用 Intersection Observer API

当元素进入视口时触发动画,适合滚动展示效果。

<template>
  <div ref="target">
    <transition name="slide">
      <div v-if="isVisible">滚动到此处时显示</div>
    </transition>
  </div>
</template>

<script>
export default {
  data() {
    return { isVisible: false }
  },
  mounted() {
    const observer = new IntersectionObserver((entries) => {
      this.isVisible = entries[0].isIntersecting
    })
    observer.observe(this.$refs.target)
  }
}
</script>

使用 CSS @keyframes 自动动画

通过纯 CSS 实现无需交互的循环动画。

<template>
  <div class="auto-rotate">旋转元素</div>
</template>

<style>
.auto-rotate {
  animation: rotate 2s linear infinite;
}
@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
</style>

定时器控制动画

通过 setTimeoutsetInterval 实现延迟动画。

<template>
  <transition name="bounce">
    <div v-if="active">定时显示的内容</div>
  </transition>
</template>

<script>
export default {
  data() {
    return { active: false }
  },
  mounted() {
    setTimeout(() => {
      this.active = true // 3秒后触发动画
    }, 3000)
  }
}
</script>

使用 Vue 的 transition-group

列表自动渲染时应用动画效果。

<template>
  <transition-group name="list" tag="ul">
    <li v-for="item in items" :key="item.id">{{ item.text }}</li>
  </transition-group>
</template>

<script>
export default {
  data() {
    return {
      items: [] // 初始为空
    }
  },
  created() {
    // 模拟异步数据加载
    setTimeout(() => {
      this.items = [
        { id: 1, text: '项目1' },
        { id: 2, text: '项目2' }
      ]
    }, 1000)
  }
}
</script>

注意事项

  • 使用 CSS 动画时注意性能优化,优先使用 transformopacity 属性
  • 复杂动画可考虑使用 GreenSock (GSAP) 等专业动画库
  • 移动端注意硬件加速优化,可添加 will-change 属性
  • 组件销毁前清除定时器或观察器避免内存泄漏

vue不点击实现动画

标签: 动画vue
分享给朋友:

相关文章

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…