vue实现宫格轮转抽奖

 更新时间:2021年11月14日 08:41:59   作者:Cy.Lau  
这篇文章主要为大家详细介绍了vue实现宫格轮转抽奖,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

vue实现宫格轮转抽奖(类似穿越火线的xx轮回),供大家参考,具体内容如下

不做过多的解说,直接上代码啦。关键的代码都写了注释,很容易理解。直接复制即可使用!
另外css部分依赖 node-sass、sass-loader,没有安装的安装一下,已有的小伙伴直接跳过~~

"node-sass": "^4.12.0",
"sass-loader": "^8.0.2",

<template>
  <div class="home">
    <div class="home-container">
      <div class="home-container-line">
        <div
          class="home-container-line-box"
          v-for="item in list.slice(0, 5)"
          :key="item.index"
          :class="{ selected: item.active }"
        >
          {{ item.label }}
        </div>
      </div>
      <div class="home-container-line">
        <div
          class="home-container-line-box"
          v-for="item in list.slice(11, 12)"
          :key="item.index"
          :class="{ selected: item.active }"
        >
          {{ item.label }}
        </div>
        <div class="home-container-line-btn" @click="handleClick" :disable="isAnimate"></div>
        <div
          class="home-container-line-box"
          v-for="item in list.slice(5, 6)"
          :key="item.index"
          :class="{ selected: item.active }"
        >
          {{ item.label }}
        </div>
      </div>
      <div class="home-container-line">
        <div
          class="home-container-line-box"
          v-for="item in Array.prototype.reverse.call(list.slice(6, 11))"
          :key="item.index"
          :class="{ selected: item.active }"
        >
          {{ item.label }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Luck",
  data() {
    return {
      list: [
        { label: "未中奖", val: 1, img: "", index: 0, active: false },
        { label: "大保健", val: 2, img: "", index: 1, active: false },
        { label: "iPhone13", val: 3, img: "", index: 2, active: false },
        { label: "MacBook Pro", val: 4, img: "", index: 3, active: false },
        { label: "MacBook Air", val: 5, img: "", index: 4, active: false },
        { label: "1积分", val: 6, img: "", index: 5, active: false },
        { label: "5积分", val: 7, img: "", index: 6, active: false },
        { label: "10积分", val: 8, img: "", index: 7, active: false },
        { label: "小爱同学", val: 9, img: "", index: 8, active: false },
        { label: "安慕希酸奶", val: 10, img: "", index: 9, active: false },
        { label: "清空购物车", val: 11, img: "", index: 10, active: false },
        { label: "50元话费", val: 12, img: "", index: 11, active: false },
      ],
      isAnimate: false, // 为 true时代表正在抽奖,当前抽奖未结束时点击抽奖按钮无效
      jumpIndex:  Math.floor(Math.random() * 12), // 抽奖轮跳时的索引
      jumpingTime: 0, // 正在轮跳的时间
      jumpingTotalTime: 0,  // 轮跳的时间总量,基于 duration 变量加上一个 0~1000 之间的随机数组成
      jumpingChange: 0, // 轮跳速率峰值,基于 velocity 变量加上一个 0~3 之间的随机数组成
      duration: 4500,  // 持续时间
      velocity: 300,  // 速率
    };
  },
  methods: {
    handleClick() {
      if(this.isAnimate) return;
      this.jumpingTime = 0;
      this.jumpingTotalTime = Math.random() * 1000 + this.duration;
      this.jumpingChange = Math.random() * 3 + this.velocity;
      this.animateRound(this.jumpIndex);
    },

    animateRound(index) {
      this.isAnimate = true; 
      if(this.jumpIndex < this.list.length - 1 )  this.jumpIndex ++;
      if(this.jumpIndex >= this.list.length - 1 )  this.jumpIndex = 0;

      this.jumpingTime += 100;  // 每一帧执行 setTimeout 方法所消耗的时间

      // 如果当前时间大于时间总量后, 退出动画,  清算奖品
      if(this.jumpingTime >= this.jumpingTotalTime){
        this.isAnimate = false; 
        if(index == 0) {
          alert(`很遗憾没有抽到奖品,再接再厉哦~`);
        }
        else{
          alert(`恭喜您抽到了:${this.list[index].label}`)
        }
        return
      }

      // 轮训动画
      if (index >= this.list.length-1) {
        index = 0;
        this.list[11].active = false;
        this.list[index].active = true;
        index -= 1;
      } else {
        this.list[index].active = false;
        this.list[index + 1].active = true;
      }

      setTimeout(() => {
        this.animateRound(index + 1);
      }, this.easeOut(this.jumpingTime, 0, this.jumpingChange, this.jumpingTotalTime));
    },

    /**
     * 缓动函数,由快到慢
     * @param {Num} t 当前时间
     * @param {Num} b 初始值
     * @param {Num} c 变化值
     * @param {Num} d 持续时间
     */
    easeOut(t, b, c, d) {
      if ((t /= d / 2) < 1) return (c / 2) * t * t + b;
      return (-c / 2) * (--t * (t - 2) - 1) + b;
    },
  },
};
</script>
<style lang="scss" scoped>
.center {
  display: flex;
  justify-content: center;
  align-items: center;
}
.home {
  padding: 0;
  margin: 0;
  width: 100%;
  height: calc(100vh - 16px);
  background-image: linear-gradient(25deg, #30007c, #464995, #4d83ad, #41bfc4);
  @extend .center;
  &-container {
    width: 1000px;
    height: 600px;
    &-line {
      width: 100%;
      height: 198px;
      display: flex;
      &-box {
        flex: 1;
        overflow: hidden;
        margin: 5px 3px 5px 3px;
        @extend .center;
        background: #fff;
        transition: all .3s;
      }
      &-btn {
        position: relative;
        flex: 3;
        overflow: hidden;
        margin: 5px 3px 3px 3px;
        @extend .center;
        box-shadow: 0 1px 10px 0px #cf5531;
        background-image: linear-gradient(
          25deg,
          #cf5531,
          #d0853a,
          #cdaf43,
          #c4d84d
        );
        cursor: pointer;
        &:active {
          background-image: linear-gradient(
            25deg,
            #3f3e41,
            #6d6340,
            #9a8b39,
            #c9b629
          );
        }
        &::before {
          position: absolute;
          content: "点击抽奖";
          font-size: 2.5rem;
          color: #fff;
          font-weight: bold;
        }
      }
    }
  }
}
.selected {
  background: rgba($color: #f6e58d, $alpha: 0.5);
  animation-name: twinkle;
  animation-duration: 3s;
  animation-iteration-count: infinite;
}
@keyframes twinkle {
  0%   {background:#ffbe76;}
 100% {background:#f6e58d;}
}
</style>

效果图:

最后特别说明一下,概率完全是随机的。目前还没有特别好的思路去调整中奖的概率,如果有比较好的想法的小伙伴们也非常欢迎一起来探讨一下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 关于Element table组件滚动条不显示的踩坑记录

    关于Element table组件滚动条不显示的踩坑记录

    这篇文章主要介绍了关于Element table组件滚动条不显示的踩坑记录,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • vue生命周期beforeDestroy和destroyed调用方式

    vue生命周期beforeDestroy和destroyed调用方式

    这篇文章主要介绍了vue生命周期beforeDestroy和destroyed调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Vue项目中是否使用store原理深究

    Vue项目中是否使用store原理深究

    这篇文章主要为大家介绍了在Vue项目中是否使用store原理深究,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • vue-cli 引入jQuery,Bootstrap,popper的方法

    vue-cli 引入jQuery,Bootstrap,popper的方法

    这篇文章主要介绍了vue-cli 引入jQuery,Bootstrap,popper的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • vue3 Class 与 Style 绑定操作方法

    vue3 Class 与 Style 绑定操作方法

    数据绑定的一个常见需求场景是操纵元素的 CSS class 列表和内联样式,因为 class 和 style 都是 attribute,我们可以和其他 attribute 一样使用 v-bind 将它们和动态的字符串绑定,这篇文章主要介绍了vue3 Class 与 Style 绑定操作方法,需要的朋友可以参考下
    2024-05-05
  • vue 解决addRoutes动态添加路由后刷新失效问题

    vue 解决addRoutes动态添加路由后刷新失效问题

    这篇文章主要介绍了vue 解决addRoutes动态添加路由后刷新失效问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • vue简单的二维数组循环嵌套方式

    vue简单的二维数组循环嵌套方式

    这篇文章主要介绍了vue简单的二维数组循环嵌套方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • vue使用高德地图实现轨迹显隐效果

    vue使用高德地图实现轨迹显隐效果

    本文主要介绍了在vue中如何使用高德地图实现轨迹显隐的功能,包括了相关代码的编写和具体实现步骤,对于想要在自己的项目中使用这一功能的开发者有一定的参考价值,希望大家对此有所帮助,同时也欢迎大家多多支持脚本之家
    2024-10-10
  • vue中集成省市区街四级地址组件的实现过程

    vue中集成省市区街四级地址组件的实现过程

    我们在开发中常会遇到选择地址的需求,有时候只需要选择省就可以,有时候则需要选择到市、县,以至于乡镇,甚至哪个村都有可能,下面这篇文章主要给大家介绍了关于vue中集成省市区街四级地址组件的相关资料,需要的朋友可以参考下
    2022-12-12
  • 搭建vue3项目以及按需引入element-ui框架组件全过程

    搭建vue3项目以及按需引入element-ui框架组件全过程

    element是基于vue.js框架开发的快速搭建前端的UI框架,下面这篇文章主要给大家介绍了关于搭建vue3项目以及按需引入element-ui框架组件的相关资料,文中通过图文以及代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02

最新评论