Vue realizes the palace grid rotation lottery

Vue realizes the palace grid rotation lottery

Vue implements the palace grid rotation lottery (similar to the xx reincarnation of CrossFire), for your reference, the specific content is as follows

Without further explanation, let's get straight to the code. The key codes are commented and easy to understand. Just copy and use!
In addition, the css part depends on node-sass and sass-loader. If you haven't installed them, install them. If you already have them, just skip it~~

"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: "Not winning", val: 1, img: "", index: 0, active: false },
        { label: "Health Care", 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 point", val: 6, img: "", index: 5, active: false },
        { label: "5 points", val: 7, img: "", index: 6, active: false },
        { label: "10 points", val: 8, img: "", index: 7, active: false },
        { label: "Xiao Ai", val: 9, img: "", index: 8, active: false },
        { label: "Anmuxi Yogurt", val: 10, img: "", index: 9, active: false },
        { label: "Clear shopping cart", val: 11, img: "", index: 10, active: false },
        { label: "50 yuan phone bill", val: 12, img: "", index: 11, active: false },
      ],
      isAnimate: false, // true means the lottery is in progress. Clicking the lottery button before the current lottery ends is invalid jumpIndex: Math.floor(Math.random() * 12), // Index of the lottery jump jumpingTime: 0, // Time of the jump jumpingTotalTime: 0, // Total jump time, based on the duration variable plus a random number between 0 and 1000 jumpingChange: 0, // Peak jump rate, based on the velocity variable plus a random number between 0 and 3 duration: 4500, // Duration velocity: 300, // Velocity};
  },
  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; // The time consumed by executing the setTimeout method in each frame // If the current time is greater than the total time, exit the animation and clear the prize if (this.jumpingTime >= this.jumpingTotalTime) {
        this.isAnimate = false; 
        if(index == 0) {
          alert(`It's a pity that I didn't win the prize, keep up the good work~`);
        }
        else{
          alert(`Congratulations, you have drawn: ${this.list[index].label}`)
        }
        return
      }

      // Rotation animation 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));
    },

    /**
     * Easing function, from fast to slow* @param {Num} t current time* @param {Num} b initial value* @param {Num} c change value* @param {Num} d duration*/
    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: "Click to win the prize";
          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>

Effect picture:

Finally, I would like to point out that the probability is completely random. There is no particularly good idea to adjust the probability of winning at present. If you have better ideas, you are welcome to discuss it together.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue3 realizes lottery template setting
  • Vue implements the big turntable lottery function
  • Vue component to achieve digital scrolling lottery effect
  • VUE implements big turntable lottery
  • Vue simple implementation of turntable lottery
  • Vue component to realize mobile terminal nine-square grid turntable lottery
  • Summary and implementation ideas of Vue.js big turntable lottery
  • Jiugongge lottery function based on VUE
  • Vue implements the up and down scrolling animation example of mobile phone number lottery
  • Vue realizes nine-square lottery

<<:  The difference between method=post/get in Form

>>:  What are your principles for designing indexes? How to avoid index failure?

Recommend

React internationalization react-i18next detailed explanation

Introduction react-i18next is a powerful internat...

KVM virtualization installation, deployment and management tutorial

Table of contents 1.kvm deployment 1.1 kvm instal...

JavaScript to achieve Taobao product image switching effect

JavaScript clothing album switching effect (simil...

Practical basic Linux sed command example code

The Linux stream editor is a useful way to run sc...

Summary of 16 XHTML1.0 and HTML Compatibility Guidelines

1. Avoid declaring the page as XML type . The pag...

Detailed explanation of map overlay in openlayers6

1. Overlay Overview Overlay means covering, as th...

How to use mysqldump for full and point-in-time backups

Mysqldump is used for logical backup in MySQL. Al...

Design Theory: Hierarchy in Design

<br />Original text: http://andymao.com/andy...

How to install MySQL and Redis in Docker

This article is based on the CentOS 7.3 system en...

Summary of common commands for building ZooKeeper3.4 middleware under centos7

1. Download and decompress 1. Introduction to Zoo...

jQuery achieves full screen scrolling effect

This article example shares the specific code of ...

Small program to implement a simple calculator

This article example shares the specific code of ...