当前位置:首页 > VUE

vue实现呼吸灯

2026-01-17 10:42:45VUE

实现呼吸灯效果

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

使用CSS动画实现

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

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定时器实现更灵活的控制。

vue实现呼吸灯

<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 中实现表格功能可以通过多种方式完成,以下介绍几种常见的方法: 使用原生 HTML 表格 最简单的表格可以通过 Vue 模板中的原生 HTML 表格元素实现: &l…

vue 实现全选

vue 实现全选

Vue 实现全选功能 在 Vue 中实现全选功能通常需要结合复选框的状态管理,以下是几种常见的实现方式: 使用 v-model 绑定数组 通过 v-model 绑定一个数组来管理选中的项,全…

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &l…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-i…

vue实现飘窗原理

vue实现飘窗原理

vue实现飘窗原理 飘窗效果通常指在页面上悬浮显示的弹窗或通知,跟随页面滚动或固定在某个位置。Vue实现这一效果主要依赖组件化开发、CSS定位和动态数据绑定。 组件结构设计 创建一个独立的Vue组件…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…