当前位置:首页 > VUE

vue实现抽奖滚动

2026-01-19 05:57:26VUE

实现抽奖滚动效果

使用Vue实现抽奖滚动效果可以通过动态绑定CSS样式和JavaScript定时器实现。以下是一个完整的实现方案:

基础实现方案

HTML结构

<template>
  <div class="lottery-container">
    <div class="prize-list" :style="{ transform: `translateY(${translateY}px)` }">
      <div class="prize-item" v-for="(item, index) in prizes" :key="index">
        {{ item.name }}
      </div>
    </div>
    <button @click="startLottery">开始抽奖</button>
  </div>
</template>

JavaScript逻辑

<script>
export default {
  data() {
    return {
      prizes: [
        { name: '奖品1' },
        { name: '奖品2' },
        { name: '奖品3' },
        { name: '奖品4' },
        { name: '奖品5' },
        { name: '奖品6' }
      ],
      translateY: 0,
      isRolling: false,
      targetIndex: 0,
      speed: 50,
      timer: null
    }
  },
  methods: {
    startLottery() {
      if (this.isRolling) return;

      this.isRolling = true;
      this.targetIndex = Math.floor(Math.random() * this.prizes.length);

      this.timer = setInterval(() => {
        this.translateY -= 30;

        if (this.translateY <= -this.targetIndex * 60 - 300) {
          clearInterval(this.timer);
          setTimeout(() => {
            this.isRolling = false;
          }, 1000);
        }
      }, this.speed);
    }
  }
}
</script>

CSS样式

<style>
.lottery-container {
  height: 60px;
  overflow: hidden;
  position: relative;
}

.prize-list {
  transition: transform 0.5s ease-out;
}

.prize-item {
  height: 60px;
  line-height: 60px;
  text-align: center;
  border-bottom: 1px solid #eee;
}
</style>

进阶优化方案

添加缓动效果

methods: {
  easeOut(t, b, c, d) {
    t /= d;
    return -c * t*(t-2) + b;
  },

  startLottery() {
    if (this.isRolling) return;

    const duration = 3000;
    const startTime = Date.now();
    const startPosition = this.translateY;
    const endPosition = -this.targetIndex * 60;

    const animate = () => {
      const now = Date.now();
      const progress = now - startTime;

      if (progress < duration) {
        this.translateY = this.easeOut(progress, startPosition, endPosition - startPosition, duration);
        requestAnimationFrame(animate);
      } else {
        this.translateY = endPosition;
        this.isRolling = false;
      }
    };

    this.isRolling = true;
    this.targetIndex = Math.floor(Math.random() * this.prizes.length);
    animate();
  }
}

添加循环滚动效果

methods: {
  startLottery() {
    if (this.isRolling) return;

    this.isRolling = true;
    const rounds = 3;
    const totalItems = this.prizes.length * rounds + this.targetIndex;
    let current = 0;

    this.timer = setInterval(() => {
      current++;
      this.translateY = - (current % this.prizes.length) * 60;

      if (current >= totalItems) {
        clearInterval(this.timer);
        this.isRolling = false;
      }
    }, 100);
  }
}

注意事项

  • 确保奖品列表高度与容器高度匹配
  • 动画结束后需要重置状态
  • 防止用户重复点击开始按钮
  • 考虑移动端兼容性问题
  • 可以添加音效增强用户体验

以上方案可以根据实际需求进行调整,如改变动画速度、添加更多视觉效果等。

vue实现抽奖滚动

标签: vue
分享给朋友:

相关文章

vue实现单词反转

vue实现单词反转

实现思路 在Vue中实现单词反转功能,可以通过多种方式完成。核心思路是利用JavaScript的字符串处理方法,将输入的单词或句子拆分为数组,反转数组后再拼接成字符串。 方法一:使用计算属性 通过计…

vue实现组件循环

vue实现组件循环

Vue 组件循环的实现方法 在 Vue 中实现组件循环通常使用 v-for 指令,可以循环渲染数组或对象的数据到组件上。 基本数组循环 通过 v-for 指令遍历数组,渲染多个相同结构的组件。…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 EC…