当前位置:首页 > VUE

vue实现抽奖滚动

2026-01-19 05:57:26VUE

实现抽奖滚动效果

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

基础实现方案

HTML结构

vue实现抽奖滚动

<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样式

vue实现抽奖滚动

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

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue实现文字

vue实现文字

Vue 中实现文字显示的方法 在 Vue 中实现文字显示可以通过多种方式,包括插值表达式、指令、组件等。以下是几种常见的实现方法: 插值表达式 使用双大括号 {{ }} 进行文本插值,这是 Vue…

vue实现骰子

vue实现骰子

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

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装…