gui-turntable.vue 3.7 KB

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