jQuery实现适用于移动端的跑马灯抽奖特效示例


Posted in jQuery onJanuary 18, 2019

本文实例讲述了jQuery实现适用于移动端的跑马灯抽奖特效。分享给大家供大家参考,具体如下:

图片全部隐私处理

跑马灯抽奖特效难点一:奖品位置排放,如下图

jQuery实现适用于移动端的跑马灯抽奖特效示例

<div class="gift_div">
  <div class="gift gift1">奖品1</div>
  <div class="gift gift2">奖品2</div>
  <div class="gift gift3">奖品3</div>
  <div class="gift gift4">奖品4</div>
  <div class="gift gift5">奖品5</div>
  <div class="gift gift6">奖品6</div>
  <div class="gift gift7">奖品7</div>
  <div class="gift gift8">奖品8</div>
  <div class="start">开始抽奖</div>
</div>

按照代码常规,奖品1,2,3,4是顺序排列,在这里,使用了定位将他们绕成一个圈。

难点二:速度控制,其实这个没啥,多尝试几个速度就行;

js代码重点就是定时器的循环,代码如下:

$(function() {
  var speed = 150, //跑马灯速度
    click = true, //阻止多次点击
    img_index = -1, //阴影停在当前奖品的序号
    circle = 0, //跑马灯跑了多少次
    maths,//取一个随机数;
    num=$('.red').text();
  $('.start').click(function() {
    if(click&&num>0) {
      click = false;
      maths = parseInt((Math.random() * 10) + 80);
      light();
    } else {
      return false;
    }
  });
  function light() {
    img();
    circle++;
    var timer = setTimeout(light, speed);
    if(circle > 0 && circle < 5) {
      speed -= 10;
    } else if(circle > 5 && circle < 20) {
      speed -= 5;
    } else if(circle > 50 && circle < 70) {
      speed += 5
    } else if(circle > 70 && circle < maths) {
      speed += 10
    } else if(circle == maths) {
      var text = $('.gift_div .gift:eq(' + img_index + ')').text();
      console.log(circle + maths, 'aaa', img_index, $('.gift_div .gift:eq(' + img_index + ')').text())
      clearTimeout(timer);
      setTimeout(function() {
        alert('恭喜获得' + text)
      }, 300)
      click = true;
      speed = 150;
      circle = 0;
      img_index = -1;
      num--;
      $('.red').text(num)
    }
  }
  function img() {
    if(img_index < 7) {
      img_index++;
    } else if(img_index == 7) {
      img_index = 0;
    }
    $('.gift_div .gift:eq(' + img_index + ')').addClass('gift_b').siblings().removeClass('gift_b');
  }
});

上面的代码,从最上面定义我们所需的各种参数(都已做了注解);

接着点击开始抽奖,首先,在抽奖执行以前我们要先判断让一次的抽奖是否已经结束并且今天是否还有剩余的抽奖次数,当这两个条件都满足,开始执行抽奖light(),同时,在开始抽奖之前,将click这个参数置为false,避免抽奖还没结束用户就开始下一次的抽奖;

在抽奖light()函数里面调用抽奖阴影不停移动的函数img(),接着,给一个定时器var timer = setTimeout(light, speed);这个定时器里面的light就是根据speed的速度来不停的调用light()这个函数本身(城会玩),然后我们在下面根据这个抽奖阴影移动的次数不停地改变speed来改变light的调用速度从而改变阴影的移动速度(这个速度自己看数值怎么舒服怎么改吧);

最后在这个light()函数的最后要做定时器的清除,抽奖总要抽到东西的呀,不暂停怎么抽。。暂停以后要重置开始抽奖之前的参数。

上面有一个maths随机数,这个是随机让用户抽奖随机中哪一个,要是需要固定比例的下一节出。

完整代码如下:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <script src="js/rem.js" type="text/javascript" charset="utf-8"></script>
    <link rel="stylesheet" type="text/css" href="css/choujiang.css" rel="external nofollow" />
    <style type="text/css">
    </style>
  </head>
  <body>
    <div class="light">
      <div class="num">
        您今日抽奖机会还有<span class="red">3</span>次
      </div>
      <div class="gift_div">
        <div class="gift gift1">奖品1</div>
        <div class="gift gift2">奖品2</div>
        <div class="gift gift3">奖品3</div>
        <div class="gift gift4">奖品4</div>
        <div class="gift gift5">奖品5</div>
        <div class="gift gift6">奖品6</div>
        <div class="gift gift7">奖品7</div>
        <div class="gift gift8">奖品8</div>
        <div class="start">开始抽奖</div>
      </div>
    </div>
  </body>
<script src="js/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/choujiang.js" type="text/javascript" charset="utf-8"></script>
</html>

css部分:

* {
  margin: 0;
  padding: 0;
}
.light {
  width: 100%;
  height: 7.6rem;
  background: #BD1D25;
  padding: .2rem;
  box-sizing: border-box;
  font-size: .24rem;
}
.light .gift_div {
  width: 100%;
  height: 6.4rem;
  background: #139365;
  border-radius: .1rem;
  position: relative;
  padding: .05rem .5%;
  box-sizing: border-box;
  margin-top: .2rem;
}
.gift_div>div {
  position: absolute;
  width: 32%;
  height: 2rem;
  margin: .05rem .5%;
  background: #E6F0EC;
  border-radius: .06rem;
}
.gift2,
.gift6,
.start{
  left: 33.5%;
}
.gift3,
.gift4,
.gift5{
  right: .5%;
}
.gift4,
.gift8,
.start{
  top: 2.15rem;
}
.gift5,
.gift6,
.gift7{
  bottom: .05rem;
}
.gift_div .start{
  background: #FDB827;
  text-align: center;
  line-height: 2rem;
  color: #FF001F;
}
.red{
  color: red;
}
.num{
  text-align: center;
  font-size: .32rem;
  line-height: .6rem;
  background: #E6EFEC;
  border-radius: .6rem;
}
.gift_b:after{
  position: absolute;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.6);
  content: '';
  left: 0;
}

js部分:

$(function() {
  var speed = 150, //跑马灯速度
    click = true, //阻止多次点击
    img_index = -1, //阴影停在当前奖品的序号
    circle = 0, //跑马灯跑了多少次
    maths,//取一个随机数;
    num=$('.red').text();
  $('.start').click(function() {
    if(click&&num>0) {
      click = false;
      maths = parseInt((Math.random() * 10) + 80);
      light();
    } else {
      return false;
    }
  });
  function light() {
    img();
    circle++;
    var timer = setTimeout(light, speed);
    if(circle > 0 && circle < 5) {
      speed -= 10;
    } else if(circle > 5 && circle < 20) {
      speed -= 5;
    } else if(circle > 50 && circle < 70) {
      speed += 5
    } else if(circle > 70 && circle < maths) {
      speed += 10
    } else if(circle == maths) {
      var text = $('.gift_div .gift:eq(' + img_index + ')').text();
      console.log(circle + maths, 'aaa', img_index, $('.gift_div .gift:eq(' + img_index + ')').text())
      clearTimeout(timer);
      setTimeout(function() {
        alert('恭喜获得' + text)
      }, 300)
      click = true;
      speed = 150;
      circle = 0;
      img_index = -1;
      num--;
      $('.red').text(num)
    }
  }
  function img() {
    if(img_index < 7) {
      img_index++;
    } else if(img_index == 7) {
      img_index = 0;
    }
    $('.gift_div .gift:eq(' + img_index + ')').addClass('gift_b').siblings().removeClass('gift_b');
  }
});

html里面引用的rem.js是我自己封装的,让100px=1rem;

PS:这里推荐两款相关在线HTML/CSS/JS运行工具如下:

在线HTML/CSS/JavaScript代码运行工具:
http://tools.3water.com/code/HtmlJsRun

在线HTML/CSS/JavaScript前端代码调试运行工具:
http://tools.3water.com/code/WebCodeRun

希望本文所述对大家jQuery程序设计有所帮助。

jQuery 相关文章推荐
jQuery+pjax简单示例汇总
Apr 21 jQuery
jquery+css实现侧边导航栏效果
Jun 12 jQuery
基于JQuery的Ajax方法使用详解
Aug 16 jQuery
jQuery Ajax向服务端传递数组参数值的实例代码
Sep 03 jQuery
jQuery使用zTree插件实现可拖拽的树示例
Sep 23 jQuery
jQuery实现的下雪动画效果示例【附源码下载】
Feb 02 jQuery
jQuery实现的简单对话框拖动功能示例
Jun 05 jQuery
详解如何使用webpack打包多页jquery项目
Feb 01 jQuery
详解webpack引用jquery(第三方模块)的三种办法
Aug 21 jQuery
基于JQuery实现页面定时弹出广告
May 08 jQuery
jQuery实现购物车全功能
Jan 11 jQuery
jQuery class属性操作addClass()与removeClass()、hasClass()、toggleClass()
Mar 31 jQuery
jQuery实现的3D版图片轮播示例【滑动轮播】
Jan 18 #jQuery
Jquery遍历筛选数组的几种方法和遍历解析json对象,Map()方法详解以及数组中查询某值是否存在
Jan 18 #jQuery
jquery的$().each和$.each的区别
Jan 18 #jQuery
jquery层次选择器的介绍
Jan 18 #jQuery
jQuery无冲突模式详解
Jan 17 #jQuery
JQuery判断radio单选框是否选中并获取值的方法
Jan 17 #jQuery
Jquery获取radio选中值实例总结
Jan 17 #jQuery
You might like
实用函数4
2007/11/08 PHP
php笔记之:AOP的应用
2013/04/24 PHP
怎样使用php与jquery设置和读取cookies
2013/08/08 PHP
深入讲解PHP的Yii框架中的属性(Property)
2016/03/18 PHP
PHP回调函数与匿名函数实例详解
2017/08/16 PHP
php curl操作API接口类完整示例
2019/05/21 PHP
javascript 强制刷新页面的实现代码
2009/12/13 Javascript
JS中把字符转成ASCII值的函数示例代码
2013/11/21 Javascript
Jquery Ajax解析XML数据(同步及异步调用)简单实例
2014/02/12 Javascript
Google 地图叠加层实例讲解
2016/08/06 Javascript
详解VueRouter进阶之导航钩子和路由元信息
2017/09/13 Javascript
关闭Vue计算属性自带的缓存功能方法
2018/03/02 Javascript
对layui中的onevent 和event的使用详解
2019/09/06 Javascript
使用React代码动态生成栅格布局的方法
2020/05/24 Javascript
JS画布动态实现黑客帝国背景效果
2020/11/08 Javascript
[46:47]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS NE
2014/05/22 DOTA
[53:23]Secret vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
[54:30]Liquid vs Newbee 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
完美解决python中ndarray 默认用科学计数法显示的问题
2018/07/14 Python
使用python打印十行杨辉三角过程详解
2019/07/10 Python
Python基于进程池实现多进程过程解析
2020/04/30 Python
瑞典的玛丽小姐:Miss Mary of Sweden
2019/02/13 全球购物
英国家电购物网站:Sonic Direct
2019/03/26 全球购物
Expedia西班牙:预订酒店、机票、旅行和廉价度假套餐
2019/04/10 全球购物
一家外企的面试题目(C/C++面试题,C语言面试题)
2014/03/24 面试题
final, finally, finalize的区别
2012/03/01 面试题
制冷与空调专业毕业生推荐信
2014/07/07 职场文书
2014年秘书工作总结
2014/11/25 职场文书
2014小学语文教学工作总结
2014/12/17 职场文书
2015年党支部公开承诺书
2015/01/22 职场文书
2015年团支部工作总结
2015/04/03 职场文书
2015年护理工作总结范文
2015/04/03 职场文书
设备技术员岗位职责
2015/04/11 职场文书
2015年音乐教师个人工作总结
2015/05/20 职场文书
鲲鹏 CentOS 7 安装Python3.7
2022/05/11 Servers
python如何将mat文件转为png
2022/07/15 Python