当前位置:首页 > 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 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue点击实现排序

vue点击实现排序

实现点击排序的方法 在Vue中实现点击排序功能,可以通过以下几种方式完成: 使用计算属性结合排序方法 定义一个响应式数据存储排序状态,通过计算属性动态返回排序后的数组。点击事件切换排序状态。…

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框…

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-i…