当前位置:首页 > VUE

vue实现呼吸灯

2026-01-17 10:42:45VUE

实现呼吸灯效果

在Vue中实现呼吸灯效果可以通过CSS动画和Vue的动态绑定结合完成。呼吸灯通常表现为元素亮度或透明度周期性变化。

使用CSS动画实现

创建一个周期性改变透明度的CSS动画,通过Vue绑定类名或样式。

<template>
  <div class="breathing-light"></div>
</template>

<style>
.breathing-light {
  width: 100px;
  height: 100px;
  background-color: #42b983;
  border-radius: 50%;
  animation: breathe 3s infinite ease-in-out;
}

@keyframes breathe {
  0%, 100% {
    opacity: 0.5;
    transform: scale(0.9);
  }
  50% {
    opacity: 1;
    transform: scale(1.1);
  }
}
</style>

使用Vue动态控制

通过Vue的响应式数据控制样式属性,结合JavaScript定时器实现更灵活的控制。

<template>
  <div 
    class="breathing-light" 
    :style="{
      opacity: currentOpacity,
      transform: `scale(${currentScale})`
    }"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      currentOpacity: 0.5,
      currentScale: 0.9,
      animationInterval: null
    }
  },
  mounted() {
    this.startAnimation()
  },
  beforeDestroy() {
    clearInterval(this.animationInterval)
  },
  methods: {
    startAnimation() {
      let direction = 1
      const speed = 0.02

      this.animationInterval = setInterval(() => {
        this.currentOpacity += speed * direction
        this.currentScale += speed * direction * 0.2

        if(this.currentOpacity >= 1 || this.currentOpacity <= 0.5) {
          direction *= -1
        }
      }, 50)
    }
  }
}
</script>

<style>
.breathing-light {
  width: 100px;
  height: 100px;
  background-color: #42b983;
  border-radius: 50%;
  transition: opacity 0.1s ease, transform 0.1s ease;
}
</style>

使用第三方动画库

考虑使用动画库如GSAP或Animate.css实现更复杂的呼吸效果。

<template>
  <div ref="lightElement" class="breathing-light"></div>
</template>

<script>
import gsap from 'gsap'

export default {
  mounted() {
    gsap.to(this.$refs.lightElement, {
      opacity: 0.5,
      scale: 0.9,
      duration: 1.5,
      repeat: -1,
      yoyo: true,
      ease: "sine.inOut"
    })
  }
}
</script>

<style>
.breathing-light {
  width: 100px;
  height: 100px;
  background-color: #42b983;
  border-radius: 50%;
}
</style>

实现带颜色的呼吸灯

扩展基本呼吸灯效果,加入颜色变化。

<template>
  <div class="color-breathing-light"></div>
</template>

<style>
.color-breathing-light {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  animation: colorBreathe 4s infinite;
}

@keyframes colorBreathe {
  0% {
    background-color: #42b983;
    opacity: 0.5;
    transform: scale(0.9);
  }
  50% {
    background-color: #ff7e67;
    opacity: 1;
    transform: scale(1.1);
  }
  100% {
    background-color: #42b983;
    opacity: 0.5;
    transform: scale(0.9);
  }
}
</style>

vue实现呼吸灯

标签: 呼吸vue
分享给朋友:

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提交…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组件中引…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="clock-…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-mode…