当前位置:首页 > VUE

vue实现图标闪烁

2026-02-18 04:07:32VUE

Vue 实现图标闪烁效果

图标闪烁可以通过 CSS 动画或 JavaScript 定时器实现。以下是几种常见方法:

方法一:使用 CSS 动画

定义闪烁的 CSS 动画关键帧:

@keyframes blink {
  0% { opacity: 1; }
  50% { opacity: 0; }
  100% { opacity: 1; }
}

.blink-icon {
  animation: blink 1s infinite;
}

在 Vue 模板中应用动画类:

vue实现图标闪烁

<template>
  <div class="icon blink-icon">✨</div>
</template>

方法二:通过 v-bind 动态切换类

使用 Vue 的数据绑定动态控制样式:

<template>
  <div 
    class="icon" 
    :class="{ 'transparent': isBlinking }"
    @click="toggleBlink"
  >🔔</div>
</template>

<script>
export default {
  data() {
    return {
      isBlinking: false,
      blinkInterval: null
    }
  },
  methods: {
    toggleBlink() {
      this.isBlinking = !this.isBlinking
      if (this.isBlinking) {
        this.blinkInterval = setInterval(() => {
          this.isBlinking = !this.isBlinking
        }, 500)
      } else {
        clearInterval(this.blinkInterval)
      }
    }
  }
}
</script>

<style>
.transparent {
  opacity: 0;
  transition: opacity 0.3s ease;
}
</style>

方法三:使用第三方动画库

安装 animate.css 库:

vue实现图标闪烁

npm install animate.css

在 Vue 组件中使用:

<template>
  <div class="icon animate__animated animate__flash animate__infinite">💡</div>
</template>

<script>
import 'animate.css'
export default {
  name: 'BlinkingIcon'
}
</script>

方法四:通过 SVG 滤镜实现高级效果

对于 SVG 图标可以使用滤镜创建发光闪烁效果:

<template>
  <svg width="100" height="100">
    <defs>
      <filter id="blink-filter">
        <feGaussianBlur in="SourceGraphic" stdDeviation="2" result="blur"/>
        <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0 
                                                       0 1 0 0 0 
                                                       0 0 1 0 0 
                                                       0 0 0 10 -5" result="glow"/>
        <feComposite in="SourceGraphic" in2="glow" operator="over"/>
      </filter>
    </defs>
    <circle 
      cx="50" 
      cy="50" 
      r="40" 
      fill="gold" 
      :filter="isBlinking ? 'url(#blink-filter)' : ''"
      :style="{ transition: 'filter 0.5s ease' }"
    />
  </svg>
</template>

性能优化建议

  • 优先使用 CSS 动画而非 JavaScript 定时器
  • 对频繁动画的元素启用硬件加速:
    .blink-icon {
      will-change: opacity;
      transform: translateZ(0);
    }
  • 合理设置动画频率,避免过高频率导致性能问题

以上方法可根据实际需求选择,CSS 动画方案性能最优,JavaScript 方案控制更灵活,第三方库使用最简便。

标签: 图标vue
分享给朋友:

相关文章

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现公告

vue实现公告

Vue 实现公告功能的方法 公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式: 使用 marquee 标签实现滚动公告 <template> <…

vue实现socket

vue实现socket

Vue 中实现 WebSocket 通信 在 Vue 项目中实现 WebSocket 通信可以通过原生 WebSocket API 或第三方库(如 socket.io-client)完成。以下是两种常…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…