js组件SlotMachine实现图片切换效果制作抽奖系统


Posted in Javascript onApril 17, 2016

前言:前两天在网上找组件,无意中发现了我们儿时游戏机效果的“SlotMachine组件”,浏览一遍下来,勾起了小时候满满的回忆。

下面就带着大家来看看这么一个神奇的组件——SlotMachine吧。

一、组件预览

先来一发简单的效果压压惊

js组件SlotMachine实现图片切换效果制作抽奖系统

觉得太简单?别急,好戏在后头,试试手气先。

js组件SlotMachine实现图片切换效果制作抽奖系统

什么?还没达到想要的效果,好!下面,真实效果来一发。

js组件SlotMachine实现图片切换效果制作抽奖系统

点击了好长时间,都没有中奖,难怪小时候怎么都赢不了呢。不信邪,继续点击开始,终于有一次中奖的了,真心不容易。

js组件SlotMachine实现图片切换效果制作抽奖系统

还有我们年终抽奖效果,开始!停止!

js组件SlotMachine实现图片切换效果制作抽奖系统

二、代码示例

既然是js组件,肯定是先要下载组件库。首先贴上 开源地址

js组件SlotMachine实现图片切换效果制作抽奖系统

然后来看看文件的引用:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <link href="~/Content/jQuery-SlotMachine-master/dist/jquery.slotmachine.css" rel="stylesheet" />

    <link href="~/Content/jQuery-SlotMachine-master/css/style.css" rel="stylesheet" />

    <link href="~/Content/toastr/toastr.min.css" rel="stylesheet" />
    <script type="text/javascript" src="//code.jquery.com/jquery-2.1.1.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

    <script src="~/Content/jQuery-SlotMachine-master/dist/jquery.slotmachine.js"></script>

    <script src="~/Content/toastr/toastr.min.js"></script>

这里需要注意几点:

1)、引用jquery和bootstrap都是通过cdn加速的方式引用的,不懂cdn加速的可以百度。
2)、Jquery组件必须,并且组件需要Jquery 2.0以上版本的支持,版本太低会有js异常。
3)、bootstrap组件并非必须,但是本篇布局需要部分bootstrap的样式支持。
4)、toastr组件并非必须,此处用于显示中奖的结果。
1、试试手气效果代码
html部分

<div id="triky">
 <div class="content" style="text-align: center">
  <h1>请选择你想吃的食物</h1>

  <div class="row">
  <div style="margin: auto;">
   <div id="triky1">
   <div>
    <img src="/Content/jQuery-SlotMachine-master/img/cookie.png" />
   </div>
   <div>
    <img src="/Content/jQuery-SlotMachine-master/img/food1.jpg" />
   </div>
   <div>
    <img src="/Content/jQuery-SlotMachine-master/img/food2.jpg" />
   </div>
   <div>
    <img src="/Content/jQuery-SlotMachine-master/img/food3.jpg" />
   </div>
   </div>
   <div>
   <div class="btn-group btn-group-justified btn-group-triky" style="margin-left:-15px" role="group">
    <div id="trikyShuffle" type="button" class="btn btn-primary btn-lg">试试手气</div>
   </div>
   </div>
  </div>
  </div>
 </div>

 <div class="clearfix"></div>
 </div>

JS部分

$(function () {
  //试试手气
  var triky = $("#triky1").slotMachine({
  active: 2, //初始化的时候显示的项的索引
  //delay: 150,//切换两张图片的间隔时间(毫秒单位)
  //randomize: function () {
  // return 0;//每次旋转后选中值的索引(从0开始)
  //}
  });

  $("#trikyShuffle").click(function () {
  triky.shuffle(8);//开始旋转方法,参数8表示每次旋转跳过8个图标
  });
 });

JS常用属性、方法、事件详解

(1)初始化方法 var machine = $("#id").slotMachine({}); 返回当前旋转的对象。slotMachine()方法里面传递初始化的参数,比如

active:表示初始化的时候显示项的索引,从0开始
delay:切换两张图片的间隔时间(毫秒单位)
auto:是否自动旋转,取值为true or false
spins:当auto为true的时候,这是每次跳过图标的个数
stophidden:是否出现开始和停止时候的动画
randomize:function(activeElementIndex){}此属性表示每次旋转后选中值的索引(从0开始)
direction:动画的方向,取值(up||down)
(2)常用方法

 machine.shuffle( repeat, onStopCallback ); 表示开始旋转,repeat表示每次跳过的图片个数;onstopCallback表示旋转停止后的事件回调方法。
 machine.prev(); 返回前一个元素
 machine.next(); 返回后一个元素
 machine.stop(); 停止旋转
 machine.active; 得到选中的元素的索引
 machine.running; 检测是否正在旋转,true表示正在旋转
 machine.stopping; 检测是否已经停止
 machine.destroy(); 摧毁旋转节点
2、简单游戏机效果代码示例
html部分

<div id="randomize">
 <div class="content container" style="text-align: center;max-width: 900px;">
  <h1>简易游戏机</h1>

  <div class="row">
  <div class="col-xs-4">
   <div>
   <div id="machine1" class="randomizeMachine">
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot1.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot2.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot3.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot4.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot5.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot6.png" /></div>
   </div>
   </div>
  </div>

  <div class="col-xs-4">
   <div>
   <div id="machine2" class="randomizeMachine">
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot1.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot2.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot3.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot4.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot5.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot6.png" /></div>
   </div>
   </div>
  </div>

  <div class="col-xs-4">
   <div>
   <div id="machine3" class="randomizeMachine">
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot1.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot2.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot3.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot4.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot5.png" /></div>
    <div><img src="/Content/jQuery-SlotMachine-master/img/slot6.png" /></div>
   </div>
   </div>
  </div>
  </div>

  <div>
  <div class="btn-group btn-group-justified btn-group-randomize" role="group">
   <div id="ranomizeButton" type="button" class="btn btn-danger btn-lg">开始</div>
  </div>
  </div>
 </div>
 </div>

JS部分

$(function () {
  //简易游戏机
  var machine1 = $("#machine1").slotMachine({
  active: 0,
  delay: 500
  });

  var machine2 = $("#machine2").slotMachine({
  active: 1,
  delay: 500,
  direction: 'down'
  });

  var machine3 = $("#machine3").slotMachine({
  active: 2,
  delay: 500
  });

  var arr = [];
  function onComplete(active) {
  if (arr.length <= 1) {
   arr.push(active);
  }
  else if (arr.length > 1) {
   arr.push(active);
   if (arr[0] == arr[1] && arr[1] == arr[2]) {
   toastr.success("恭喜你中奖了!");
   }
   else if (arr[0] == arr[1] || arr[0] == arr[2] || arr[1] == arr[2]) {
   toastr.success("还差一点,继续加油");
   }
   else {
   toastr.success("手气不行");
   }
   arr = [];
  }
  }

  $("#ranomizeButton").click(function () {

  machine1.shuffle(5, onComplete);

  setTimeout(function () {
   machine2.shuffle(5, onComplete);
  }, 500);

  setTimeout(function () {
   machine3.shuffle(5, onComplete);
  }, 1000);

  })
 });

3、单个停止效果代码示例
Html部分

<div id="casino" style="padding-top:50px;">
 <div class="content">
  <h1>抽奖</h1>

  <div>
  <div id="casino1" class="slotMachine" style="margin-left: -65px;">
   <div class="slot slot1"></div>
   <div class="slot slot2"></div>
   <div class="slot slot3"></div>
   <div class="slot slot4"></div>
   <div class="slot slot5"></div>
   <div class="slot slot6"></div>
  </div>

  <div id="casino2" class="slotMachine">
   <div class="slot slot1"></div>
   <div class="slot slot2"></div>
   <div class="slot slot3"></div>
   <div class="slot slot4"></div>
   <div class="slot slot5"></div>
   <div class="slot slot6"></div>
  </div>

  <div id="casino3" class="slotMachine">
   <div class="slot slot1"></div>
   <div class="slot slot2"></div>
   <div class="slot slot3"></div>
   <div class="slot slot4"></div>
   <div class="slot slot5"></div>
   <div class="slot slot6"></div>
  </div>

  <div class="btn-group btn-group-justified btn-group-casino" role="group">
   <div id="slotMachineButtonShuffle" type="button" class="btn btn-primary btn-lg">开始</div>
   <div id="slotMachineButtonStop" type="button" class="btn btn-primary btn-lg">停止</div>
  </div>
  </div>

 </div>
 <div class="clearfix"></div>
 </div>

JS部分

$(function () {
  //单个停止
  var machine4 = $("#casino1").slotMachine({
  active: 0,
  delay: 500
  });

  var machine5 = $("#casino2").slotMachine({
  active: 1,
  delay: 550
  });

  machine6 = $("#casino3").slotMachine({
  active: 2,
  delay: 600
  });

  var started = 0;

  $("#slotMachineButtonShuffle").click(function () {
  started = 3;
  machine4.shuffle();
  machine5.shuffle();
  machine6.shuffle();
  });

  $("#slotMachineButtonStop").click(function () {
  switch (started) {
   case 3:
   machine4.stop();
   break;
   case 2:
   machine5.stop();
   break;
   case 1:
   machine6.stop();
   break;
  }
  started--;
  });
 });

三、总结
整个过程并不复杂,所有的属性、事件、方法基本看看文档都能很好理解运用,演示代码也没什么好说的,一看就懂,希望对大家学习javascript组件有所帮助。

Javascript 相关文章推荐
jQuery与ExtJS之选择实例分析
Aug 19 Javascript
JavaScript判断DOM何时加载完毕的技巧
Nov 11 Javascript
jQuery背景插件backstretch使用指南
Apr 21 Javascript
js纯数字逐一停止显示效果的实现代码
Mar 16 Javascript
js实现带简单弹性运动的导航条
Feb 22 Javascript
JS操作xml对象转换为Json对象示例
Mar 25 Javascript
jquery实现tab选项卡切换效果(悬停、下方横线动画位移)
May 05 jQuery
Node.JS循环删除非空文件夹及子目录下的所有文件
Mar 12 Javascript
vue路由中前进后退的一些事儿
May 18 Javascript
vue动态注册组件实例代码详解
May 30 Javascript
jquery实现直播弹幕效果
Nov 28 jQuery
基于JavaScript实现轮播图效果
Jan 02 Javascript
JS中取二维数组中最大值的方法汇总
Apr 17 #Javascript
JS组件Bootstrap ContextMenu右键菜单使用方法
Apr 17 #Javascript
js实现select二级联动下拉菜单
Apr 17 #Javascript
基于javascript实现精确到毫秒的倒计时限时抢购
Apr 17 #Javascript
第二次聊一聊JS require.js模块化工具的基础知识
Apr 17 #Javascript
第一次接触JS require.js模块化工具
Apr 17 #Javascript
jqGrid 学习笔记整理——进阶篇(一 )
Apr 17 #Javascript
You might like
Aptana调试javascript图解教程
2009/11/30 Javascript
js 未结束的字符串常量错误解决方法
2010/06/13 Javascript
JavaScript 创建运动框架的实现代码
2013/05/08 Javascript
理解javascript中的回调函数(callback)
2014/09/02 Javascript
一道JS前端闭包面试题解析
2015/12/25 Javascript
jQuery的实例及必知重要的jQuery选择器详解
2016/05/20 Javascript
ES6新特征数字、数组、字符串
2016/10/01 Javascript
用v-html解决Vue.js渲染中html标签不被解析的问题
2016/12/14 Javascript
H5上传本地图片并预览功能
2017/05/08 Javascript
详解webpack2+React 实例demo
2017/09/11 Javascript
Nuxt配合Node在实际生产中的应用详解
2018/08/07 Javascript
微信小程序学习笔记之登录API与获取用户信息操作图文详解
2019/03/29 Javascript
ionic2.0双击返回键退出应用
2019/09/17 Javascript
[01:42]TI4西雅图DOTA2前线报道 第一顿早饭哦
2014/07/08 DOTA
[01:16:13]DOTA2-DPC中国联赛 正赛 SAG vs Dragon BO3 第一场 2月22日
2021/03/11 DOTA
python的urllib模块显示下载进度示例
2014/01/17 Python
Python自动登录126邮箱的方法
2015/07/10 Python
python中is与双等于号“==”的区别示例详解
2017/11/21 Python
浅谈python可视化包Bokeh
2018/02/07 Python
使用python进行文本预处理和提取特征的实例
2018/06/05 Python
Python mutiprocessing多线程池pool操作示例
2019/01/30 Python
Python2与Python3的区别实例总结
2019/04/17 Python
Django错误:TypeError at / 'bool' object is not callable解决
2019/08/16 Python
python使用socket 先读取长度,在读取报文内容示例
2019/09/26 Python
Python使用Pandas库常见操作详解
2020/01/16 Python
实现Python3数组旋转的3种算法实例
2020/09/16 Python
北美Newegg打造的全球尖货海购平台:tt海购
2018/09/28 全球购物
乌克兰鞋类购物网站:Eobuv.com.ua
2020/11/28 全球购物
在对linux系统分区进行格式化时需要对磁盘簇(或i节点密度)的大小进行选择,请说明选择的原则
2012/01/13 面试题
致1500米运动员广播稿
2014/02/07 职场文书
目标责任书范文
2014/04/14 职场文书
2015年主婚人婚礼致辞
2015/07/28 职场文书
导游词之桂林
2019/08/20 职场文书
提高系统的吞吐量解决数据库重复写入问题
2022/04/23 MySQL
pd.drop_duplicates删除重复行的方法实现
2022/06/16 Python
Linux在两个服务器直接传文件的操作方法
2022/08/05 Servers