国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery實現適用于移動端的跑馬燈抽獎特效示例

2019-11-19 12:15:48
字體:
來源:轉載
供稿:網友

本文實例講述了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.VeVB.COm/code/HtmlJsRun

在線HTML/CSS/JavaScript前端代碼調試運行工具:
http://tools.VeVB.COm/code/WebCodeRun

更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery圖片操作技巧大全》、《jQuery表格(table)操作技巧匯總》、《jQuery切換特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常用插件及用法總結》、《jQuery常見經典特效匯總》及《jquery選擇器用法總結

希望本文所述對大家jQuery程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲松县| 油尖旺区| 宜章县| 特克斯县| 布拖县| 茂名市| 巴林左旗| 雅江县| 睢宁县| 兰州市| 茌平县| 梁河县| 怀来县| 陕西省| 武山县| 黄石市| 东源县| 六安市| 海安县| 拉萨市| 漳浦县| 微博| 江口县| 视频| 平江县| 临城县| 泸西县| 辽宁省| 新乐市| 望江县| 周宁县| 梁平县| 武定县| 沅陵县| 九龙县| 定边县| 北京市| 集贤县| 贡嘎县| 若羌县| 抚顺市|