当前位置:首页 > VUE

vue实现呼吸灯

2026-03-09 16:05:17VUE

实现呼吸灯效果的方法

呼吸灯效果可以通过CSS动画或JavaScript动态控制实现。以下是两种常见的实现方式:

使用CSS动画实现

通过CSS的@keyframes定义透明度或亮度的周期性变化,结合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;
}

@keyframes breathe {
  0% { opacity: 0.3; }
  50% { opacity: 1; }
  100% { opacity: 0.3; }
}
</style>

使用JavaScript动态控制

通过Vue的响应式数据结合requestAnimationFramesetInterval动态修改样式属性。

vue实现呼吸灯

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

<script>
export default {
  data() {
    return {
      currentOpacity: 0.3,
      direction: 1,
      speed: 0.01
    };
  },
  mounted() {
    this.animate();
  },
  methods: {
    animate() {
      this.currentOpacity += this.speed * this.direction;
      if (this.currentOpacity >= 1 || this.currentOpacity <= 0.3) {
        this.direction *= -1;
      }
      requestAnimationFrame(this.animate);
    }
  }
};
</script>

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

结合Vue过渡效果

使用Vue的<transition>组件和CSS过渡属性实现平滑的呼吸效果。

<template>
  <transition name="breath">
    <div v-if="show" class="breathing-light"></div>
  </transition>
</template>

<script>
export default {
  data() {
    return {
      show: true
    };
  },
  mounted() {
    setInterval(() => {
      this.show = !this.show;
    }, 1500);
  }
};
</script>

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

.breath-enter-active, .breath-leave-active {
  transition: opacity 1.5s;
}
.breath-enter, .breath-leave-to {
  opacity: 0.3;
}
</style>

使用第三方库(如GSAP)

通过GSAP等动画库实现更复杂的呼吸效果。

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

<script>
import { gsap } from 'gsap';

export default {
  mounted() {
    gsap.to(this.$refs.light, {
      opacity: 0.3,
      duration: 1.5,
      repeat: -1,
      yoyo: true
    });
  }
};
</script>

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

注意事项

  • 性能优化:CSS动画通常性能更好,优先考虑纯CSS方案。
  • 兼容性:检查requestAnimationFrame和CSS动画的浏览器支持情况。
  • 参数调整:呼吸速度(animation-durationspeed)和透明度范围可根据需求调整。

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

相关文章

vue项目实现

vue项目实现

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 安装 Vue CLI(脚手架工具): np…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vue.…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue实现一个tab栏

vue实现一个tab栏

Vue 实现 Tab 栏 基础实现(选项式 API) <template> <div class="tab-container"> <div class="t…

vue实现帖子

vue实现帖子

Vue 实现帖子功能 使用 Vue 实现帖子功能需要结合前端框架和后端数据交互,以下是实现的关键步骤和代码示例。 帖子列表展示 创建帖子列表组件,使用 v-for 渲染帖子数据,通过 axios 获…

vue实现active

vue实现active

Vue 实现 active 状态的方法 在 Vue 中实现 active 状态可以通过以下几种方式实现,具体取决于使用场景和需求。 使用 v-bind:class 动态绑定类名 通过 v-bind:…