当前位置:首页 > VUE

vue 实现闪烁

2026-01-12 18:19:38VUE

Vue 实现元素闪烁效果

方法一:使用 CSS 动画 通过 Vue 绑定 class 或 style 来触发 CSS 动画实现闪烁效果。

<template>
  <div :class="{ 'blink': isBlinking }">闪烁内容</div>
  <button @click="toggleBlink">切换闪烁</button>
</template>

<script>
export default {
  data() {
    return {
      isBlinking: false
    }
  },
  methods: {
    toggleBlink() {
      this.isBlinking = !this.isBlinking
    }
  }
}
</script>

<style>
.blink {
  animation: blink 1s infinite;
}

@keyframes blink {
  0% { opacity: 1; }
  50% { opacity: 0; }
  100% { opacity: 1; }
}
</style>

方法二:使用 setInterval 通过 JavaScript 定时器动态改变样式实现更灵活的控制。

<template>
  <div :style="{ opacity: currentOpacity }">动态闪烁内容</div>
  <button @click="startBlink">开始闪烁</button>
  <button @click="stopBlink">停止闪烁</button>
</template>

<script>
export default {
  data() {
    return {
      currentOpacity: 1,
      blinkInterval: null
    }
  },
  methods: {
    startBlink() {
      this.blinkInterval = setInterval(() => {
        this.currentOpacity = this.currentOpacity === 1 ? 0 : 1
      }, 500)
    },
    stopBlink() {
      clearInterval(this.blinkInterval)
      this.currentOpacity = 1
    }
  },
  beforeDestroy() {
    clearInterval(this.blinkInterval)
  }
}
</script>

方法三:使用 Vue 过渡 结合 Vue 的 transition 组件实现平滑的闪烁效果。

<template>
  <transition name="blink" mode="out-in">
    <div v-if="show" key="content">过渡闪烁内容</div>
  </transition>
  <button @click="toggleShow">切换显示</button>
</template>

<script>
export default {
  data() {
    return {
      show: true,
      toggleTimer: null
    }
  },
  methods: {
    toggleShow() {
      this.toggleTimer = setInterval(() => {
        this.show = !this.show
      }, 1000)
    }
  },
  beforeDestroy() {
    clearInterval(this.toggleTimer)
  }
}
</script>

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

注意事项

vue 实现闪烁

  • 使用 CSS 动画性能更好,适合简单的闪烁效果
  • JavaScript 定时器可以实现更复杂的逻辑控制
  • 记得在组件销毁时清除定时器防止内存泄漏
  • 可以根据需求调整闪烁频率和过渡效果

标签: vue
分享给朋友:

相关文章

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue 实现平面

vue 实现平面

Vue 实现平面的方法 在 Vue 中实现平面效果通常涉及 CSS 样式、UI 框架或自定义组件的使用。以下是几种常见方法: 使用 CSS 样式 通过 Vue 的样式绑定或 scoped CSS 为…

vue 实现loading

vue 实现loading

Vue 实现 Loading 的方法 使用 v-if 和 v-show 控制显示 在 Vue 中可以通过 v-if 或 v-show 控制 loading 组件的显示与隐藏。v-if 会动态创建或销毁…

vue实现tabbar

vue实现tabbar

Vue 实现 TabBar 的方法 使用 Vue Router 实现基础 TabBar 安装 Vue Router 依赖: npm install vue-router 创建路由配置文件(route…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…

vue实现tabs

vue实现tabs

Vue实现Tabs组件的方法 使用动态组件和v-for指令 在Vue中创建Tabs组件可以通过动态组件和v-for指令实现。定义一个包含tab标题和内容的数组,使用v-for渲染tab标题,并通过点击…