


How to achieve lottery (square) effect in js? Implementation of two lottery effects (code example)
The content of this article is to introduce how to achieve the lottery (square) effect in js? Implementation of two lottery effects (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
exhibit:
HTML:
<div id="table"></div> <div id="btn"> <button onclick="start('p', 'active','newactive', 100)">顺序抽/停止</button> <button onclick="startRan('p', 'active','newactive', 100)">随机抽/停止</button> </div>
CSS:
table { text-align: center; border-collapse: collapse; } table * { width: 60px; height: 60px; } #btn { box-sizing: border-box; width: 190px; display: flex; justify-content: space-between; align-items: center; } #btn * { flex-grow: 1; background-color: red; border: 1px solid #000; color: #fff; height: 30px; font-size: 10px; } .active { background-color: #ccc; } .newactive { background-color: #00ffff; }
JavaScript:
// 定义一个奖池 var jackpot = [ ['奖品A1', '奖品A2', '奖品A3'], ['奖品B1', '奖品B2', '奖品B3'], ['奖品C1', '奖品C2', '奖品C3'] ]; /** * [table 创建表格] * @param {[Array]} arr [奖品数组] * @param {[String]} selector [选择器] * @return {[String]} table [返回一个HTML标签] */ function table(arr, selector) { var table = '<table border="1">'; for (var i = 0; i < arr.length; i++) { table += '<tr>'; for (var j = 0; j < arr[i].length; j++) { table += '<td class="' + selector + '">' + arr[i][j] + '</td>'; } table += '</tr>'; } table += '</table>'; return table; } // 输出奖池 document.getElementById('table').innerHTML = table(jackpot, 'p'); var key = true; // start,startRan控制器 var num = 3; // 抽奖次数 // 抽过的还能抽 可定义抽奖次数-->次数限制 num需要定义 // 不定义抽奖次数-->次数无限 num不需定义 // 抽过的不能抽 可定义抽奖次数-->次数限制(次数不超过选择器长度) num需要定义 // 不定义抽奖次数-->次数等于选择器长度 num需要定义 /** * [start 开始抽奖] * @param {[String]} selector [选择器] * @param {[String]} addselector [给选中的添加样式] * @param {[String]} newaddselector [中奖奖品样式] * @param {[Number]} speed [时间越小,速度越快] * @return {[type]} [description] */ function start(selector, addselector, newaddselector, speed) { if (key) { if (typeof(num) == 'undefined' || num != 0) { var count = 0; // 如果写成var timer会每次执行时重新定义一个timer,那么clearInterval(timer)只能清除后面定义的那个timer,前面定义的已经没有变量指向了 无法清除 timer = setInterval(function() { if (count < $('.' + selector).length) { $('.' + selector).eq(count).addClass(addselector); $('.' + selector).eq(count).siblings().removeClass(addselector); $('.' + selector).eq(count).parent().siblings().children().removeClass(addselector); count++; } else { count = 0; } }, speed); if(typeof(num) != 'undefined'){ num--; } } else{ key = false; console.log("抽奖结束"); } } else { clearInterval(timer); // 决定抽中的奖品的样式和抽中的奖品能否继续抽 $('.' + addselector).addClass(newaddselector).removeClass(selector); // 奖品 console.log($('.' + addselector).html()); } key = !key; } /** * [start 开始抽奖] * @param {[String]} selector [选择器] * @param {[String]} addselector [给选中的添加样式] * @param {[String]} newaddselector [中奖奖品样式] * @param {[Number]} speed [时间越小,速度越快] * @return {[type]} [description] */ function startRan(selector, addselector, newaddselector, speed) { if (key) { if (typeof(num) == 'undefined' || num != 0) { // 如果写成var timer会每次执行时重新定义一个timer,那么clearInterval(timer)只能清除后面定义的那个timer,前面定义的已经没有变量指向了 无法清除 timer = setInterval(function() { var count = Math.floor(Math.random() * $('.' + selector).length); $('.' + selector).eq(count).addClass(addselector); $('.' + selector).eq(count).siblings().removeClass(addselector); $('.' + selector).eq(count).parent().siblings().children().removeClass(addselector); }, speed); if(typeof(num) != 'undefined'){ num--; } } else { key = false; console.log("抽奖结束"); } } else { clearInterval(timer); // 决定抽中的奖品的样式和抽中的奖品能否继续抽 $('.' + addselector).addClass(newaddselector).removeClass(selector); // 奖品 console.log($('.' + addselector).html()); } key = !key; }
The above is the detailed content of How to achieve lottery (square) effect in js? Implementation of two lottery effects (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

In daily work, we encounter a lot of things that require drawing lots. The traditional method is to randomly draw numbers using paper numbers. With the development of electronic software, we can use computers to draw lots. The lesson I want to share with you today is How to make an excel lottery applet. 1. First, we open the Excel software and open the table we prepared. The table must contain our names. 2. Then we merge the cells on the right, fill in black who is lucky tonight, and merge the cells below and fill in red, as shown in the figure below. 3. Then we enter the randbetween function in the red area and set the first line to 2 and the last line to 7, as shown in the figure below. 4. Then we enter ind in front

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Detailed explanation of the design and implementation of PHP lottery system 1. Overview Lottery is a marketing tool used by many websites and applications. Through lottery, it can attract users to participate in activities, increase user interactivity and improve user stickiness. In this article, we will introduce in detail how to use PHP language to design and implement a simple lottery system. By studying this article, readers will understand the construction principles and specific code implementation of the lottery system. 2. System Design Before designing a lottery system, we first need to determine the functional requirements and processes of the system. one
