vue抽奖效果Demo

只做了简单的抽奖效果,没有给出弹框和中奖提示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="./vue.js"></script>
    <link rel="stylesheet" href="./index.css">
    <title>九宫格抽奖案例(PC端)</title>
</head>
<body>
    <div  id="app">
        <div class="lotteryBox" >
            <div v-for='(item,key) in lotteryData' :key="item.id" :class="key === index?'active':''">
                <img :src=item.imgUrl alt="" class="itemImg">
                <p>{{item.text}}</p>
            </div>
            
        
        </div>
        <div class="lotteryBtn" @click='lotteryBtn'>
            立即抽奖
        </div>
    </div>
</body>
</html>
<script>
var vm = new Vue({
    el:"#app",
    data:{
        lotteryData:[
            {id:0,imgUrl:"./count.png",text:"10元"},
            {id:1,imgUrl:"./count.png",text:"11元"},
            {id:2,imgUrl:"./count.png",text:"10元"},
            {id:3,imgUrl:"./count.png",text:"13元"},
            {id:4,imgUrl:"./count.png",text:"111元"},
            {id:5,imgUrl:"./count.png",text:"19元"},
            {id:6,imgUrl:"./count.png",text:"310元"},
            {id:7,imgUrl:"./count.png",text:"14元"},
            {id:8,imgUrl:"./count.png",text:"1元"},
        ],
        index:-1,   //滚动下标
        times:1,    //滚动次数(每一个方格为一次)
        cycle:50,   //循环滚动的次数
        speed:200,  //转动速度

    },
    methods:{
        lotteryBtn:function(){
            console.log('点击')
            this.startRoll();
            this.index = -1;
            this.speed = 200;
            this.times = 1;
        },
        roll:function(){
            var index = this.index;
            var count = this.lotteryData.length;  //总长度
            index += 1;
            if(index > count -1){
                index = 0
            }
            this.index = index
        },
        startRoll:function(){
            this.times += 1;
            this.roll();
            if(this.times > this.cycle + 20){
                clearTimeout(timer);  //清除延迟函数
                this.times = 0;
            }else{
                if(this.times < this.cycle+20){
                    this.speed -=10;    //加快转动速度
                }else if(this.times >= this.cycle){
                    this.speed += 20;
                    var ind = parseInt(Math.random()*8,0) || 0;    // 随机产生一个中奖号码
                    console.log(ind)
                    this.index = ind
                }
                var timer = setTimeout(this.startRoll,this.speed)
            }
          
        }
    },
    updated(){

    }
})
</script>

效果图;\

下面的代码是我平时做项目的时候写的代码,数据是从后台获取的,由于id的特殊性,所以中奖位置我做了一些调整,

data:{
                times: 1 ,  //转动次数
                indexs: -1 ,  //滚动下标
                cycle:50,    //基本转动次数
                speed:200,      //转动速度
}   
下面是methods中的方法

startRoll:function(){  //开始滚动
                    var that = this,
                        _this = this;
                    if (_this.on_load == 1) return;
                    that.Roll();
                    that.times += 1;  // 转动次数
                    if(that.times > that.cycle + 10){
                        clearTimeout(this.timer);      //清除定时器
                        that.times = 0 ;
                    }else{
                        if(that.times < this.cycle){
                            this.speed -=10;   //加快转动速度
                        }else if(that.times >= that.cycle ){ 
                            this.speed += 20;
                            _this.on_load = 1;
                            $.post("{{:url('lotteryProcessHandle')}}", function (res) {
                                switch (parseInt(res.code)) {
                                    case 0:
                                        that.prizes(res.data.id);   
                                        _this.user_info.luckydraw_times;
                                        if (parseInt(res.data.reward_category) == 0) layerMsg(res.msg, 5, 3000, 5, '', function(){
                                            _this.on_load = 0;
                                        });
                                        else layerMsg(res.msg, 1, 3000, 5, '', function(){
                                            _this.on_load = 0;
                                        });
                                        break;
                                    default:
                                        layerMsg(res.msg, 5, 1500, 5, '', function(){
                                            _this.on_load = 0;
                                        });
                                        break;
                                }
                            });

                            /**
                             * 重置参数
                             */
                            _this.times = 1;
                            _this.indexs = -1;
                            _this.speed = 200;
                        }
                        if(that.speed <40) that.speed = 40;
                        this.timer = setTimeout(this.startRoll,this.speed);
                    }
                },
                Roll:function(){   //滚动函数
                    var _this = this;
                    var index = _this.indexs;
                    var count = _this.lotteryItem.length;
                    index += 1;
                    if(index > count -1) index = 0;
                    _this.indexs = index;
                },
                prizes:function(id){
                    var that = this,
                        datas = that.lotteryItem;
                    if (!isEmpty(datas)) {
                        for(var i=0; i< datas.length ;i++){
                            var ind = i;
                            if(datas[ind].id == id){
                                that.indexs = i;
                                break;
                            }
                        }
                    } 
                },
            },
            mounted(){
                // this.changeType();
            },
            updated:function(){
                this.prizes()
                var _that = this
                // 抽奖按钮点击事件
                var letts =  document.getElementsByClassName('lotterydefaultBg');
                for(var i =0 ; i<letts.length; i++){
                    var index = i
                    if(index === 4){
                        letts[i].onclick = function(){
                            if (_that.on_load == 1) return;
                            _that.startRoll();
                        }
                    }
                }
                
            }

 

Vue扭蛋抽奖效果是一种基于Vue框架实现的具有互动性的抽奖效果。在这个效果中,参与者可以点击扭蛋按钮,然后蛋壳会随机转动一段时间,最终停止在一个奖品上。 实现这个效果的关键是利用Vue的动态绑定和过渡效果来实现扭蛋转动和停止的动画效果。首先,可以使用Vue的data属性来存储扭蛋的状态,例如设置一个变量isSpinning来表示是否正在旋转。当点击扭蛋按钮时,可以通过设置isSpinning为true来触发扭蛋的旋转动画。 在旋转动画方面,可以借助Vue的过渡效果来实现。可以使用transition组件来包裹扭蛋元素,并为其添加旋转动画的样式类。在data属性中添加一个变量,例如spinClass,用来控制旋转样式类的切换。在isSpinning为true时,将spinClass设置为启动旋转动画的样式类,当扭蛋停止旋转时,将spinClass设置为停止旋转的样式类。 为了使扭蛋停止在一个奖品上,可以使用Vue的计时器功能来控制扭蛋停止转动的时间。例如,在点击扭蛋按钮后,使用setTimeout函数设置一个定时器,当定时器到达指定的时间后,将isSpinning设置为false,停止扭蛋的旋转。 另外,为了实现随机停止在一个奖品上的效果,可以利用Vue的计算属性来生成随机数,并将该随机数与奖品索引绑定在一起。这样,当扭蛋停止旋转时,可以根据随机数获取对应的奖品信息,并展示在页面上。 综上所述,通过结合Vue的动态绑定、过渡效果和计时器功能,可以实现一个具有扭蛋抽奖效果的交互式页面。用户可以通过点击扭蛋按钮,享受扭蛋转动的动画效果,并在随机停止时获取奖品。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值