当前位置: 首页 > 编程日记 > 正文

九宫格抽奖转盘源码分析

    

效果如上图所示,下面对其实现代码进行分析,看能不能破解其抽奖规则。需要引入jquery-1.8.3.min.js和images/9张图片。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>九宫格抽奖转盘</title>
<style type="text/css">
#lottery{width:574px;height:584px;margin:20px auto 0;background:url(images/bg.jpg) no-repeat;padding:50px 55px;}
#lottery table td{width:142px;height:142px;text-align:center;vertical-align:middle;font-size:24px;color:#333;font-index:-999}
#lottery table td a{width:284px;height:284px;line-height:150px;display:block;text-decoration:none;}
#lottery table td.active{background-color:#ea0000;}
</style>
</head>
<body>
<div id="lottery"><table border="0" cellpadding="0" cellspacing="0"><tr><td class="lottery-unit lottery-unit-0"><img src="images/1.png"></td><td class="lottery-unit lottery-unit-1"><img src="images/2.png"></td><td class="lottery-unit lottery-unit-2"><img src="images/4.png"></td><td class="lottery-unit lottery-unit-3"><img src="images/3.png"></td></tr><tr><td class="lottery-unit lottery-unit-11"><img src="images/7.png"></td><td colspan="2" rowspan="2"><a href="#"></a></td><td class="lottery-unit lottery-unit-4"><img src="images/5.png"></td></tr><tr><td class="lottery-unit lottery-unit-10"><img src="images/1.png"></td><td class="lottery-unit lottery-unit-5"><img src="images/6.png"></td></tr><tr><td class="lottery-unit lottery-unit-9"><img src="images/3.png"></td><td class="lottery-unit lottery-unit-8"><img src="images/6.png"></td><td class="lottery-unit lottery-unit-7"><img src="images/8.png"></td><td class="lottery-unit lottery-unit-6"><img src="images/7.png"></td></tr></table>
</div>
<script type="text/javascript" src="jquery-1.8.3.min.js"></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();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+'^^^^^^'+lottery.speed+'^^^^^^^'+lottery.prize);
                lottery.timer = setTimeout(roll,lottery.speed);}return false;}var click=false;window.onload=function(){lottery.init('lottery');$("#lottery a").click(function(){if (click) {return false;}else{lottery.speed=100;roll();click=true;return false;}});};
</script>
</body>
</html>

先来看看Style样式的布局,如下:

<style type="text/css">
#lottery{width:574px;height:584px;margin:20px auto 0;background:url(images/bg.jpg) no-repeat;padding:50px 55px;}
#lottery table td{width:142px;height:142px;text-align:center;vertical-align:middle;font-size:24px;color:#333;font-index:-999}
#lottery table td a{width:284px;height:284px;line-height:150px;display:block;text-decoration:none;}
#lottery table td.active{background-color:#ea0000;}
</style>

以上为jQuery的选择器:

#选择ID,那么全局搜索:id="lottery"定位到了div九宫格区域。定义了div区域高、宽。内外边距(手机像素和PC像素不一样px),背景图片。

#lottery table td,是层叠选择器。选择所有的id="lottery" 标签下的table标签下的td标签(即所有td标签)。text-align:center即文本对齐方式:居中对齐(嵌入里面的图片居中对齐)。vertical-align:middle即垂直对齐方式:居中对齐(类比word中)。font-size:24px即字体宽度像素。font-index:-999即设置元素的堆叠顺序(这个是分层的)。line-height:150px即行高。display:block即让对象成为块级元素。text-decoration:none即设置text文本修饰。

var lottery={
         index:-1,    //当前转动到哪个位置,起点位置count:0,    //总共有多少个位置timer:0,    //setTimeout的ID,用clearTimeout清除speed:20,    //初始转动速度times:0,    //转动次数cycle:50,    //转动基本次数:即至少需要转动多少次再进入抽奖环节prize:-1,    //中奖位置
          running:false, //添加锁,使转盘在转动过程中再点击抽奖无效。

//JSON(JavaScript Object Notation), Notation字面的意思是符号,计法。类比JSON数据传输格式。JS对象计法。是最早期的JavaScript的对象定义方法。
                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;}
};

这是JS中定义对象的方式,不是jQuery定义对象。(说明一下,$(".type")返回的就是一个jQuery对象;同样CSS选择器返回的就是一个CSS对象,这点很重要)

以上定义了对象lottery,index = -1,count = 0,表示创建了对象的两个属性,并赋值。

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");};
},
//作用是将js的转动切换到就绪状态。好像运动员到了预备状态。一样。

对于lottery中的初始化函数init( ), $("#"+id).find(".lottery-unit").length>0 中使用了jQuery的find函数( find() 方法获得当前元素集合中每个元素的后代 ),看到上面class的定义中,class="lottery-unit lottery-unit-1"这两个class的名字是平等的关系。就是给它搞了两个名字(为什么这么使用,前端同学说,页面尽量不要用id,因为id会表示唯一,容易有冲突重复)。这个语句,判断了class="lottery-unit"的个数,正好是12个,奖品池中奖品个数。

JQuery中事件的return false;它表示阻止浏览器的默认行为。

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;                         //第一步,以速度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)) //第五步,大于60圈 且 同时满足中奖。迅速跳到中奖处。{lottery.speed += 110;}else //第四步,在(50圈,60圈)区间内,迅速依次序跳到获奖位置。{lottery.speed += 20;}} if (lottery.speed<40) { //第二步,保证圈圈转够50圈,以速度40。lottery.speed=40;};

 这个页面是由微信公众号来玩儿的,目的是引入流量。所以如果pc端访问H5页面的话,希望能够做出判断。根据当前的平台,选择适配的页面。这儿有一个知识点叫做:通过UserAgent判断智能手机(设备,Android,IOS)

// 代码的实现,通过加如下的js代码到首页 index.html中。
<
script type="text/javascript">function IsPhone() {var userAgentInfo = navigator.userAgent;var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone","iPad", "iPod"];var flag = true;for (var v = 0; v < Agents.length; v++) {if (userAgentInfo.indexOf(Agents[v]) > 0) {flag = false;break;}}return flag;}if (IsPhone()) { window.location ="https://www.qmcaifu.com";};</script>

整个js的操作:先init初始化到起点位置;然后执行roll方法,进行转盘操作。

转载于:https://www.cnblogs.com/RunForLove/p/4642746.html

相关文章:

关于使用strtok的一个小问题

今天在弄一下啊小小程序的时候。报错&#xff0c;出现了问题。先看代码 int main(int argc, char* argv[]) {char *filename "interface_ipset_1_1.json";char* split1 "_";char* split2 ".";char splitfile1[4][NAME_MAX];sagent_string_sp…

微信小程序发送模板消息,php发送模板消息

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 formId 在安卓系统是纯数字&#xff0c;在IOS系统是一串加密字符&#xff0c;如图&#xff1a; 发送模板消息&#xff08;服务通知&#xff09;效果图&#xff1a; 前端 wxml <form bindsubmit"…

使用TypeScript映射和条件类型使React组件更出色

by Deepu K Sasidharan通过Deepu K Sasidharan 使用TypeScript映射和条件类型使React组件更出色 (Make your React components great with TypeScript mapped and conditional types) You’ve probably heard about TypeScript. You may have heard someone claiming how grea…

2017年6月16号课堂笔记

2017年6月16号 星期五 空气质量&#xff1a;中度污染~轻度污染 内容&#xff1a;jQuery&#xff1a;remove&#xff0c;bind&#xff0c;attr&#xff0c;on和live&#xff0c;同辈和父辈节点的操作&#xff0c; keyup/keypress/keydown,focus-blur应用&#xff0c;表单事件/键…

大宗商品(Bulk Stock)交易

大宗商品&#xff08;Bulk Stock&#xff09;是指可进入流通领域&#xff0c;但非零售环节&#xff0c;具有商品属性用于工农业生产与消费使用的大批量买卖的物质商品。在金融投资市场&#xff0c;大宗商品指同质化. 可交易. 被广泛作为工业基础原材料的商品大宗商品电子交易主…

【Ant Design Pro 二】 创建页面,组件,并在页面调用

开发交流qq群 173683895 路由里面的参数作用介绍: {path: "/a_nowdayserver/nowdayserver", //随便取名,显示在访问路径url中,如果是子路由,需要和父路径匹配icon: "file", //菜单栏显示的图标name: "你好", //菜单栏显示的标题component…

安卓收取费用_作为自由职业者应收取的费用:以价值为基础的定价是否能达到炒作的目的?...

安卓收取费用by Benek Lisefski由Benek Lisefski 作为自由职业者应收取的费用&#xff1a;以价值为基础的定价是否能达到炒作的目的&#xff1f; (What to charge as a freelancer: does value-based pricing live up to the hype?) 定价很难。 (Pricing is hard.) Even afte…

Java笔记(25):设计模式概述

1、设计模式的概述和分类 设计模式&#xff1a; 经验的总结。 A:创建型 创建对象 B:结构型 对象的组成 C:行为型 对象的功能创建型模式&#xff1a;1)简单工厂模式 2)工厂方法模式 3)设计模式 2、简单工厂模式概述和使用 1 package cn.itcast_01; 2 3 public abstract class A…

Ant Design Pro 使用图表 charts bizcharts

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 淌了一下午坑,都是辛酸泪 总结:首先要知道, 它不能直接使用 charts ,需要安装 bizcharts 插件,然后导入 bizcharts 中的 charts; 点击跳转到 bizcharts 官方文档,建议看完整个流程再跳转 首先,…

【剑指offer 面试题47】不用加减乘除做加法

思路&#xff1a; 利用位运算 C&#xff1a; 1 #include <iostream>2 using namespace std;3 4 int main()5 {6 int a 11, b 17;7 int sum, carry;8 do9 { 10 sum a ^ b; 11 carry (a & b) << 1; 12 a sum; 13 …

travis-ci自动部署_如何使用Travis CI部署(几乎)零恐惧的Cloud Foundry应用

travis-ci自动部署by Robin Bobbitt罗宾波比(Robin Bobbitt) 如何使用Travis CI部署(几乎)零恐惧的Cloud Foundry应用 (How to deploy your Cloud Foundry app with (almost) zero fear using Travis CI) Application deployments to the cloud should be painless. We should…

Activiti 规则任务(businessRuleTask)

Activiti 规则任务&#xff08;businessRuleTask&#xff09; 作者&#xff1a;邓家海 目前国内研究Activiti规则任务businessRuleTask&#xff09;的文章在网上应该不超出3篇 小觑夜漫酒作伴&#xff0c;破晓黎明当笑言 前言&#xff1a; 最近一直在研究Activiti工作流的自动化…

10年工作经验老程序员推荐的7个开发类工具

做.NET软件工作已经10年了&#xff0c;从程序员做到高级程序员&#xff0c;再到技术主管&#xff0c;技术总监。见证了Visual Studio .NET 2003,Visul Studio 2005, Visual Studio Team System 2008, Visual Studio 2010 Ultimate,Visual Studio 2013一系列近5个版本的变化与亲…

PHP SSL certificate: unable to get local issuer certificate的解决办法

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 当本地curl需要访问https时&#xff0c;出现SSL certificate: unable to get local issuer certificate错误信息 解决办法&#xff1a; 到http://curl.haxx.se/ca/cacert.pem下载pem文件&#xff0c;并…

辞职前为什么挣扎_当您感到自己像开发人员一样挣扎时,为什么学得最多

辞职前为什么挣扎by Walt Schlender由Walt Schlender 当您感到自己像开发人员一样挣扎时&#xff0c;为什么学得最多 (Why you learn the most when you feel like you’re struggling as a developer) The times when I have made the greatest leaps in my development skil…

Hadoop学习之Mapreduce执行过程详解

一、MapReduce执行过程 MapReduce运行时&#xff0c;首先通过Map读取HDFS中的数据&#xff0c;然后经过拆分&#xff0c;将每个文件中的每行数据分拆成键值对&#xff0c;最后输出作为Reduce的输入&#xff0c;大体执行流程如下图所示&#xff1a; 整个流程图具体来说&#xff…

汇编试验十五:安装新的int 9中断例程

安装新的int 9中断例程&#xff08;按A键后显示满屏幕的A&#xff09; int 9 是外中断&#xff0c;同样&#xff0c;程序编写还是和其他中断例程类似&#xff0c;安装&#xff08;复制&#xff09;&#xff0c;调用&#xff1b; 不同点是在于&#xff0c;他要从端口读取数据60h…

php判断前端传的多个字段与数据库匹配

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 代码 <?phpheader("Content-Type:text/html;charsetutf8"); header("Access-Control-Allow-Origin: *"); //解决跨域header(Access-Control-Allow-Methods:POST);// 响应类型 …

javascript编写_用JavaScript深入探讨:为什么对编写好的代码至关重要。

javascript编写Using simple terminology and a real world example, this post explains what this is and why it is useful.这篇文章使用简单的术语和一个真实的例子&#xff0c;解释了this是什么以及为什么有用。 这是给你的吗 (Is this for you) I have noticed that man…

peak num

class Solution {public: int findPeakElement(vector<int>& nums) { int i0; int nnums.size(); while(i<n){ if(i0){ //处理第一位 if(nums[1] < nums[0]) return 0; else { …

用Eclipse的snippets功能实现代码重用

snippets功能实现代码重用 Snippets 代码片段是Eclipse的一个插件。 很多时候可以通过这个功能&#xff0c;重复使用常用的代码片段&#xff0c;加快开发效率。 创建一个代码段的步骤&#xff1a; 在Eclipse的editor中选中一块代码段&#xff0c;右键点击【Add to Snippets…

JS删除选中的数组

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 js // 删除数组deleteArr: function (e) {let middlearr [{a:1},{b:2}];//全部数组let items [{a:1}];//选中的数组for (var i 0; i < items.length; i) {for (var j 0; j < middlearr.lengt…

Git合并和变基简介:它们是什么,以及如何使用它们

by Vali Shah通过瓦利沙阿 Git合并和Git变基简介&#xff1a;它们做什么以及何时使用它们 (An Introduction to Git Merge and Git Rebase: What They Do and When to Use Them) As a Developer, many of us have to choose between Merge and Rebase. With all the reference…

[转]单点登录原理与简单实现

一、单系统登录机制 1、http无状态协议 web应用采用browser/server架构&#xff0c;http作为通信协议。http是无状态协议&#xff0c;浏览器的每一次请求&#xff0c;服务器会独立处理&#xff0c;不与之前或之后的请求产生关联&#xff0c;这个过程用下图说明&#xff0c;三次…

[JAVA] DUMP

jmap -dump:live,formatb,fileD:\heap.bin 31563156是PID转载于:https://www.cnblogs.com/MasterMonkInTemple/p/4655547.html

ThinkPHP 5.0 入门教程 一:安装ThinkPHP并在Web浏览器访问

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 严格来说&#xff0c;ThinkPHP无需安装过程&#xff0c;这里所说的安装其实就是把ThinkPHP框架放入WEB运行环境&#xff08;前提是你的WEB运行环境已经OK&#xff09; 下面我们开始安装ThinkPHP的运行环…

以太坊区块链同步_以太坊69:如何在10分钟内建立完全同步的区块链节点

以太坊区块链同步by Lukas Lukac卢卡斯卢卡奇(Lukas Lukac) Ethereu M 69&#xff1a;如何在10分钟内建立完全同步的区块链节点 (Ethereum 69: how to set up a fully synced blockchain node in 10 mins) Welcome in the first article of our new go-ethereum series!欢迎来…

微信小程序客服实现自动回复图文消息链接,点击去关注公众号

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 用户打开客服消息&#xff0c;发送任意消息自动回复图文链接&#xff0c;达到关注公众号的目的。 先看效果&#xff1a; 打开芝麻小客服的后台&#xff0c;选择一键接入小程序智能客服 点击跳转 1.授权…

HtmlUnit、httpclient、jsoup爬取网页信息并解析

转载&#xff1a;http://tianxingzhe.blog.51cto.com/3390077/1755511转载于:https://www.cnblogs.com/puhongtao/p/7063563.html

《Maven 实战》笔记之setting.xml介绍

maven是什么&#xff1f;有什么用&#xff1f; Maven是一个跨平台的项目管理工具,主要服务于Java平台的项目构建,依赖管理和项目信息管理。项目构建包括创建项目框架、清理、编译、测试、到生成报告&#xff0c;再到打包和部署&#xff0c;项目信息包括项目描述,开发者列表,版本…