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实现字体颜色渐变效果的方法
Mar 29 jQuery
jQuery实现web页面樱花坠落的特效
Jun 01 jQuery
JQuery判断正整数整理小结
Aug 21 jQuery
jQuery实现广告条滚动效果
Aug 22 jQuery
jquery实现倒计时小应用
Sep 19 jQuery
jQueryMobile之窗体长内容的缺陷与解决方法实例分析
Sep 20 jQuery
Vue中正确使用jQuery的方法
Oct 30 jQuery
jquery获取元素到屏幕四周可视距离的方法
Sep 05 jQuery
IE8中jQuery.load()加载页面不显示的原因
Nov 15 jQuery
JQuery的加载和选择器用法简单示例
May 13 jQuery
详解webpack引用jquery(第三方模块)的三种办法
Aug 21 jQuery
jQuery中event.target和this的区别详解
Aug 13 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
同一空间绑定多个域名而实现访问不同页面的PHP代码
2006/12/06 PHP
php增删改查示例自己写的demo
2013/09/04 PHP
ThinkPHP的cookie和session冲突造成Cookie不能使用的解决方法
2014/07/01 PHP
laravel-admin 后台表格筛选设置默认的查询日期方法
2019/10/03 PHP
自己的js工具 Event封装
2009/08/21 Javascript
canvas轨迹回放功能实现
2017/12/20 Javascript
vue配置请求本地json数据的方法
2018/04/11 Javascript
angular4笔记系列之内置指令小结
2018/11/09 Javascript
JS实现带阴历的日历功能详解
2019/01/24 Javascript
[44:15]DOTA2上海特级锦标赛主赛事日 - 5 败者组决赛Liquid VS EG第二局
2016/03/06 DOTA
Python操作列表的常用方法分享
2014/02/13 Python
python异步任务队列示例
2014/04/01 Python
Python基于pygame实现的font游戏字体(附源码)
2015/11/11 Python
Python实现通过文件路径获取文件hash值的方法
2017/04/29 Python
Python实现的单向循环链表功能示例
2017/11/10 Python
python3.4控制用户输入与输出的方法
2018/10/17 Python
TensorFlow查看输入节点和输出节点名称方式
2020/01/04 Python
Django多层嵌套ManyToMany字段ORM操作详解
2020/05/19 Python
python转化excel数字日期为标准日期操作
2020/07/14 Python
python 模拟登录B站的示例代码
2020/12/15 Python
CSS3中background-clip和background-origin的区别示例介绍
2014/03/10 HTML / CSS
马来西亚网上美容店:Hermo.my
2017/11/25 全球购物
美国机场停车位预订:About Airport Parking
2018/03/26 全球购物
BannerBuzz加拿大:在线定制横幅印刷、广告和标志
2020/03/10 全球购物
客户经理岗位职责
2013/12/08 职场文书
追悼会上的答谢词
2014/01/10 职场文书
环保小标语
2014/06/13 职场文书
四风问题专项整治工作情况报告
2014/10/28 职场文书
2014镇党委书记党建工作汇报材料
2014/11/02 职场文书
校车司机安全责任书
2015/05/11 职场文书
道歉的话语大全
2015/05/12 职场文书
穆斯林的葬礼读书笔记
2015/06/26 职场文书
2015年新教师个人工作总结
2015/10/14 职场文书
初中信息技术教学反思
2016/02/16 职场文书
Java图书管理系统,课程设计必用(源码+文档)
2021/06/30 Java/Android
PostgreSQL之连接失败的问题及解决
2023/05/08 PostgreSQL