当前位置:首页 > 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();
  }
}

添加循环滚动效果

vue实现抽奖滚动

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 CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue广告实现

vue广告实现

Vue 广告实现方法 在 Vue 项目中实现广告功能可以通过多种方式完成,具体取决于广告类型和需求。以下是几种常见的实现方法: 使用第三方广告平台 许多广告平台如 Google AdSense、百…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…