当前位置:首页 > VUE

vue实现呼吸灯

2026-02-18 03:19:28VUE

Vue 实现呼吸灯效果

呼吸灯效果可以通过 CSS 动画结合 Vue 的动态样式绑定实现。以下是两种常见方法:

vue实现呼吸灯

使用 CSS 动画和 Vue 数据绑定

定义 CSS 关键帧动画:

vue实现呼吸灯

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

.breathe-effect {
  animation: breathe 3s infinite;
}

在 Vue 组件中应用:

<template>
  <div class="breathe-effect" :style="{ 'animation-duration': duration + 's' }">
    <!-- 你的内容 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      duration: 3 // 可动态调整呼吸周期
    }
  }
}
</script>

使用 Vue 过渡和动态样式

通过计算属性动态控制透明度:

<template>
  <div 
    class="breathing-element"
    :style="{ opacity: currentOpacity }"
  ></div>
</template>

<script>
export default {
  data() {
    return {
      minOpacity: 0.4,
      maxOpacity: 1,
      speed: 0.01,
      currentOpacity: 0.4,
      increasing: true
    }
  },
  mounted() {
    this.animateBreath()
  },
  methods: {
    animateBreath() {
      if (this.increasing) {
        this.currentOpacity += this.speed
        if (this.currentOpacity >= this.maxOpacity) {
          this.increasing = false
        }
      } else {
        this.currentOpacity -= this.speed
        if (this.currentOpacity <= this.minOpacity) {
          this.increasing = true
        }
      }
      requestAnimationFrame(this.animateBreath)
    }
  }
}
</script>

<style>
.breathing-element {
  width: 100px;
  height: 100px;
  background-color: #42b983;
  transition: opacity 0.1s linear;
}
</style>

注意事项

  • 性能优化:CSS 动画通常比 JavaScript 实现的动画性能更好
  • 移动端适配:可以添加 -webkit- 前缀确保兼容性
  • 自定义参数:通过 props 接收呼吸周期、颜色等参数使组件更灵活

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

相关文章

vue实现聊天

vue实现聊天

Vue实现聊天功能 基础项目结构搭建 使用Vue CLI或Vite创建项目,安装必要依赖如socket.io-client或axios(根据通信方式选择)。核心组件通常包括: ChatWindow.…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-li…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…