当前位置:首页 > 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 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue实现评价

vue实现评价

Vue实现评价功能 数据绑定与双向绑定 Vue的核心特性是数据绑定和双向绑定,通过v-model指令可以轻松实现表单输入与应用状态之间的双向绑定。在评价功能中,可以使用v-model绑定评论文本和评分…

vue实现登录检验

vue实现登录检验

实现登录状态检验的方法 在Vue中实现登录状态检验通常涉及前端路由守卫、Token验证以及与后端的交互。以下是几种常见的方法: 使用路由守卫进行登录验证 通过Vue Router的beforeEac…

vue实现导航栏

vue实现导航栏

使用 Vue 实现导航栏 基础导航栏结构 在 Vue 中实现导航栏通常使用 <router-link> 或自定义组件。以下是一个基础示例: <template> <…

vue实现的聊天

vue实现的聊天

Vue 实现的聊天应用 使用 Vue 实现聊天应用可以通过 Vue 的响应式特性和组件化开发来构建高效、可维护的前端界面。以下是一个基于 Vue 3 的聊天应用实现方案。 项目初始化 创建 Vue…