redBag
var redBag = (function () {
var initialed = false,
raining = true,
createInterval,
walkInterval,
createIntervalTime = 500,//生成红包间隔时间
walkIntervalTime = 30,//红包动画的间隔时间
imgPath = 'img/',
images = ['monkey1.png', 'monkey2.png', 'monkey3.png', 'monkey4.png', 'monkey5.png', 'monkey6.png','monkey7.png','monkey8.png','monkey9.png','monkey10.png'],
windowWidth = $(window).width(),
windowHeight = $(window).height(),
callback = null;
/**
* 设置定时器
*/
function redBagSetInterval() {
if (raining && !createInterval) {
createInterval = setInterval(createRedBag, createIntervalTime);
}
if (raining && !walkInterval) {
walkInterval = setInterval(walk, walkIntervalTime);
}
}
/**
* 清除定时器
*/
function redBagClearInterval() {
if (createInterval) {
clearInterval(createInterval);
createInterval = null;
}
if (walkInterval) {
clearInterval(walkInterval);
walkInterval = null;
}
}
/**
* minValue 最小值
* maxValue 最大值
*/
function generateRandom(minValue, maxValue) {
/**
* 取值范围总数
*/
var choices = maxValue - minValue + 1;
return Math.floor(Math.random() * choices + minValue);
}
function createRedBag() {
var leftValue = generateRandom(0, windowWidth);
/**
* 图片随机出现
*/
var imgSrc = imgPath + images[generateRandom(0, images.length - 1)];
//
$('<img src="' + imgSrc + '"/>').addClass('red-bag').css({left: leftValue + 'px'})
//30
.data('step', 3)
.appendTo('.red-bag-wrap');
}
/**
* 红包运动
*/
function walk() {
$('.red-bag').each(function () {
$(this).css({top: parseInt($(this).css('top')) + $(this).data('step') + 'px'});
//
if (windowHeight <= parseInt($(this).css('top'))) {
$(this).remove();
}
});
}
function redBagWrapDelegate() {
/**
* 红包选中事件
*/
$('.red-bag-wrap').delegate('.red-bag', 'click', function () {
raining = false;
redBagClearInterval();
loadingUtil.show();
//这边需要后台交互,后台判断是否已经参与活动,是否中奖(后台随机生成)
try {
var redBagAjax = $.ajax({
url: 'http://www.wood365.com/Ashx/MarAct.ashx?' + Math.random(), //请求的URL
type: 'POST',
timeout: 1000, //超时时间设置,单位毫秒
dataType: 'json',//返回的数据格式
success: function (data) { //请求成功的回调函数
var status = data.status;
var message = '';
switch (status) {
case 0:
message = '恭喜您获得' + data.prize + ',我们会在\
3个工作日内将奖品寄送到您注册时所\
填的地址,如有其他疑问请联系客服!';
break;
case 1:
message = '抱歉,该红包中没有奖品哦,继续加油吧!';
break;
case 2:
message = '抱歉!您已无抽奖机会.';
break;
default :
break;
}
requestDone(message);
},
error: function (data) { //请求失败的回调函数
requestDone('网络有问题,请重试');
},
complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
if (status == 'timeout') {//超时,status还有success,error等值的情况
redBagAjax.abort();
requestDone('请求超时,请重试');
}
}
});
}
catch(e) {
requestDone('出错了,请重试');
}
function requestDone(message) {
loadingUtil.hide();
$('.red-bag-result p').text(message);
$('.red-bag-result-mask').show();
$('.red-bag-wrap').undelegate('.red-bag', 'click');
$('.red-bag').addClass('no-click');
//2016-3-3新添加
$('.close-wrap').unbind('click');
}
//test start
//setTimeout(function () {
// var status = generateRandom(0,2);
// var message = '';
// switch (status) {
// case 0:
// message = '恭喜您获得 iPhone 6S 一台,我们会在\
// 3个工作日内将奖品寄送到您注册时所\
// 填的地址,如有其他疑问请联系客服!';
// break;
// case 1:
// message = '抱歉,该红包中没有奖品哦,继续加油吧!';
// break;
// case 2:
// message = '抱歉!您已无抽奖机会.';
// break;
// default :
// break;
// }
//
// loadingUtil.hide();
// $('.red-bag-result p').text(message);
// $('.red-bag-result-mask').show();
// $('.red-bag-wrap').undelegate('.red-bag', 'click');
//$('.red-bag').addClass('no-click');
//}, 2000);
// test end
});
}
var init = function () {
initialed = true;
/**
* body添加class名为red-bag-wrap
*/
$('<div></div>').addClass('red-bag' +
'-wrap')
.width(windowWidth)
.height(windowHeight)
.appendTo('body');
/**
* 添加关闭
*/
$('<div></div>').addClass('close-relative').appendTo('.red-bag-wrap');
$('<div></div>').addClass('close-wrap').appendTo('.close-relative');
$('<img src="img/close.png" />').appendTo('.close-wrap');
//2016-3-3关闭按钮回到初始页面
$('.close-wrap').on('click',function() {
stop();
});
$('<div></div>').addClass('red-bag-result-mask').hide().appendTo('.red-bag-wrap');
$('<div></div>').addClass('red-bag-result').appendTo('.red-bag-result-mask');
$('<p></p>').appendTo('.red-bag-result');
$('<div></div>').addClass('red-bag-close').appendTo('.red-bag-result');
/**
* 钱包确定点击事件
*/
$('.red-bag-close').on('click', function () {
raining = true;
redBagWrapDelegate();//这是猴子的点击事件
$('.red-bag').removeClass('no-click');
$('.red-bag-result-mask').hide();
redBagSetInterval();
$('.red-bag-mask').remove();
//2016-3-3福袋的时候关闭
$('.close-wrap').on('click',function() {
stop();
});
});
//
redBagSetInterval();
/**
* 生成红包
*/
redBagWrapDelegate();
$(window).resize(function () {
windowWidth = $(window).width();
windowHeight = $(window).height();
$('.red-bag-wrap').width(windowWidth).height(windowHeight);
});
window.onfocus = function () {
redBagSetInterval();
};
window.onblur = function () {
redBagClearInterval();
};
$(document).unload(function () {
redBagClearInterval();
});
};
//开始
var start = function (fn) {
if(typeof fn === 'function') {
callback = fn;
}
if(!initialed) {
init();
}
else {
resume();
}
};
//停止
var stop = function () {
$('.red-bag-wrap').hide();
redBagClearInterval();
$('.red-bag').remove();
callback();
};
//继续
var resume = function () {
$('.red-bag-wrap').show();
redBagSetInterval();
};
return {
start: start,
stop: stop
}
}());
$(function () {
function redBagBtnBindClick() {
$('.red-bag-btn').on('click', function () {
$('.red-bag-btn').unbind('click');
redBag.start(redBagBtnBindClick);
});
}
redBagBtnBindClick();
});
redBag的更多相关文章
- EntityFramework 优化建议
Entity Framework目前最新版本是6.1.3,当然Entity Framework 7 目前还是预览版,并不能投入正式生产环境,估计正式版16年第一季度会出来,了解过EF7的部分新特性后, ...
- 洛谷 P1466 集合 Subset Sums Label:DP
题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...
- NOIp 2012 #2 借教室 Label:区间修改线段树
题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然 ...
- C#实用杂记-EF全性能优化技巧
原文链接:http://www.makmong.com/947.html#comment-31 EntityFramework 优化建议 2016年1月15日 下午4:54 LEILINKANG ...
- [洛谷2397]yyy loves Maths VI
题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居然也不会,所以只好找你 题目描述 他让redbag找众数他还特意 ...
- Codeforces Round #446 (Div. 2)
Codeforces Round #446 (Div. 2) 总体:rating涨了好多,虽然有部分是靠和一些大佬(例如redbag和ShichengXiao)交流的--希望下次能自己做出来2333 ...
- luogu【P2753】[USACO4.3]字母游戏Letter Game
这个题...一开始看了很久题目(并且问了机房几个大佬)才明白题意.. (原题入口) 题意 大概是一开始给你一些字母出现的次数 你之后组成的单词(最多两个单词)每个字母出现次数 必须小于或等于标准(st ...
- THUWC2018滚粗记
THUWC2018滚粗记 前言 又是一篇滚粗记, 不过可能还要写过很多很多篇滚粗记, 才会有一篇不是滚粗记的东西啦 总而言之,我现在还是太菜了 还要过一大段时间我才会变强啦 Day -inf 联赛考完 ...
- NOIp2017 滚粗记
NOIp2017 滚粗记 Day0 早上 早自习的时候,班主任忽然告诉我们, 我们要参加期中考试... 这对于我们真是一个沉重的打击... 但是,管不着了 明天就死去考试了 上午 \(8:10\)到了 ...
随机推荐
- C#调用WebService实例和开发(转)
1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...
- vector 汇总
Vector成员函数 函数 表述 c.assign(beg,end) c.assign(n,elem) 将[beg; end)区间中的数据赋值给c. 将n个elem的拷贝赋值给c. c.at(idx) ...
- theano安装
theano安装有两类方法,一种是自己一步步安装,还有一种是借助其他软件安装.我是安装到一半发现第二种方法的...........所以就用的第一种麻烦的办法装的,但是过程也是一种学习. 电脑:win7 ...
- window.location.href("url") 无法在chrome和Firefoxz中使用
今天在js代码中加了一句window.location.href(‘url’)希望实现页面的跳转,IE中可以正常使用,但是Firefox却提示window.location is not a func ...
- 容联手机接口封装到ThinkPHP3.2.菜鸟图文教学
今天来说下短信发送技术. 使用的是 容联http://www.yuntongxun.com/ 用法很简单, 具体要知道的参数有 ACCOUNT SID 应用ID AUTH TOKEN 应用toke ...
- Nginx安装配置PHP(FastCGI)环境的教程
这篇是Nginx安装配置PHP(FastCGI)环境的教程.Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用. 一.什么是 FastCGI F ...
- ASP.NET 短路由配置
1. 首先在项目新建文件叫App_Code或者App_Start 在文件中新建WebFromRouteHandler.cs 文件. WebFromRouteHandler中的代码如下, public ...
- 以Qemu模拟Linux,学习Linux内核
文章名称:以Qemu模拟Linux,学习Linux内核作 者:five_cent文章地址:http://www.cnblogs.com/senix/archive/2013/02/21/29 ...
- C++之单元测试
以前编写程序从没有做过单元测试的工作,所以在后期会花很多时间去纠错,这也就是软件工程中的2:8定律.最近要完成一个项目,要求要对系统中的主类和主函数作出单元测试的保证,才去查找了相关方面的资料,看过后 ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...