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

MySQL 8.0.12 installation and configuration method graphic tutorial (windows10)

This article records the installation graphic tut...

Solve the problem of docker log mounting

The key is that the local server does not have wr...

Extract specific file paths in folders based on Linux commands

Recently, there is a need to automatically search...

How to install and deploy zabbix 5.0 for nginx

Table of contents Experimental environment Instal...

Trash-Cli: Command-line Recycle Bin Tool on Linux

I believe everyone is familiar with the trashcan,...

XHTML Getting Started Tutorial: Form Tags

<br />Forms are an important channel for use...

Vue3 draggable left and right panel split component implementation

Table of contents Breaking down components Left P...

Example code for implementing page floating box based on JS

When the scroll bar is pulled down, the floating ...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

React implements the expansion and collapse function of complex search forms

Give time time and let the past go. In the previo...