当前位置:首页 > VUE

vue实现点亮灯光

2026-01-15 01:41:04VUE

Vue 实现点亮灯光效果

使用 Vue 实现点亮灯光效果可以通过动态绑定样式或类名,结合 CSS 动画或过渡效果完成。以下是几种常见实现方式:

动态类名绑定

通过 v-bind:class 动态切换类名,控制灯光亮灭状态:

<template>
  <div>
    <div class="light" :class="{ 'light-on': isOn }"></div>
    <button @click="toggleLight">Toggle Light</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isOn: false
    }
  },
  methods: {
    toggleLight() {
      this.isOn = !this.isOn
    }
  }
}
</script>

<style>
.light {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: #ccc;
  transition: background-color 0.3s;
}

.light-on {
  background-color: yellow;
  box-shadow: 0 0 20px yellow;
}
</style>

使用 CSS 动画

添加更生动的发光动画效果:

.light-on {
  background-color: yellow;
  animation: glow 1s infinite alternate;
}

@keyframes glow {
  from {
    box-shadow: 0 0 10px yellow;
  }
  to {
    box-shadow: 0 0 30px yellow;
  }
}

多个灯光控制

控制多个灯光可以使用数组存储状态:

<template>
  <div>
    <div 
      v-for="(light, index) in lights" 
      :key="index"
      class="light"
      :class="{ 'light-on': light.isOn }"
      @click="toggleLight(index)"
    ></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      lights: [
        { isOn: false },
        { isOn: false },
        { isOn: false }
      ]
    }
  },
  methods: {
    toggleLight(index) {
      this.lights[index].isOn = !this.lights[index].isOn
    }
  }
}
</script>

使用第三方动画库

如需更复杂效果,可引入动画库如 anime.jsGSAP

<template>
  <div>
    <div ref="light" class="light"></div>
    <button @click="animateLight">Animate Light</button>
  </div>
</template>

<script>
import anime from 'animejs'

export default {
  methods: {
    animateLight() {
      anime({
        targets: this.$refs.light,
        backgroundColor: ['#ccc', 'yellow'],
        boxShadow: ['0 0 0 rgba(255,255,0,0)', '0 0 20px yellow'],
        duration: 1000,
        easing: 'easeInOutQuad'
      })
    }
  }
}
</script>

以上方法可根据实际需求选择或组合使用,Vue 的响应式特性使得状态管理和 DOM 更新变得简单高效。

vue实现点亮灯光

标签: 灯光vue
分享给朋友:

相关文章

vue实现多级组件

vue实现多级组件

实现多级组件的基本结构 在Vue中实现多级组件通常涉及父子组件嵌套,通过props向下传递数据,通过$emit向上传递事件。多级组件的核心是递归或动态组件渲染。 <!-- 父组件 Paren…

vue前端实现注册

vue前端实现注册

使用 Vue 实现用户注册功能 环境准备 确保已安装 Vue CLI 或通过 CDN 引入 Vue。若使用 Vue CLI,需安装 axios 或其他 HTTP 库用于后端通信。 注册表单组件 创建…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-t…

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="…

vue实现视频会议

vue实现视频会议

使用 Vue 实现视频会议 技术选型 Vue.js 作为前端框架,结合 WebRTC 技术实现实时音视频通信。常用的库包括: peerjs:简化 WebRTC 的点对点连接。 socket.io:用…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…