当前位置:首页 > VUE

vue实现点亮灯光

2026-03-28 14:54:58VUE

Vue 实现点亮灯光效果

在 Vue 中实现点亮灯光效果可以通过多种方式完成,以下是几种常见的方法:

使用 CSS 动画和 Vue 数据绑定

通过 Vue 的数据绑定控制 CSS 类名,实现灯光的点亮和熄灭效果。

<template>
  <div>
    <div 
      class="light" 
      :class="{ 'light-on': isLightOn }"
      @click="toggleLight"
    ></div>
  </div>
</template>

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

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

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

使用 SVG 和动态样式

通过 SVG 实现更复杂的灯光效果,利用 Vue 控制 SVG 元素的属性。

<template>
  <div>
    <svg width="200" height="200">
      <circle 
        cx="100" 
        cy="100" 
        r="50" 
        :fill="isLightOn ? 'yellow' : 'gray'"
        :filter="isLightOn ? 'url(#glow)' : 'none'"
        @click="toggleLight"
      />
      <defs>
        <filter id="glow" x="-30%" y="-30%" width="160%" height="160%">
          <feGaussianBlur stdDeviation="5" result="blur" />
          <feComposite in="SourceGraphic" in2="blur" operator="over" />
        </filter>
      </defs>
    </svg>
  </div>
</template>

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

使用第三方动画库

结合动画库如 GSAP 或 Anime.js 实现更丰富的灯光动画效果。

<template>
  <div>
    <div ref="lightBulb" class="light-bulb" @click="toggleLight"></div>
  </div>
</template>

<script>
import { gsap } from 'gsap'

export default {
  data() {
    return {
      isLightOn: false
    }
  },
  methods: {
    toggleLight() {
      this.isLightOn = !this.isLightOn
      if (this.isLightOn) {
        gsap.to(this.$refs.lightBulb, {
          backgroundColor: 'yellow',
          boxShadow: '0 0 20px yellow',
          duration: 0.5
        })
      } else {
        gsap.to(this.$refs.lightBulb, {
          backgroundColor: '#ccc',
          boxShadow: 'none',
          duration: 0.5
        })
      }
    }
  }
}
</script>

<style>
.light-bulb {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background-color: #ccc;
  cursor: pointer;
}
</style>

结合 Canvas 实现动态灯光

使用 Canvas 绘制更复杂的灯光效果,通过 Vue 控制绘制逻辑。

vue实现点亮灯光

<template>
  <div>
    <canvas ref="lightCanvas" width="200" height="200" @click="toggleLight"></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isLightOn: false,
      ctx: null
    }
  },
  mounted() {
    this.ctx = this.$refs.lightCanvas.getContext('2d')
    this.drawLight()
  },
  methods: {
    toggleLight() {
      this.isLightOn = !this.isLightOn
      this.drawLight()
    },
    drawLight() {
      this.ctx.clearRect(0, 0, 200, 200)

      if (this.isLightOn) {
        // 绘制发光效果
        const gradient = this.ctx.createRadialGradient(100, 100, 0, 100, 100, 50)
        gradient.addColorStop(0, 'yellow')
        gradient.addColorStop(1, 'transparent')

        this.ctx.beginPath()
        this.ctx.arc(100, 100, 50, 0, Math.PI * 2)
        this.ctx.fillStyle = gradient
        this.ctx.fill()

        this.ctx.beginPath()
        this.ctx.arc(100, 100, 30, 0, Math.PI * 2)
        this.ctx.fillStyle = 'yellow'
        this.ctx.fill()
      } else {
        // 绘制关闭状态
        this.ctx.beginPath()
        this.ctx.arc(100, 100, 30, 0, Math.PI * 2)
        this.ctx.fillStyle = '#ccc'
        this.ctx.fill()
      }
    }
  }
}
</script>

以上方法可以根据项目需求选择适合的实现方式,从简单的 CSS 动画到复杂的 Canvas 绘制都能实现点亮灯光的效果。

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

相关文章

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class=…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…

vue实现拖动

vue实现拖动

Vue实现拖动的常用方法 在Vue中实现拖动功能通常需要结合HTML5的拖放API或第三方库。以下是几种常见实现方式: 使用HTML5原生拖放API HTML5提供了原生拖放API,可以通过drag…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…