当前位置:首页 > VUE

vue实现点亮灯光

2026-01-15 01:41:04VUE

Vue 实现点亮灯光效果

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

动态类名绑定

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

vue实现点亮灯光

<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 动画

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

vue实现点亮灯光

.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 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.defineProperty…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现树形

vue实现树形

Vue 实现树形组件 在 Vue 中实现树形结构通常需要递归组件和动态数据绑定。以下是实现树形组件的几种常见方法。 递归组件实现 递归组件是 Vue 中实现树形结构的核心方法。通过组件调用自身,可以…

vue实现uuid

vue实现uuid

使用 uuid 库生成 UUID 在 Vue 项目中,可以使用 uuid 库来生成 UUID。安装依赖: npm install uuid 在 Vue 组件中使用: import { v4 as…

vue实现eventbus

vue实现eventbus

Vue 中实现 EventBus 在 Vue 中,EventBus 是一种跨组件通信的机制,尤其适用于非父子组件之间的数据传递。以下是实现 EventBus 的几种方法: 方法一:使用 Vue 实例…