大家好!先上图看看本次案例的整体效果。

完整版实战课程附源码:【Vue.js游戏机实战】- Vue.js实现老虎-机抽奖

实现思路:

  1. Vue component实现老虎-机组件,可以嵌套到任意要使用的页面。
  2. css3 transform控制老虎-机抽奖过程的动画效果。
  3. 抽奖组件内使用钩子函数watch监听抽奖结果的返回情况播放老虎-机动画并给用户弹出中奖提示。
  4. 中奖结果弹窗,为抽奖组件服务。

实现步骤如下:

  1. 构建api奖品配置信息和抽奖接口,vuex全局存放奖品配置和中奖结果数据信息。
    api:

    export default {
    getPrizeList () {
    let prizeList = [
    {
    id: 1,
    name: '小米8',
    img: 'https://i1.mifile.cn/f/i/g/2015/cn-index/m8-140.png'
    },
    {
    id: 2,
    name: '小米电视',
    img: 'https://i1.mifile.cn/f/i/g/2015/TV4A-43QC.png'
    }, {
    id: 3,
    name: '小米平衡车',
    img: 'https://i1.mifile.cn/f/i/g/2015/cn-index/scooter-140!140x140.jpg'
    }, {
    id: 4,
    name: '小米耳机',
    img: 'https://c1.mifile.cn/f/i/g/2015/video/pinpai140!140x140.jpg'
    }
    ]
    return prizeList
    },
    lottery () {
    return {
    id: 4,
    name: '小米耳机',
    img: 'https://c1.mifile.cn/f/i/g/2015/video/pinpai140!140x140.jpg'
    }
    }
    }

    store:

    import lotteryApi from '../../api/lottery.api.js'
    
    const state = {
    prizeList: [],
    lotteryResult: {}
    } const getters = {
    prizeList: state => state.prizeList,
    lotteryResult: state => state.lotteryResult
    } const mutations = {
    SetPrizeList (state, { prizeList }) {
    state.prizeList = prizeList
    },
    SetLotteryResult (state, { lotteryResult }) {
    state.lotteryResult = lotteryResult
    }
    } const actions = {
    getPrizeList ({ commit }) {
    let result = lotteryApi.getPrizeList()
    commit('SetPrizeList', { prizeList: result })
    },
    lottery ({ commit }) {
    let result = lotteryApi.lottery()
    commit('SetLotteryResult', { lotteryResult: result })
    }
    } export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
    }
  2. 编写抽奖组件,为保证通用性,组件只负责播放抽奖结果。接收两个数据和一个方法,如下:
    数据一:预置的奖品列表数据(轮播奖品需要)
    数据二:抽奖结果,播放抽奖动画和弹出中奖结果需要
    方法:抽奖动作,返回的抽奖结果数据即为数据二,响应式传递给组件
    大概代码思路如下(仅供参考,不可运行)
    <template>
    <section>
    <div class="main">
    <!-- 老虎-机 -->
    <div class="recreation">
    <div class="recreation-list clearfix" v-if="slotPrizes.length>0">
    <ul class="recreation-item" v-bind:style="{transform: 'translateY('+y1+'rem)'}">
    <li class="recreation-prices" v-for="(item,index) in slotPrizes" v-bind:key="index">
    <img v-bind:src="item.img" alt>
    </li>
    </ul>
    <ul class="recreation-item" v-bind:style="{transform: 'translateY('+y2+'rem)'}">
    <li class="recreation-prices" v-for="(item,index) in slotPrizes" v-bind:key="index">
    <img v-bind:src="item.img" alt>
    </li>
    </ul>
    <ul class="recreation-item" v-bind:style="{transform: 'translateY('+y3+'rem)'}">
    <li class="recreation-prices" v-for="(item,index) in slotPrizes" v-bind:key="index">
    <img v-bind:src="item.img" alt>
    </li>
    </ul>
    </div>
    <a
    href="javascript:;"
    @click="parentEmitLottery"
    class="btn-recreation"
    ></a>
    </div>
    <prize-pop :prize="lotteryResult" v-if="showPrize" @closeLotteryPop="showPrize=false"/>
    </div>
    </section>
    </template>
    <script>
    import PrizePop from './common/prize-pop.vue'
    export default {
    name: 'SlotMachine',
    data () {
    return {
    }
    },
    components: {
    PrizePop
    },
    props: {
    prizeList: {
    type: Array,
    default: () => []
    },
    lotteryResult: {
    type: Object,
    default: () => {}
    }
    },
    computed: {
    slotPrizes () {
    let prizes = []
    if (this.prizeList.length > 0) {
    Object.assign(prizes, this.prizeList)
    prizes.push(this.prizeList[0])
    }
    return prizes
    }
    },
    methods: {
    /**
    * 触发父页面调用抽奖
    */
    parentEmitLottery () {
    this.$emit('lottery')
    },
    /**
    * 开始抽奖
    */
    startLottery () {
    },
    /**
    * 获取中奖结果所在奖品列表中的索引,以确定抽奖动画最终落在哪个奖品
    */
    getPrizeIndex () {
    },
    /**
    * 执行抽奖动画
    */
    startPlay (index1, index2, index3) {
    }
    },
    watch: {
    /**
    * 监听抽奖结果,一旦有中奖信息就开始执行抽奖动画
    */
    lotteryResult (newVal, oldVal) {
    if (newVal.id && newVal.id > 0) {
    this.startLottery()
    }
    }
    }
    }
    </script>
  3. 弹出中奖结果组件,依附于抽奖组件,在上一步的执行抽奖结果动画结束后执行。
    <template>
    <div class="subject-pop" style="z-index: 10;" v-if="prize.id>0">
    <div class="subject-pop-mask"></div>
    <div class="subject-pop-box">
    <h3>恭喜您</h3>
    <p>
    <img :src="prize.img" alt>
    </p>
    <h4>获得
    <span></span>
    <span>{{prize.name}}</span>
    </h4>
    <div class="subject-pop-footer">
    <a href="javascript:;" class="november-btn1" @click="closeLotteryEmit">知道了</a>
    </div>
    </div>
    </div>
    </template>
    <script>
    export default {
    props: {
    prize: {
    type: Object,
    default: () => {
    return {
    id: 0
    }
    }
    }
    },
    methods: {
    closeLotteryEmit () {
    this.$emit('closeLotteryPop')
    }
    }
    }
    </script>
  4. 抽奖组件运用在需要使用的页面中,此页面需要为抽奖组件提前准备好预置奖品列表和中奖结果信息,并提供好抽奖方法供子组件(抽奖组件)触发,触发完更改抽奖结果响应式传入到抽奖组件中。
    <template>
    <section>
    <div style="width:100%;text-align:center;margin:2rem 0;">您有一次抽奖机会,祝君好运~~~</div>
    <slotMachine :prizeList="prizeList" :lotteryResult="lotteryResult" @lottery="lottery"/>
    </section>
    </template> <script>
    import { mapGetters, mapActions } from 'vuex'
    import slotMachine from '@/components/slotMachine'
    export default {
    created () {
    this.getPrizeList()
    },
    components: {
    slotMachine
    },
    computed: {
    ...mapGetters({
    prizeList: 'lottery/prizeList',
    lotteryResult: 'lottery/lotteryResult'
    })
    },
    methods: {
    ...mapActions({
    getPrizeList: 'lottery/getPrizeList',
    lottery: 'lottery/lottery'
    })
    }
    }
    </script>

以上就是老虎-机抽奖核心步骤的整体思路,欢迎讨论。

完整版实战课程附源码:【Vue.js游戏机实战】- Vue.js实现老虎-机抽奖

Vue.js实战之游戏抽奖系列全集

↓↓↓↓↓↓↓↓↓↓↓

【Vue.js实战案例】- Vue.js实现老虎-机抽奖总结

【Vue.js实战案例】- Vue.js实现九宫格水果机抽奖游戏总结

【Vue.js实战案例】- Vue.js实现大转盘抽奖总结

【Vue.js游戏机实战】- Vue.js实现老虎-机抽奖总结的更多相关文章

  1. 【Vue.js游戏机实战】- Vue.js实现九宫格水果机抽奖游戏总结

    大家好!先上图看看本次案例的整体效果. 完整版实战课程附源码:[Vue.js游戏机实战]- Vue.js实现九宫格水果机抽奖 实现思路: Vue component实现九宫格水果机组件,可以嵌套到任意 ...

  2. 【Vue.js游戏机实战】- Vue.js实现大转盘抽奖总结

    大家好!先上图看看本次案例的整体效果. 实现思路: Vue component实现大转盘组件,可以嵌套到任意要使用的页面. css3 transform控制大转盘抽奖过程的动画效果. 抽奖组件内使用钩 ...

  3. js 大转盘,老虎 机

     http://www.helloweba.com/view-blog-215.htmlhttp://www.ui3g.com/demos/show/1408/http://www.js-css.cn ...

  4. Vue.js起手式+Vue小作品实战

    本文是小羊根据Vue.js文档进行解读的第一篇文章,主要内容涵盖Vue.js的基础部分的知识的,文章顺序基本按照官方文档的顺序,每个知识点现附上代码,然后根据代码给予个人的一些理解,最后还放上在线编辑 ...

  5. 08Vue.js快速入门-Vue综合实战项目

    8.1. 前置知识学习 npm 学习 官方文档 推荐资料 npm入门 npm介绍 需要了解的知识点 package.json 文件相关配置选项 npm 本地安装.全局安装.本地开发安装等区别及相关命令 ...

  6. 每天记录一点:NetCore获得配置文件 appsettings.json vue-router页面传值及接收值 详解webpack + vue + node 打造单页面(入门篇) 30分钟手把手教你学webpack实战 vue.js+webpack模块管理及组件开发

    每天记录一点:NetCore获得配置文件 appsettings.json   用NetCore做项目如果用EF  ORM在网上有很多的配置连接字符串,读取以及使用方法 由于很多朋友用的其他ORM如S ...

  7. 分享Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站

    这是个什么的项目? 使用 Node.js + Koa2 + MySQL + Vue.js 实战开发一套完整个人博客项目网站. 博客线上地址:www.boblog.com Github地址:https: ...

  8. 第10章-Vue.js 项目实战

    一.本节内容 掌握项目环境中路由的配置方法 ***** 熟练掌握编写单文件组件的编写 *** 能够使用swiper.js进行轮播图组件的封装 能够使用axios进行数据请求 二.webpack项目的目 ...

  9. 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 十七 ║Vue基础:使用Vue.js 来画博客首页+指令(一)

    缘起 书说前两篇文章<十五 ║ Vue前篇:JS对象&字面量&this>和 <十六 ║ Vue前篇:ES6初体验 & 模块化编程>,已经通过对js面向对 ...

随机推荐

  1. python3基础之“函数(1)”

    1.type:查看当前字符串的类型 c=' print(type(c),c) b= int(c) print(type(b),b) num=" a=int(num,base=16) prin ...

  2. karma mocha angular angular-mock 测试

    describe('工具方法测试', function () { var utilsModule; beforeEach(function () { module('Admin'); // modul ...

  3. C++线程同步之临界区

    #include <iostream> #include <windows.h> using namespace std; CRITICAL_SECTION cs; // Lo ...

  4. with读、写文件

    1.with写文件 save_file = "1.txt" str_data = "123a\nbc" with open(save_file, 'a', en ...

  5. iview 标题内边距过大; 调整iview 单元格内边距、行高;

    1css代码: /*调整table cell间隔和行高*/ .ivu-table-cell { padding-left: 1px; padding-right: 1px; } .ivu-table- ...

  6. ScheduledExecutorService周期性的定时任务

    从j2se的api文档上查看ScheduledExecutorService的方法都是推迟一段时间然后相隔一段时间之后再去执行,没有想Timer定时器一样的可以在定点时间执行的api,如果也想像Tim ...

  7. 搭建一个VUE项目

    搭建环境 搭建node环境 下载 1.进入node.js官方网站下载页,点击下图中框出位置,进行下载即可,当前版本为8.9.4,下载网址为:https://nodejs.org/zh-cn/downl ...

  8. 解决linux下创建用户时出现 Creating mailbox file: 文件已存在

    原来linux下添加用户后,会在系统里自动加一个邮箱(系统邮箱),路径是:/var/spool/mail/用户名.      可以直接用命令#rm -rf /var/spool/mail/用户名    ...

  9. Flask-SQLAlchemy操作指南

    Flask-SQLAlchemy官方文档 from flask_sqlalchemy import SQLAlchemy app = Flask(__name__)app.config['SQLALC ...

  10. 常用模块(collections模块,时间模块,random模块,os模块,sys模块,序列化模块,re模块,hashlib模块,configparser模块,logging模块)

    认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...