Home Web Front-end JS Tutorial jquery carousel lottery function implementation_jquery

jquery carousel lottery function implementation_jquery

May 19, 2018 pm 04:09 PM

1. Materials used

2. The code is as follows, focusing on the js part

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery九宫格大转盘抽奖</title>
<style>
#lottery{width:570px;height:510px;margin:0px auto;border:4px solid #ba1809;}
#lottery table{background-color:yellow;}
#lottery table td{position:relative;width:190px;height:170px;text-align:center;color:#333;font-index:-999}
#lottery table td img{display:block;width:190px;height:170px;}
#lottery table td a{width:190px;height:170px;display:block;text-decoration:none;background:url(images/lottery1.jpg) no-repeat top center;}
#lottery table td a:hover{background-image:url(images/lottery2.jpg);}
#lottery table td.active .mask{display:block;}
.mask{
 width:100%;
 height:100%;
 position:absolute;
 left:0;
 top:0;
 background:url(images/mask.png) no-repeat;
 display:none;
}
</style>
</head>
<body class="keBody">
<!--效果html开始-->
<p id="lottery">
 <table border="0" cellpadding="0" cellspacing="0">
  <tr>
   <td class="lottery-unit lottery-unit-0"><img src="images/gift0.jpg"><p class="mask"></p></td>
   <td class="lottery-unit lottery-unit-1"><img src="images/gift1.jpg"><p class="mask"></p></td>
   <td class="lottery-unit lottery-unit-2"><img src="images/gift2.jpg"><p class="mask"></p></td>
  </tr>
  <tr>
   <td class="lottery-unit lottery-unit-7"><img src="images/gift7.jpg"><p class="mask"></p></td>
   <td><a href="#"></a></td>
   <td class="lottery-unit lottery-unit-3"><img src="images/gift3.jpg"><p class="mask"></p></td>
  </tr>
  <tr>
   <td class="lottery-unit lottery-unit-6"><img src="images/gift6.jpg"><p class="mask"></p></td>
   <td class="lottery-unit lottery-unit-5"><img src="images/gift5.jpg"><p class="mask"></p></td>
   <td class="lottery-unit lottery-unit-4"><img src="images/gift4.jpg"><p class="mask"></p></td>
  </tr>
 </table>
</p>

<script src="http://code.jquery.com/jquery-latest.js?7.1.34"></script>
<script type="text/javascript">
var lottery={
 index:-1, //当前转动到哪个位置,起点位置
 count:0, //总共有多少个位置
 timer:0, //setTimeout的ID,用clearTimeout清除
 speed:20, //初始转动速度
 times:0, //转动次数
 cycle:50, //转动基本次数:即至少需要转动多少次再进入抽奖环节
 prize:-1, //中奖位置
 init:function(id){
  if ($("#"+id).find(".lottery-unit").length>0) {
   $lottery = $("#"+id);
   $units = $lottery.find(".lottery-unit");
   this.obj = $lottery;
   this.count = $units.length;
   $lottery.find(".lottery-unit-"+this.index).addClass("active");
  };
 },
 roll:function(){
  var index = this.index;
  var count = this.count;
  var lottery = this.obj;
  $(lottery).find(".lottery-unit-"+index).removeClass("active");
  index += 1;
  if (index>count-1) {
   index = 0;
  };
  $(lottery).find(".lottery-unit-"+index).addClass("active");
  this.index=index;
  return false;
 },
 stop:function(index){
  this.prize=index;
  return false;
 }
};

function roll(){
 lottery.times += 1;
 lottery.roll();//转动过程调用的是lottery的roll方法,这里是第一次调用初始化
 if (lottery.times > lottery.cycle+10 && lottery.prize==lottery.index) {
  clearTimeout(lottery.timer);
  lottery.prize=-1;
  lottery.times=0;
  click=false;
 }else{
  if (lottery.times<lottery.cycle) {
   lottery.speed -= 10;
  }else if(lottery.times==lottery.cycle) {
   var index = Math.random()*(lottery.count)|0;
   lottery.prize = index;  
  }else{
   if (lottery.times > lottery.cycle+10 && ((lottery.prize==0 && lottery.index==7) || lottery.prize==lottery.index+1)) {
    lottery.speed += 110;
   }else{
    lottery.speed += 20;
   }
  }
  if (lottery.speed<40) {
   lottery.speed=40;
  };
  //console.log(lottery.times+&#39;^^^^^^&#39;+lottery.speed+&#39;^^^^^^^&#39;+lottery.prize);
  lottery.timer = setTimeout(roll,lottery.speed);//循环调用
 }
 return false;
}

var click=false;

window.onload=function(){
 lottery.init(&#39;lottery&#39;);
 $("#lottery a").click(function(){
  if (click) {//click控制一次抽奖过程中不能重复点击抽奖按钮,后面的点击不响应
   return false;
  }else{
   lottery.speed=100;
   roll(); //转圈过程不响应click事件,会将click置为false
   click=true; //一次抽奖完成后,设置click为true,可继续抽奖
   return false;
  }
 });
};
</script>
<!--效果html结束-->

</body>
</html>
Copy after login

The effect is as follows:


3. Notes

.mask starts as follows. It uses rgba, but IE8 is not compatible. Instead, use png image background:url(images/mask.png) no-repeat;

.mask {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
background-color: rgba(252,211,4,0.5);
display: none
}
Copy after login

I hope you can gain something from this article compiled by the editor, and it will be helpful to you in implementing the lottery function.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles