gui-turntable.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view
  3. class="gui-turntable"
  4. :animation="animationData">
  5. <view
  6. class="gui-turntable-item"
  7. v-for="(item, index) in rewardNames"
  8. :key="index"
  9. :style="{
  10. transform : 'rotate('+(inital - averageRotate * index)+'deg)'
  11. }">
  12. <view
  13. class="gui-turntable-inner"
  14. :style="{
  15. transform : 'translateX(-300rpx) rotate(' + averageRotate + 'deg)',
  16. background : rewardBGColors[index]
  17. }">
  18. <text
  19. class="gui-turntable-text"
  20. :style="{
  21. transform : 'translateY(120rpx) translateX('+textTrX+') rotate('+textRotate+')',
  22. fontSize : fontSize,
  23. color : rewardColors[index]
  24. }">{{item}}</text>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default{
  31. name : "gui-turntable",
  32. props : {
  33. // 奖品名称
  34. rewardNames : {
  35. type : Array,
  36. default : function(){
  37. return ["", "", "", "", "", ""];
  38. }
  39. },
  40. // 奖品展示区背景颜色
  41. rewardBGColors : {
  42. type : Array,
  43. default : function(){
  44. return [];
  45. }
  46. },
  47. // 奖品展示区文本颜色
  48. rewardColors : {
  49. type : Array,
  50. default : function(){
  51. return [];
  52. }
  53. },
  54. // 文本尺寸
  55. fontSize : {
  56. type : String,
  57. default : '32rpx'
  58. }
  59. },
  60. data() {
  61. return {
  62. // 是否正在抽奖
  63. luckyStatus : 0,
  64. // 动画对象
  65. animationData : {},
  66. tpmimgtimmer : null,
  67. // 中奖奖品 index
  68. RewardIndex : -1,
  69. // 角度
  70. averageRotate : 0,
  71. inital : 0,
  72. textTrX : '43rpx',
  73. textRotate : '-30deg'
  74. }
  75. },
  76. created:function(){
  77. this.init();
  78. },
  79. watch:{
  80. rewardNames:function(){
  81. this.init();
  82. }
  83. },
  84. methods:{
  85. init : function(){
  86. var length = this.rewardNames.length;
  87. this.averageRotate = 360 / length ;
  88. this.inital = (360 / length / 2) * -1;
  89. switch(length){
  90. case 4:
  91. this.textTrX = '0rpx';
  92. this.textRotate = '-45deg';
  93. break
  94. case 6:
  95. this.textTrX = '43rpx';
  96. this.textRotate = '55deg';
  97. break
  98. case 8:
  99. this.textTrX = '72rpx';
  100. this.textRotate = '65deg';
  101. break
  102. }
  103. var animation = null;
  104. animation = uni.createAnimation({
  105. duration: 0,
  106. timingFunction: 'ease',
  107. });
  108. animation.rotateZ(this.averageRotate/2).step();
  109. this.animationData = animation.export();
  110. },
  111. goto : function(index){
  112. if(this.luckyStatus != 0){
  113. return ;
  114. }
  115. this.RewardIndex = index;
  116. this.animationData = {};
  117. this.luckyStatus = 1;
  118. // 轮盘归零
  119. var animation = null;
  120. animation = uni.createAnimation({
  121. duration: 0,
  122. timingFunction: 'ease',
  123. });
  124. animation.rotateZ(this.averageRotate/2).step();
  125. this.animationData = animation.export();
  126. // 计算奖品角度
  127. var rewardRadiu = (360 / this.rewardNames.length);
  128. var rewardRadiuNeed = 360*6 + rewardRadiu * this.RewardIndex;
  129. setTimeout(()=>{
  130. animation = uni.createAnimation({
  131. duration:5000,
  132. timingFunction: 'ease',
  133. });
  134. animation.rotateZ(rewardRadiuNeed).step();
  135. this.animationData = animation.export();
  136. },100);
  137. setTimeout(()=>{
  138. // 停止动画
  139. this.luckyStatus = 0;
  140. this.$emit('end', index);
  141. this.RewardIndex = -1;
  142. },5000);
  143. }
  144. },
  145. emits : ['end']
  146. }
  147. </script>
  148. <style scoped>
  149. .gui-turntable{
  150. position : relative;
  151. transform-origin: center;
  152. width : 600rpx;
  153. height : 600rpx;
  154. }
  155. .gui-turntable-item {
  156. position : absolute;
  157. left : 50%;
  158. width : 300rpx;
  159. height : 600rpx;
  160. border-radius:0px 300rpx 300rpx 0;
  161. overflow : hidden;
  162. transform-origin : left center;
  163. }
  164. .gui-turntable-inner {
  165. text-align: center;
  166. width : 300rpx;
  167. height : 600rpx;
  168. transform-origin : right center;
  169. border-radius : 300rpx 0 0 300rpx;
  170. }
  171. .gui-turntable-text {
  172. display: block;
  173. transform-origin:center;
  174. }
  175. </style>