demo.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div class="luckDrawBox">
  3. <h4>基本用法</h4>
  4. <div>
  5. <nut-cell>
  6. <span slot="title">剩余抽奖次数:{{ num }}</span>
  7. </nut-cell>
  8. </div>
  9. <nut-luckdraw
  10. class="drawTable"
  11. ref="luckDrawPrize"
  12. :luckWidth="luckWidth"
  13. :luckheight="luckheight"
  14. :prizeList="prizeList"
  15. :turnsNumber="turnsNumber"
  16. :turnsTime="turnsTime"
  17. :prizeIndex="prizeIndex"
  18. :styleOpt="styleOpt"
  19. @endTurns="endTurns"
  20. >
  21. <template slot="item" slot-scope="scope">
  22. <div class="drawTable-name">{{ scope.item.prizeName }}</div>
  23. <div class="drawTable-img">
  24. <img :src="scope.item.prizeImg">
  25. </div>
  26. </template>
  27. </nut-luckdraw>
  28. <div @click="startTurns" class="pointer" :style="pointerStyle"></div>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. // 转盘大小
  36. luckWidth: '300px',
  37. luckheight: '300px',
  38. // 转盘指针图片样式
  39. pointerStyle: {
  40. width: '80px',
  41. height: '80px',
  42. backgroundImage: 'url("https://img11.360buyimg.com/imagetools/jfs/t1/106989/15/11126/137350/5e265414E8ee514bc/3456bd0d3a0454da.png")',
  43. backgroundSize: 'contain',
  44. backgroundRepeat: 'no-repeat',
  45. },
  46. // 转盘上要展示的奖品数据
  47. prizeList: [
  48. {
  49. id: 'xiaomi',
  50. prizeName: '小米手机',
  51. prizeImg: 'https://m.360buyimg.com/mobilecms/s843x843_jfs/t1/96788/40/337/73706/5dabd0e2E1f166028/7120ca2b421cb0a0.jpg!q70.dpg.webp',
  52. },
  53. {
  54. id: 'blue',
  55. prizeName: '蓝牙耳机',
  56. prizeImg: 'https://m.360buyimg.com/mobilecms/s843x843_jfs/t1/65070/13/4325/183551/5d26e23fE09ab2010/a94eaff8242e6c63.jpg!q70.dpg.webp',
  57. },
  58. {
  59. id: 'apple',
  60. prizeName: 'apple watch',
  61. prizeImg: 'https://m.360buyimg.com/mobilecms/s843x843_jfs/t1/105083/3/4010/126031/5de4aa51E1c7fefc6/0288f4cf3016e061.jpg!q70.dpg.webp',
  62. },
  63. {
  64. id: 'fruit',
  65. prizeName: '迪士尼苹果',
  66. prizeImg: 'https://m.360buyimg.com/mobilecms/s750x750_jfs/t1/47486/35/13399/356858/5da3cde2E9b3ec40f/3b3a56d54d5db565.jpg!q80.dpg.webp',
  67. },
  68. {
  69. id: 'fish',
  70. prizeName: '海鲜套餐',
  71. prizeImg: 'https://m.360buyimg.com/mobilecms/s843x843_jfs/t1/109529/24/1330/283533/5dfc836fE33d8ce6b/372adb638802710a.jpg!q70.dpg.webp',
  72. },
  73. {
  74. id: 'thanks',
  75. prizeName: '谢谢参与',
  76. prizeImg: 'https://img11.360buyimg.com/imagetools/jfs/t1/104502/28/10892/5123/5e265414Ec167392c/2831c6155895f33d.png',
  77. }
  78. ],
  79. // 转动圈数
  80. turnsNumber: 5,
  81. // 转动需要持续的时间(秒)
  82. turnsTime: 5,
  83. // 转盘样式的选项
  84. styleOpt: {
  85. // 转盘中每一块扇形的背景色,根据奖品的index来取每一块的对应颜色
  86. 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)'],
  87. // 每一个扇形的外边框颜色
  88. borderColor: '#ff9800',
  89. },
  90. // 中奖的奖品的index(此数据可根据后台返回的值重新赋值)
  91. prizeIndex: -1,
  92. // 用来锁定转盘,避免同时多次点击转动
  93. lock: false,
  94. // 剩余抽奖次数
  95. num: 5,
  96. }
  97. },
  98. methods: {
  99. startTurns() {
  100. // 如果还不可以转动
  101. if (!this.canBeRotated()) {
  102. return false;
  103. }
  104. // 开始转动
  105. // 先上锁
  106. this.lock = true;
  107. // 设置在哪里停下,应该与后台交互,这里随机抽取0~5 ,这里应该是后台返回的中奖信息,现在是测试
  108. const index = Math.floor(Math.random() * this.prizeList.length);
  109. // 成功后次数减少一次
  110. this.num--;
  111. this.prizeIndex = index;
  112. // 告诉子组件,开始转动了
  113. this.$refs.luckDrawPrize.rotate(index);
  114. },
  115. // 已经转动完转盘触发的函数
  116. endTurns() {
  117. // 提示中奖
  118. this.$dialog({
  119. content: `恭喜中奖!!!${this.prizeList[this.prizeIndex].prizeName}`,
  120. noCloseBtn: false,
  121. noOkBtn: true,
  122. cancelBtnTxt: "我知道了"
  123. });
  124. // 解锁
  125. this.lock = false;
  126. },
  127. // 判断是否可以转动
  128. canBeRotated() {
  129. if (this.num <= 0) {
  130. alert('已经没有次数了,继续加油赚积分吧!');
  131. this.$dialog({
  132. content: `已经没有次数了,继续加油赚积分吧!`,
  133. noCloseBtn: false,
  134. noOkBtn: true,
  135. cancelBtnTxt: "我知道了"
  136. });
  137. return false;
  138. }
  139. if (this.lock) {
  140. return false;
  141. }
  142. return true;
  143. },
  144. }
  145. }
  146. </script>
  147. <style lang="scss">
  148. .luckDrawBox {
  149. h3 {
  150. width: 100%;
  151. text-align: center;
  152. font-size: 24px;
  153. }
  154. // .drawTable {
  155. // position: absolute;
  156. // left: 50%;
  157. // top: 50%;
  158. // transform: translate(-50%, -50%);
  159. // }
  160. // .drawTable-name {
  161. // position: absolute;
  162. // left: 10px;
  163. // top: 20px;
  164. // width: calc(100% - 20px);
  165. // font-size: 12px;
  166. // text-align: center;
  167. // color: #ff5722;
  168. // }
  169. // .drawTable-img {
  170. // position: absolute;
  171. // /*要居中就要50% - 宽度 / 2*/
  172. // left: calc(50% - 30px / 2);
  173. // top: 60px;
  174. // width: 30px;
  175. // height: 30px;
  176. // img {
  177. // display: inline-block;
  178. // width: 100%;
  179. // height: 100%;
  180. // }
  181. // }
  182. // .pointer {
  183. // position: absolute;
  184. // // left: calc(50% - 35px);
  185. // // top: calc(50% - 40px);
  186. // left: 50%;
  187. // top: 50%;
  188. // transform: translate(-43.75%, -50%);
  189. // // background-image: url("https://img11.360buyimg.com/imagetools/jfs/t1/106989/15/11126/137350/5e265414E8ee514bc/3456bd0d3a0454da.png");
  190. // // background-size: contain;
  191. // // background-repeat: no-repeat;
  192. // }
  193. }
  194. </style>