vue实现大转盘抽奖功能

本文实例为大家分享了vue实现大转盘抽奖的具体代码,供大家参考,具体内容如下

效果图如下

中奖提示

代码如下

<template>
  <div class="dial" v-wechat-title="$route.meta.title">
    <div class="times">抽奖次数{{LuckyClick}}</div>
    <!-- 转盘包裹 -->
    <div class="rotate">
      <!-- 绘制圆点 -->
      <div
        :class="'circle circle_'+index"
        v-for="(item,index) in circleList"
        :key="index"
        :style="{background:index%2==0?colorCircleFirst:colorCircleSecond}"
      ></div>
      <!-- 转盘图片 -->
      <img
        class="dish"
        src="../../assets/dial.png"
        :style="{transform:rotate_deg,transition:rotate_transition}"
      />
      <!-- 指针图片 -->
      <img class="pointer" src="../../assets/click.png" @click="start" />
    </div>
  </div>
</template>
 
<script>
var light_timer; //灯光定时器
 
export default {
  name: "dial",
  data() {
    return {
      LuckyClick: 3,
      circleList: [], //原点设置
      colorCircleFirst: "#FE4D32", //圆点颜色
      colorCircleSecond: "#fff", //圆点颜色
 
      cat: 45, //总共8个扇形区域,每个区域约45度
      isAllowClick: true, //是否能够点击
      rotate_deg: 0, //指针旋转的角度
      rotate_transition: "transform 3s ease-in-out" //初始化选中的过度属性控制
    };
  },
 
  components: {
    // Calendar: Calendar
  },
  created() {
    this.showcircleList();
  },
 
  watch: {},
 
  mounted() {},
 
  methods: {
    //绘制圆点设置
    showcircleList() {
      let circleList = [];
      for (var i = 0; i < 16; i++) {
        circleList.push(i);
      }
      this.circleList = circleList;
      this.light();
    },
 
    //闪动效果
    light: function() {
      var that = this;
      clearInterval(light_timer);
      light_timer = setInterval(function() {
        if (that.colorCircleFirst == "#FE4D32") {
          that.colorCircleFirst = "#fff";
          that.colorCircleSecond = "#FE4D32";
        } else {
          that.colorCircleFirst = "#FE4D32";
          that.colorCircleSecond = "#fff";
        }
      }, 300); //设置圆点闪烁的效果
    },
 
    start() {
      if (this.LuckyClick == 0) {
        alert("机会已经用完了");
        return;
      }
      this.rotating();
    },
 
    rotating() {
      if (!this.isAllowClick) return;
      this.isAllowClick = false;
      this.rotate_transition = "transform 3s ease-in-out";
      this.LuckyClick--;
      var rand_circle = 5; //默认多旋转5圈
      //   var winningIndex = Math.floor(Math.random() * 8); //获奖的下标   0-7   没有概率每个平均
      var winningIndex = this.set(); //设置了概率的
 
      console.log(winningIndex);
      var randomDeg = 360 - winningIndex * 45; //当前下标对应的角度    45是总共8个扇形区域,每个区域约45度
 
      var deg = rand_circle * 360 + randomDeg; //将要旋转的度数  由于是顺时针的转动方向需要用360度来减
      this.rotate_deg = "rotate(" + deg + "deg)";
 
      var that = this;
      setTimeout(function() {
        that.isAllowClick = true;
        that.rotate_deg = "rotate(" + 0 + "deg)"; //定时器关闭的时候重置角度
        that.rotate_transition = "";
 
        if (winningIndex == 0) {
          alert("恭喜您,IphoneX");
        } else if (winningIndex == 1) {
          alert("恭喜您,获得10元现金");
        } else if (winningIndex == 2) {
          alert("很遗憾,重在参与");
        } else if (winningIndex == 3) {
          alert("恭喜您,获得30元现金");
        } else if (winningIndex == 4) {
          alert("恭喜您,获得20元现金");
        } else if (winningIndex == 5) {
          alert("恭喜您,获得50元现金");
        } else if (winningIndex == 6) {
          alert("恭喜您,获得5元现金");
        } else if (winningIndex == 7) {
          alert("恭喜您,获得100元现金");
        }
      }, 3500);
    },
 
    //设置概率
    set() {
      var winIndex;
    //方法1
    //   if (Math.floor(Math.random() * 100) < 30) {
    //     console.log("30%的概率,重在参与");
    //     winIndex = 2;
    //   } else if (Math.floor(Math.random() * 100) < 55) {
    //     console.log("25%的概率,5元");
    //     winIndex = 6;
    //   } else if (Math.floor(Math.random() * 100) < 75) {
    //     console.log("20%的概率,10元");
    //     winIndex = 1;
    //   } else if (Math.floor(Math.random() * 100) < 85) {
    //     console.log("10%的概率,20元");
    //     winIndex = 4;
    //   } else if (Math.floor(Math.random() * 100) < 92) {
    //     console.log("7%的概率,30元");
    //     winIndex = 3;
    //   } else if (Math.floor(Math.random() * 100) < 97) {
    //     console.log("5%的概率,50元");
    //     winIndex = 5;
    //   } else if (Math.floor(Math.random() * 100) < 99) {
    //     console.log("2%的概率,100元");
    //     winIndex = 7;
    //   } else if (Math.floor(Math.random() * 100) == 99) {
    //     console.log("1%的概率,IphoneX");
    //     winIndex = 0;
    //   }
      
 
      //方法2
      var __rand__ = Math.random();
      if (__rand__ < 0.3) winIndex = 2;
      else if (__rand__ < 0.55) winIndex = 6;
      else if (__rand__ < 0.75) winIndex = 1;
      else if (__rand__ < 0.85) winIndex = 4;
      else if (__rand__ < 0.92) winIndex = 3;
      else if (__rand__ < 0.97) winIndex = 5;
      else if (__rand__ < 0.99) winIndex = 7;
      else if (__rand__ = 0.99) winIndex = 0;
 
      return winIndex;
    }
  },
 
  computed: {}
};
</script>
 
 
<style  scoped lang="scss">
@import "../../common/common";
.times {
  text-align: center;
  line-height: 0.8rem;
  background: #fff;
}
.rotate {
  width: 6.1rem;
  height: 6.1rem;
  background: #ffbe04;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 48%;
  left: 50%;
  transform: translate(-50%, -50%);
}
 
.rotate .dish {
  width: 5.5rem;
  height: 5.5rem;
}
 
.pointer {
  width: 1.39rem;
  height: 2.03rem;
  position: absolute;
  top: 46%;
  left: 50%;
  transform: translate(-50%, -50%);
}
 
/* 圆点 */
.rotate .circle {
  position: absolute;
  display: block;
  border-radius: 50%;
  height: 0.16rem;
  width: 0.16rem;
  background: black;
}
 
.rotate .circle_0 {
  top: 0.08rem;
  left: 2.92rem;
}
 
.rotate .circle_1 {
  top: 0.28rem;
  left: 4.05rem;
}
 
.rotate .circle_2 {
  top: 0.86rem;
  left: 4.95rem;
}
 
.rotate .circle_3 {
  top: 1.85rem;
  left: 5.65rem;
}
 
.rotate .circle_4 {
  top: 2.85rem;
  right: 0.1rem;
}
 
.rotate .circle_5 {
  bottom: 1.89rem;
  right: 0.29rem;
}
 
.rotate .circle_6 {
  bottom: 0.96rem;
  right: 0.88rem;
}
 
.rotate .circle_7 {
  bottom: 0.34rem;
  right: 1.76rem;
}
 
.rotate .circle_8 {
  bottom: 0.06rem;
  right: 3.06rem;
}
 
.rotate .circle_9 {
  bottom: 0.28rem;
  left: 1.9rem;
}
 
.rotate .circle_10 {
  bottom: 0.96rem;
  left: 0.88rem;
}
 
.rotate .circle_11 {
  bottom: 1.82rem;
  left: 0.28rem;
}
 
.rotate .circle_12 {
  top: 2.9rem;
  left: 0.1rem;
}
 
.rotate .circle_13 {
  top: 1.9rem;
  left: 0.28rem;
}
 
.rotate .circle_14 {
  top: 1rem;
  left: 0.86rem;
}
 
.rotate .circle_15 {
  top: 0.32rem;
  left: 1.76rem;
}
</style>

本文中用到的图片

大转盘图片如下

指针的图片如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • vue简单实现转盘抽奖

    本文实例为大家分享了vue简单实现转盘抽奖的具体代码,供大家参考,具体内容如下 1.0 思路整理 转盘抽奖很常见,之前也没写过,现在有空来写写,分析如下: 1.1 转盘旋转 ----- 可以用 transform 的 rotate 来解决 1.2 旋转动画 ----- transition 过渡来处理 1.3 停留目标位置及中奖提示 ? ------ 通过控制旋转角度控制停留位置,中奖提示,考虑添加回调 1.1 开始行动 上面的思考,我们知道了大概要实现的步骤,首先我们搞张图片 这个圆盘有 10

  • vue实现宫格轮转抽奖

    vue实现宫格轮转抽奖(类似穿越火线的xx轮回),供大家参考,具体内容如下 不做过多的解说,直接上代码啦.关键的代码都写了注释,很容易理解.直接复制即可使用! 另外css部分依赖 node-sass.sass-loader,没有安装的安装一下,已有的小伙伴直接跳过~~ "node-sass": "^4.12.0", "sass-loader": "^8.0.2", <template> <div class=

  • vue组件实现移动端九宫格转盘抽奖

    本文实例为大家分享了vue组件实现移动端九宫格转盘抽奖的具体代码,供大家参考,具体内容如下 vue-lucky-draw 移动端九宫格转盘抽奖vue组件,中奖的奖品数据由接口决定. 效果图 抽奖 因为中奖的结果是后台返回的,所以要考虑转盘的最终停下来的位置必须是在对应后台返回结果的奖品的位置,也就是要模拟出这个中奖的过程(所以所谓的抽奖都是骗人的,嘿嘿).先要写出移动的背景框对应的各个位置的css,然后动态切换class来使其呈现出转动效果.明白了过程就好写了. 中间的按钮用的是css3的ani

  • 基于VUE实现的九宫格抽奖功能

    先给大家展示下效果图: HTML代码: <template> <div class="luckDraw"> <title-bar :title="title"></title-bar> <div class="container"> <div class="turntable-wrapper"> <div class="luck-wrapp

  • VUE实现大转盘抽奖

    UI 老规矩,先看下静态UI,以便于有个图像概念 初始参考各值参考图 方案分析-参数配置 核心思路: 将指针和中奖区域划分两部分,目前常规的效果,控制中奖区域旋转,然后停在指针处,当然控制指针也可以,一套思路,dom结构也比较简单,唯一算是复杂点的就是中奖区域,但是如果你足够懒,像我一样,你可以传递一张图也可以,完全依赖远端数据: 关于旋转位置 每个移动位置应均分,360/个数 === 每个奖品所占据的位置,以本文为例8个奖品位置,每个区域应为45deg,每个指针中心位置应为±22.5deg(±

  • Vue组件实现数字滚动抽奖效果

    本文实例为大家分享了Vue组件实现数字滚动抽奖效果的具体代码,供大家参考,具体内容如下 可调整数字滚动速度,可指定开奖延迟时间,可指定开奖数字,按个人需求自行改动(录了个效果供参考,临时找的录屏,表介意)不一一精简了 组件代码 <template>     <div class="info-span04" style="color: #333333;">         中奖号码:         <div style="ve

  • Vue.js实现大转盘抽奖总结及实现思路

    大家好!先上图看看本次案例的整体效果. 实现思路: Vue component实现大转盘组件,可以嵌套到任意要使用的页面. css3 transform控制大转盘抽奖过程的动画效果. 抽奖组件内使用钩子函数watch监听抽奖结果的返回情况播放大转盘动画并给用户弹出中奖提示. 中奖结果弹窗,为抽奖组件服务. 实现步骤如下: 构建api奖品配置信息和抽奖接口,vuex全局存放奖品配置和中奖结果数据信息. api: export default { getPrizeList () { let priz

  • vue实现手机号码抽奖上下滚动动画示例

    本文介绍了vue实现手机号码抽奖上下滚动动画示例,分享给大家.具体如下: <!DOCTYPE> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>Document</title> <meta name="viewport" content=

  • vue实现大转盘抽奖功能

    本文实例为大家分享了vue实现大转盘抽奖的具体代码,供大家参考,具体内容如下 效果图如下 中奖提示 代码如下 <template>   <div class="dial" v-wechat-title="$route.meta.title">     <div class="times">抽奖次数{{LuckyClick}}</div>     <!-- 转盘包裹 -->     <

  • vue实现简单转盘抽奖功能

    本文实例为大家分享了vue实现简单转盘抽奖的具体代码,供大家参考,具体内容如下 样式请大家忽略(自己调),主要看JS代码实现,点击按钮后调用start方法,判断是否在转动状态,如果没转动则调用go方法,go方法主要封装了一次性定时器,是个递归函数,调用go函数后即可实现抽奖转盘的效果了,详细代码如下: 注释清晰哦 <template>   <div class="home">     <button @click="start">

  • 使用jQuery Rotare实现微信大转盘抽奖功能

    很多公司到了年底都会做一些抽奖活动来刺激.吸引.粘住客户,比如抽奖转盘活动. 前几天用一个jqueryRotate插件实现了转盘的效果.比起那些很炫丽的flash是稍逊点,但也基本实现了需求 效果图: 实现这个其实蛮简单的,转动的效果用的jqueryRotate插件,所以只要判断每个奖荐对应的角度,然后设置指针的转动角度就可以了.比如关键的是jqueryRotate这个插件的用法. jqueryRotate的资料: 支持Internet Explorer 6.0+ .Firefox 2.0 .S

  • Vue 幸运大转盘实现思路详解

    转盘抽奖主要有两种,指针转动和转盘转动,个人觉得转盘转动比较好看点,指针转动看着头晕,转盘转动时指针是在转盘的中间位置,这里要用到css的transform属性和transition属性,这两个因为不常用最好是上网查查,用法和功能.   在html部分 <div id="wheel_surf"> <div class="wheel_surf_title">幸运大转盘</div> <div class="lucky-

  • jQuery+PHP实现微信转盘抽奖功能的方法

    本文实例讲述了jQuery+PHP实现微信转盘抽奖功能的方法.分享给大家供大家参考,具体如下: 本文结合实例将使用jQuery和PHP来实现转盘抽奖程序. 准备工作 首先要准备素材,抽奖的界面用到两张图片,圆盘图片和指针图片,实际应用中可以根据不同的需求制作不同的圆盘图片. 接着制作html页面,实例中我们在body中加入如下代码: <div class="demo"> <div id="disk"></div> <div

  • jquery转盘抽奖功能实现

     一.用到的素材 二.代码如下,重点是js部分 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jQuery九宫格大转盘抽奖</title> <style> #lottery{width:570px;height:510

随机推荐