介绍:

  前端九宫格是一种常见的抽奖方式,js实现如下,掌握其原理,不论多少宫格,都可以轻松应对。(代码可复制直接运行看效果)。

  该案例以四宫格入门,可扩展多宫格,奖品模块的布局可自由设置。

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>四宫格抽奖</title> </head> <style> html,body{
width: 100%;
height: 100%;
max-width: 750px;
margin: auto;
} .outBox{
width: 100%;
height: 60%;
background: gray;
position: relative;
display: flex;
flex-flow: row wrap; } .prize{
width: 44%;
height: 40%;
margin: 5% 3%;
text-align: center;
font-size: xx-large;
color: #fff;
padding-top: 15%;
box-sizing: border-box;
border: 6px double #AB945E;
} .prize.active{
background: goldenrod;
} .btn{
position: absolute;
left:30%;
bottom:-20%;
width: 40%;
text-align: center;
height: 12%;
font-size: xx-large;
} </style> <body> <div class="outBox" id="lotteryBoxs"> <div class="prize prize-0 one">一等奖</div>
<div class="prize prize-1 two">二等奖</div>
<div class="prize prize-3 four">谢谢参与</div>
<div class="prize prize-2 three">三等奖</div> <button class="btn">开启好运</button>
</div> </body> <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
<script> $(document).ready(function() {
// 转盘的初定义
var lottery = {
index: 0, //当前转动到哪个位置,起点位置
count: 0, //总共有多少个位置
timer: 0, //setTimeout的ID,用clearTimeout清除
speed: 10, //初始转动速度
times: 0, //转动次数
cycle: 30, //转动基本次数:即至少需要转动多少次再进入抽奖环节
prize: 0, //中奖位置
init: function(id) {
if ($("#" + id).find(".prize").length > 0) {
$lottery = $("#" + id);
$units = $lottery.find(".prize");
this.obj = $lottery;
this.count = $units.length;
}
},
roll: function() {
var index = this.index;
var count = this.count;
var lottery = this.obj;
$(lottery).find(".prize").removeClass("active");
index += 1;
if (index > count - 1) {
index = 0;
}
$(lottery).find(".prize-" + this.index).addClass("active");
this.index = index;
return false;
},
stop: function(index) {
this.prize = index;
return false;
}
}; // 中奖转动事件
function roll() {
lottery.times += 1;
lottery.roll();
var prize_site = $("#lotteryBoxs").attr("prize_site");
if (lottery.times > lottery.cycle + 10 && lottery.index == prize_site) {
var prize_id = $("#lotteryBoxs").attr("prize_id");
var prize_name = $("#lotteryBoxs").attr("prize_name");
console.log(prize_site+"结果")
//中奖情况的判断--模态框
if(prize_site == 1 || prize_site == 2 || prize_site == 3){
//已中奖
setTimeout(function(){
console.log("恭喜你获得" + prize_name)
},500)
}else{
//未中奖
setTimeout(function(){
console.log("中奖结果:" + prize_name)
},500) } clearTimeout(lottery.timer);
lottery.prize = -1;
lottery.times = 0;
click = false; } else {
if (lottery.times < lottery.cycle) {
lottery.speed -= 20;
} 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 == lottery.count - 1) || lottery.prize == lottery.index + 1)) {
lottery.speed += 90;
} else {
lottery.speed += 30;
}
}
if (lottery.speed < 30) {
lottery.speed = 30;
}
lottery.timer = setTimeout(roll, lottery.speed);
}
return false;
} var click = false; // 后台数据的调用
$(function() {
lottery.init('lotteryBoxs');
$(".btn").click(function() {
if (click) {
return false;
} else {
lottery.speed = 100; //此处数据应该从接口获取
var prizeArr=["谢谢参与","一等奖","二等奖","三等奖"]
var prizeId = Math.floor(Math.random()*(3-0+1)+0);
var prize_site = prizeId;
console.log("位置"+prizeId);
$("#lotteryBoxs").attr("prize_site", prize_site );
$("#lotteryBoxs").attr("prize_name", prizeArr[prizeId]);
roll();
click = true;
return false; }
});
}) }); </script> </html>

  页面效果如下:

  

前端js实现九宫格模式抽奖(多宫格抽奖)的更多相关文章

  1. 微信小程序多宫格抽奖

    最近闲来无事,做了一个多宫格抽奖的例子,有什么需要改进或者错误的地方,请留言,谢谢 首先看效果 思路是先让其转动2圈多,然后再进行抽奖,格子运动用的是setTimeout,让其运行的时间间隔不一样,然 ...

  2. 【Vue.js游戏机实战】- Vue.js实现九宫格水果机抽奖游戏总结

    大家好!先上图看看本次案例的整体效果. 完整版实战课程附源码:[Vue.js游戏机实战]- Vue.js实现九宫格水果机抽奖 实现思路: Vue component实现九宫格水果机组件,可以嵌套到任意 ...

  3. php+lottery.js制作九宫格抽奖实例

    php+lottery.js制作九宫格抽奖实例,本抽奖功能效果表现好,定制方便简单,新手学习跟直接拿来用都非常不错,兼容IE.火狐.谷歌等浏览器. 引入抽奖插件lottery.js <scrip ...

  4. 前端Js框架汇总

    概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人心,.net co ...

  5. 闲聊——浅谈前端js模块化演变

    function时代 前端这几年发展太快了,我学习的速度都跟不上演变的速度了(门派太多了,后台都是大牛公司支撑类似于facebook的react.google的angular,angular的1.0还 ...

  6. 2015 前端[JS]工程师必知必会

    2015 前端[JS]工程师必知必会 本文摘自:http://zhuanlan.zhihu.com/FrontendMagazine/20002850 ,因为好东东西暂时没看懂,所以暂时保留下来,供以 ...

  7. [ 学习路线 ] 2015 前端(JS)工程师必知必会 (2)

    http://segmentfault.com/a/1190000002678515?utm_source=Weibo&utm_medium=shareLink&utm_campaig ...

  8. 前端js,css文件合并三种方式,bat命令

    前端js,css文件合并三种方式,bat命令 前端js文件该如何合并三个方式如下:1. 一个大文件,所有js合并成一个大文件,所有页面都引用它.2. 各个页面大文件,各自页面合并生成自己所需js的大文 ...

  9. 前端JS模版库kino.razor - 原理流程分析 - 改进版轮子RazorJs

    1.前言 从后台获取数据,在前端JS里面拼接字符串,不累吗?敢不敢找一款前端使使... 现在这种模板库比较多了,我用过的jquery-template .JsRender .听说过的一堆,还有各种MV ...

随机推荐

  1. Python_教程_使用Visual Studio Code开发Django项目

    如何获得 Visual Studio Code 访问 http://code.visualstudio.com 下载并安装. 前提条件 安装Python 2.7 及 Python 3.5,Window ...

  2. 一个Wpf的开发框架

    引言 本框架使用Prism做MVVM,优点咱就不说了,主要了容器注入,消息和DI,比自己写省很多事.网上有很多标准的MVVM的使用方法,但是没有形成一个系统级的框架.本框架从登录到具体业务的使用,还有 ...

  3. Bugku-cms1

    一.先用御剑扫描 二.点击第一个目录,发现sql文件 三.把它载下来,用Notepad++打开.发现管理员的账号和密码(admin的密码好像被人改了,然后我用admin888登的后台)  四.后台地址 ...

  4. CVE-2017-11882利用

    CVE-2017-11882是微软公布的远程执行漏洞,通杀所有office版本及Windows操作系统 工具使用 本文使用的EXP来源于unamer/CVE-2017-11882,然后结合MSF进行渗 ...

  5. 厉害啊!第一次见到把Shiro运行流程写的这么清楚的,建议收藏起来慢慢看

    前言 shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和spring依赖 ...

  6. Android系统添加key和keypad

    平台:MTK 一.添加一个按键 1.在DCT tool keypad list 文件增加新按键的选项alps\mediatek\source\dct\Keypad_YuSu.cmp中添加新键,如SMS ...

  7. 怎么用思维导图软件iMindMap整理发文思路

    如果你是一个普通的博客作者,那么你就应该明白在枯竭时寻找灵感就像是一场噩梦,即使你有一千个想法,但是你无法将它们关联起来也是无用的,所以,为什么不试试iMindMap思维导图呢,尝试创新,进行组建,你 ...

  8. 在FL Studio编曲软件中查找采样的音高

    相信玩音乐的小伙伴们都遇到过这种情况,在用FL Studio编曲时,想添加一段音频采样,由于采样的调式与我们正在编曲的调式不同,音频之间的衔接就是一个非常头疼的问题,要解决采样的调式,我们就得先确认这 ...

  9. 【电子取证:镜像仿真篇】Windows Server镜像仿真、vmdk镜像仿真

    Windows Server镜像仿真.vmdk镜像仿真 时间过得真快呀!--[suy999] Windows Server镜像仿真.vmdk镜像仿真 一.qemu-img镜像转换工具 (一)raw.q ...

  10. img标签到底是行内元素还是块级元素

    面试官问你<img>是什么元素时你怎么回答 写这篇文章源自我之前的一次面试,题目便是问img标签属于块级元素还是行内元素,当时想都没想就说了是行内(inline)元素,面试官追问为什么能够 ...