How to use the WeChat Mini Program lottery component

How to use the WeChat Mini Program lottery component

It is provided in the form of WeChat components. Because async/await is used inside the component, please check "Enhance compilation and use npm" in the WeChat developer tool "Details = > Local settings". For the use of npm in the mini program, please refer to: WeChat Mini Program - npm support

First look at the effect diagram:

Steps:

Step 1:

Install dependencies or go to githua to download the source code and copy the lottery-turntable directory under the dist directory

npm i lottery-turntable-for-wx-miniprogram

Step 2:

Page JSON configuration

{
 "usingComponents": {
  "lottery-turntable":"lottery-turntable-for-wx-miniprogram/lottery_turntable/index"
 }
}

Step 3:

Prepare data and add event handling (using component page JS)

const datas = [{
 "id": "792085712309854208",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 1"
}, {
 "id": "766410261029724160",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 2"
}, {
 "id": "770719340921364480",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 3"
}, {
 "id": "770946438416048128",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 4"
}, {
 "id": "781950121802735616",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 5"
}, {
 "id": "766411654436233216",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 6"
}, {
 "id": "770716883860332544",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 7"
}, {
 "id": "796879308510732288",
 "imgUrl": "../../images/icon.png",
 "title": "Thunder Platinum Member Monthly Card - 8"
}];
 
Page({
 data: {
  datas: datas, // data prizeId: '', // The winning result id is passed to the component through attributes config: { // Turntable configuration is passed to the component through attributes titleLength: 7
  }
 },
 /**
  * Insufficient times callback* @param e
  */
 onNotEnoughHandle(e) {
  wx.showToast({
   icon: 'none',
   title: e.detail
  })
 },
 
 /**
  * Lottery callback*/
 onLuckDrawHandle() {
  this.setData({
   prizeId: this.data.datas[Math.floor(Math.random() * 10 % this.data.datas.length)].id
  });
 },
 
 /**
  * Animation rotation completion callback */
 onLuckDrawFinishHandle() {
  const datas = this.data.datas;
  const data = datas.find((item) => {
   return item.id === this.data.prizeId;
  });
  wx.showToast({
   icon: 'none',
   title: `Congratulations on winning ${data.title}`
  })
  this.setData({
   prizeId: ''
  });
 }
})
 

Step 4:

Page Usage

<lottery-turntable
   data="{{datas}}"
   prize-id="{{prizeId}}"
   count="{{5}}"
   config="{{config}}"
   bindLuckDraw="onLuckDrawHandle"
   bindNotEnough="onNotEnoughHandle"
   bindLuckDrawFinish="onLuckDrawFinishHandle"
  ></lottery-turntable>

Step 5:

Change the component configuration item (the following is the default configuration) and pass in a js object through the config attribute

/**
 * ease: The values ​​are as follows* 'linear' The speed of the animation is the same from beginning to end* 'ease' The animation starts at a slow speed, then speeds up, and slows down before the end* 'ease-in' The animation starts at a slow speed* 'ease-in-out' The animation starts and ends at a slow speed* 'ease-out' The animation ends at a slow speed* 'step-start' The animation jumps to the end state in the first frame and continues to end* 'step-end' The animation remains in the start state and jumps to the end state in the last frame*/
// The following is the default configuration let config = {
 size: {
  width: '572rpx',
  height: '572rpx'
 }, // Turntable width and heightbgColors: ['#FFC53F', '#FFED97'], // The background color of the turntable interval supports multiple color alternationfontSize: 10, // Text sizefontColor: '#C31A34', // Text colortitleMarginTop: 12, // Outermost text margintitleLength: 6 // Outermost text numbericonWidth: 29.5, // Icon widthiconHeight: 29.5, // Icon heighticonAndTextPadding: 4, // The margin between the innermost text and iconduration: 8000, // Turntable rotation animation durationrate: 1.5, // Obtained by duration s / number of circlesborder: 'border: 10rpx solid #FEFAE4;', // Turntable borderease: 'ease-out' // Turntable animation};

Summarize

This is the end of this article about the WeChat Mini Program lottery component. For more relevant WeChat Mini Program lottery component content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet implements flip card lottery animation
  • WeChat applet implements lucky draw
  • WeChat applet Gacha Lottery Machine CSS3 animation implementation detailed explanation
  • WeChat applet implements nine-square lottery
  • WeChat applet implements multi-grid lucky draw activities
  • WeChat applet shake lottery simple example implementation code
  • WeChat applet development: a large turntable imitating Tmall supermarket lottery example
  • WeChat applet implements revolving lantern-style lottery

<<:  Detailed explanation of data type issues in JS array index detection

>>:  Detailed explanation of uniapp's global variable implementation

Recommend

Install Centos7 using Hyper-v virtual machine

Table of contents introduce Prepare Download syst...

An audio-visual Linux distribution that appeals to audiophiles

I recently stumbled upon the Audiovisual Linux Pr...

Installing the ping tool in a container built by Docker

Because the Base images pulled by Docker, such as...

The connection between JavaScript and TypeScript

Table of contents 1. What is JavaScript? 2. What ...

Text pop-up effects implemented with CSS3

Achieve resultsImplementation Code html <div&g...

A brief discussion on two methods to solve space-evenly compatibility issues

Since its launch in 2009, flex has been supported...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

JS realizes picture digital clock

This article example shares the specific code of ...

XHTML Getting Started Tutorial: Using the Frame Tag

<br />The frame structure allows several web...

Solve the problem of inconsistent MySQL storage time

After obtaining the system time using Java and st...

How to implement image mapping with CSS

1. Introduction Image maps allow you to designate...

MySQL 5.7 installation and configuration tutorial

This article shares the MySQL installation and co...