LuckDraw 转盘抽奖

基本用法

  1. <nut-luckdraw
  2. class="drawTable"
  3. ref="luckDrawPrize"
  4. :luck-width="luckWidth"
  5. :luck-height="luckheight"
  6. :prize-list="prizeList"
  7. :turns-number="turnsNumber"
  8. :turns-time="turnsTime"
  9. :prize-index="prizeIndex"
  10. :style-opt="styleOpt"
  11. @end-turns="endTurns"
  12. >
  13. <template slot="item" slot-scope="scope">
  14. <div class="drawTable-name">{{ scope.item.prizeName }}</div>
  15. <div class="drawTable-img">
  16. <img :src="scope.item.prizeImg">
  17. </div>
  18. </template>
  19. </nut-luckdraw>
  20. <div @click="startTurns" class="pointer" :style="pointerStyle"></div>
  1. export default {
  2. data() {
  3. return {
  4. // 转盘大小
  5. luckWidth: '300px',
  6. luckheight: '300px',
  7. // 转盘指针图片样式
  8. pointerStyle: {
  9. width: '80px',
  10. height: '80px',
  11. backgroundImage: 'url("https://img11.360buyimg.com/imagetools/jfs/t1/89512/11/15244/137408/5e6f15edEf57fa3ff/cb57747119b3bf89.png")',
  12. backgroundSize: 'contain',
  13. backgroundRepeat: 'no-repeat',
  14. },
  15. // 奖品列表
  16. prizeList: [
  17. {
  18. id: 'xiaomi',
  19. prizeName: '小米手机',
  20. prizeImg: 'https://img14.360buyimg.com/imagetools/jfs/t1/104165/34/15186/96522/5e6f1435E46bc0cb0/d4e878a15bfd9362.png'
  21. },
  22. {
  23. id: 'blue',
  24. prizeColor: 'rgb(251, 219, 216)',
  25. prizeName: '蓝牙耳机',
  26. prizeImg: 'https://img13.360buyimg.com/imagetools/jfs/t1/91864/11/15108/139003/5e6f146dE1c7b511d/1ddc5aa6e502060a.jpg'
  27. },
  28. {
  29. id: 'apple',
  30. prizeName: 'apple watch',
  31. prizeImg: 'https://img11.360buyimg.com/imagetools/jfs/t1/105385/19/15140/111093/5e6f1506E48bd0dfb/829a98a8cdb4c27f.png'
  32. },
  33. {
  34. id: 'fruit',
  35. prizeColor: 'rgba(246, 142, 46, 0.5)',
  36. prizeName: '迪士尼苹果',
  37. prizeImg: 'https://img11.360buyimg.com/imagetools/jfs/t1/108308/11/8890/237603/5e6f157eE489cccf1/26e0437cfd93b9c8.png'
  38. },
  39. {
  40. id: 'fish',
  41. prizeName: '海鲜套餐',
  42. prizeImg: 'https://img14.360buyimg.com/imagetools/jfs/t1/90507/38/15165/448364/5e6f15b4E5df0c718/4bd4c3d375eec312.png'
  43. },
  44. {
  45. id: 'thanks',
  46. prizeName: '谢谢参与',
  47. prizeImg: 'https://img11.360buyimg.com/imagetools/jfs/t1/96116/38/15085/5181/5e6f15d1E48e31d30/71353b61dff705d4.png'
  48. }
  49. ],
  50. turnsNumber: 5, // 转动圈数
  51. turnsTime: 5,// 转动时间:S
  52. styleOpt: {
  53. prizeBgColors: ['rgb(255, 231, 149)','rgb(255, 247, 223)','rgb(255, 231, 149)','rgb(255, 247, 223)','rgb(255, 231, 149)','rgb(255, 247, 223)'],
  54. borderColor: '#ff9800',
  55. },
  56. prizeIndex: -1, // 中奖奖品的index
  57. lock: false,// 防止多次连续点击抽奖
  58. num: 5,// 抽奖次数,根据需求定义
  59. }
  60. },
  61. methods: {
  62. startTurns() {
  63. if (!this.canBeRotated()) {
  64. return false;
  65. }
  66. this.lock = true;
  67. // 此为模拟随机数字,这里可以接受后台中奖信息
  68. const index = Math.floor(Math.random() * this.prizeList.length);
  69. // 成功后抽奖次数减1
  70. this.num--;
  71. // 中奖奖品的index
  72. this.prizeIndex = index;
  73. // 调用组件的方法,使转盘转动并停留在中奖奖品的那个扇形区域
  74. this.$refs.luckDrawPrize.rotate(index);
  75. },
  76. endTurns() {
  77. this.$dialog({
  78. content: `恭喜中奖!!!${this.prizeList[this.prizeIndex].prizeName}`,
  79. noCloseBtn: false,
  80. noOkBtn: true,
  81. cancelBtnTxt: "我知道了"
  82. });
  83. this.lock = false;
  84. },
  85. canBeRotated() {
  86. if (this.num <= 0) {
  87. this.$dialog({
  88. content: `已经没有次数了,继续加油赚积分吧!`,
  89. noCloseBtn: false,
  90. noOkBtn: true,
  91. cancelBtnTxt: "我知道了"
  92. });
  93. return false;
  94. }
  95. if (this.lock) {
  96. return false;
  97. }
  98. return true;
  99. },
  100. }
  101. }

Prop

字段说明类型默认值
ref当前转盘的类名,转动的时候根据类名执行回调函数StringluckDrawPrize
luck-width转盘的宽度String300px
luck-height转盘的高度String300px
prize-list奖品列表Array-
turns-number转动的圈数Number5
turns-time从开始转动到结束所用时间Number5(单位是秒)
style-opt转盘中的样式,包括每个扇区的背景颜色(在每条数据中页可单独设置prizeColor),扇区的边框颜色Object{prizeBgColors: [],borderColor: ‘’}
pointerStyle转盘中指针的样式,包括背景图片、大小等Object{width: ‘80px’,height: ‘80px’}

Event

字段说明回调参数
end-turns转盘中停止转动后的回调函数-

LuckDraw 转盘抽奖 - 图1