《用JavaScript实现幸运大转盘抽奖程序》 一

    前些日子开发了一个抽奖程序,这个程序百分之九十的逻辑使用JavaScript和JQuery写的,瞬间感觉能JS学到极致,也是一种境界,虽然自己继续向这种境界前进。

    首先说一下这抽奖程序的大体逻辑,首先说怎样让转盘转起来,怎样抽中某个奖品,抽中奖品后怎样转盘停在准确位置。是这样子的,当我们点击抽奖的时候,这时候转盘开始转起来,当转够了足够圈数后,我们就利用Ajax触发一个事件,这个事件就是向后台数据库抽去一个奖品,等奖品抽出来后,将奖品的标识与转盘上的图片的标识以对应,然后实现定位。下面是代码。

    1、初始化抽奖程序

window.onload = function () {
            loaded();
            lottery.init('lottery');
            $("#lottery a").click(function () {
                if (click) {
                    return false;
                } else {
                    if (iNow == 0) return
                    lottery.speed = 100;
                    $("#lottery").find(".lottery-unit-" + lottery.index).removeClass("active");
                    lottery.index = -1;
                    roll();
                    click = true;
                    return false;
                }
            });
        };

    2、定义一个对象,这个对象负责给转盘上各个将品附加特效和初始化转盘的一些参数

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");
                if ($(".lottery-unit-" + (index + 1)).find("img").length!=0) {                 
                    index += 1;
                    if (index > count - 1) {
                        index = 0;
                    };
                    $(lottery).find(".lottery-unit-" + index).addClass("active");
                } else {                   
                    index += 1;
                    if (index > count - 1) {
                        index = 0;
                    };
                }
                this.index = index;
                return false;
            },
            stop: function (index) {
                this.prize = index;
                return false;
            }
        };

    3、最后由下一段代码进行逻辑判断,包括抽奖逻辑,以及定位逻辑。  

function roll() {

            lottery.times += 1;
            lottery.roll();
            if (lottery.times > lottery.cycle + 10 && lottery.prize == lottery.index || $("#lottery img").length==1) {
                clearTimeout(lottery.timer);
                if ($("#lottery img").length == 1) {
                    $.ajax({
                        url: "DrawDetail",
                        data: {向后台传递的参数},
                        type: "get",
                        dataType: "json",
                        success: function (data) {
                            console.log(data)
                            console.log(data.CnName)
                            iNow--;
                            $("#countClass").text('您还有' + iNow + "次抽奖机会!");
                            lottery.prize = data.SortNo;//这就是最终抽到的奖品标识

                        },
                        error: function (error) {
                            console.log(error);
                            if (error) {
                                alert("网络超时,请检查您的网络设置!");
                                location.replace(location.href);
                            }
                        }
                    })

                }
                setTimeout(function () {                
                    var index1 = lottery.prize;                   
			        $("#alert-div").show(500);			     
			        $("#alert-div img").attr("src", $(".lottery-unit-" + index1).attr("imgSrc"));
                	        $("#lottery").find(".lottery-unit-" + index1).html('');
                	        $("#lottery").find(".lottery-unit-" + index1).removeClass("active");
			        $("#lottery").find(".lottery-unit-" + index1).removeClass("lottery-unit-" + index1);

		        },600);

                lottery.times = 0;
                click = false;
            } else {
                
                    if (lottery.times < lottery.cycle) {
                        lottery.speed = 30;
                    } else if (lottery.times == lottery.cycle) {
                        $.ajax({
                            url: "DrawDetail",
                            data: {
                                userId: "f4048590-feec-4c15-990d-2f7693146937",
                                gameId: "FDFCB4C4-0865-4B33-BD31-BACF9B8A7EA7"
                            },
                            type: "get",
                            dataType: "json",
                            success: function (data) {
                                console.log(data)
                                console.log(data.CnName)
                                iNow--;
                                $("#countClass").text('您还有' + iNow + "次抽奖机会!");
                                //获取随机数(奖品个数范围内)
                                // var index = data[0].number;
                                lottery.prize = data.SortNo;

                            },
                            error: function (error) {
                                console.log(error);
                                if (error) {
                                    alert("网络超时,请检查您的网络设置!");
                                    location.replace(location.href);
                                }
                            }
                        })

                    } else {
                        if (lottery.times > lottery.cycle + 10 && (($("#lottery img").length >= 12) || lottery.prize == lottery.index + 1)) {
                            lottery.speed += 50;
                        } else if(lottery.times > lottery.cycle + 10 && ($("#lottery img").length < 12)){
                            lottery.speed += 10;
                        } 

                    }                            
                lottery.timer = setTimeout(roll, lottery.speed);

            }
            return false;
        }

     当程序启动后,转盘的速度靠着setTimeout()这个函数转动起来,将setTimeout()这个函数封装在转动函数roll()内部,这就实现了递归,使转盘不断转动起来,知道最终确定奖品的位置。


  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 21
    评论
queryRotate 这个插件就可以实现这个功能 jqueryRotate: 支持Internet Explorer 6.0+ 、Firefox 2.0 、Safari 3 、Opera 9 、Google Chrome,高级浏览器下使用Transform,低版本ie使用VML实现 google code地址:http://code.google.com/p/jqueryrotate/ 下面了解下这个插件怎么使用 1 $("触发转动元素").rotate(45); //直接这样子调用的话就是变换角度 2 3 $("触发转动元素").getRotateAngle(); //返回对象当前的角度 4 5 $("触发转动元素").stopRotare(); //停止旋转动画 6 7 $("触发转动元素").rotateRight() 向右旋转90度 $("触发转动元素").rotateLeft()向左旋转90度。 01 $("触发转动元素").rotate({   02      angle:0, //起始角度 03      animateTo:180, //结束的角度 04      duration:3000, //转动时间 05      callback:function(){}, //回调函数 06      easing:$.easing.easeOutSine // $.easing.easeInOutExpo 运动的效果,需要引用jquery.easing.min.js的文件 07   }) 08 09 $("触发转动元素").rotate(45); //转动45 10 11 $("触发转动元素").getRotateAngle(); //返回对象当前的角度 12 13 $("触发转动元素").stopRotare(); //停止旋转动画 14 15 另外可以更方便的通过调用$(el).rotateRight()和$(el).rotateLeft()来分别向右旋转90度和向左旋转90度。 大转盘原理:随机出一个数字,通过后台算出在那个概率区间返回指定跳转的角度 例 一等奖 概率1% 如果在1-100 随机出100则跳转一等奖的角度范围内 二等奖 概率2% 如果在1-100 随机出99-98则跳转二等奖的角度范围内 三等奖 概率97% 如果在1-100 随机出97-1则跳转三等奖的角度范围内 下面以一个简单demo为例子 01 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 02 <html xmlns="http://www.w3.org/1999/xhtml"> 03 <head> 04 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 05 <script type="text/javascript" src="jquery-1.8.0.min.js"></script> 06 <script type="text/javascript" src="jQueryRotate.2.2.js"></script> 07 <script type="text/javascript" src="jquery.easing.min.js"></script> 08 <script type="text/javascript" src="zp.js"></script> 09 <style type="text/css"> 10 body { 11 background: url(bg.png) 0 0 repeat; 12 } 13 14 .rotate-content { 15 width: 270px; 16 position: relative; 17 height: 270px; 18 background: url(activity-lottery-bg.png) 19 no-repeat 0 0; 20 background-size: 100% 100%; 21 margin: 0 auto 22 } 23 24 .rotate-con-pan { 25 background: url(disk.jpg) 26 no-repeat 0 0; 27 backgr
好的,实现幸运大转盘需要一些基本的前端技术,包括HTML、CSS和JavaScript。以下是一个简单的实现: HTML代码: ```html <div class="container"> <div class="wheel"> <div class="section"><p>1</p></div> <div class="section"><p>2</p></div> <div class="section"><p>3</p></div> <div class="section"><p>4</p></div> <div class="section"><p>5</p></div> <div class="section"><p>6</p></div> <div class="section"><p>7</p></div> <div class="section"><p>8</p></div> </div> <button class="spin-btn">Spin!</button> </div> ``` CSS代码: ```css .container { display: flex; justify-content: center; align-items: center; height: 100vh; } .wheel { position: relative; width: 300px; height: 300px; border-radius: 50%; overflow: hidden; } .section { position: absolute; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; background-color: #f8c471; transition: transform 5s ease-in-out; } .section:nth-child(2n) { background-color: #f0b27a; } .section:nth-child(3n) { background-color: #e67e22; } .section:nth-child(4n) { background-color: #d35400; } .section:nth-child(5n) { background-color: #c0392b; } .section:nth-child(6n) { background-color: #8e44ad; } .section:nth-child(7n) { background-color: #2980b9; } .section:nth-child(8n) { background-color: #27ae60; } .section p { font-size: 2em; font-weight: bold; color: #fff; } .spin-btn { font-size: 2em; margin-top: 30px; padding: 10px 20px; border-radius: 5px; background-color: #3498db; color: #fff; cursor: pointer; } ``` JavaScript代码: ```javascript const wheel = document.querySelector('.wheel'); const spinBtn = document.querySelector('.spin-btn'); let deg = 0; spinBtn.addEventListener('click', () => { deg = Math.floor(5000 + Math.random() * 5000); wheel.style.transition = 'transform 5s ease-in-out'; wheel.style.transform = `rotate(${deg}deg)`; }); wheel.addEventListener('transitionend', () => { const currentSection = Math.floor((deg % 360) / 45); alert(`You won section ${currentSection + 1}!`); wheel.style.transition = 'none'; wheel.style.transform = 'rotate(0)'; }); ``` 实现幸运大转盘是一个由8个扇形区域组成的轮盘,每个区域都有不同的颜色和数字。点击“Spin!”按钮会使轮盘旋转一定的角度,最终停止在某个扇形区域上,并弹出该区域的数字。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值