当前位置:首页 > 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>

实现带颜色的呼吸灯

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

vue实现呼吸灯

<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中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起: &l…

vue使用vr实现标注

vue使用vr实现标注

Vue 中使用 VR 实现标注的方法 在 Vue 项目中结合 VR 技术实现标注功能,可以通过 WebXR 和 Three.js 等库实现。以下为具体实现方法: 安装依赖 确保项目中已安装 Thre…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CSS…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…