tm-choujiangGrid.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view class="tm-choujiangGrid">
  3. <view class="tm-choujiangGrid-bg"
  4. :style="{
  5. backgroundImage: `url(${them_data.bg})`
  6. }"
  7. >
  8. <view v-if="listData.length==8" class="tm-choujiangGrid-con">
  9. <view v-for="(item,index) in 3" :key="index" class="tm-choujiangGrid-grid">
  10. <block v-if="index==0">
  11. <view class="tm-choujiangGrid-item">
  12. <view class="tm-choujiangGrid-item-img flex-center">
  13. <tm-images :width="100" :src="listData[0].img"></tm-images>
  14. </view>
  15. <view :class="{on:(aniOff==false&&active==0)||(choujiangJieGuo&&aniOff&&active==0)}" class="tm-choujiangGrid-item-hover"></view>
  16. </view>
  17. <view class="tm-choujiangGrid-item">
  18. <view class="tm-choujiangGrid-item-img flex-center">
  19. <tm-images :width="100" :src="listData[1].img"></tm-images>
  20. </view>
  21. <view :class="{on:(aniOff==false&&active==1)||(choujiangJieGuo&&aniOff&&active==1)}" class="tm-choujiangGrid-item-hover"></view>
  22. </view>
  23. <view class="tm-choujiangGrid-item">
  24. <view class="tm-choujiangGrid-item-img flex-center">
  25. <tm-images :width="100" :src="listData[2].img"></tm-images>
  26. </view>
  27. <view :class="{on:(aniOff==false&&active==2)||(choujiangJieGuo&&aniOff&&active==2)}" class="tm-choujiangGrid-item-hover"></view>
  28. </view>
  29. </block>
  30. <block v-if="index==1">
  31. <view class="tm-choujiangGrid-item">
  32. <view class="tm-choujiangGrid-item-img flex-center">
  33. <tm-images :width="100" :src="listData[7].img"></tm-images>
  34. </view>
  35. <view :class="{on:(aniOff==false&&active==7)||(choujiangJieGuo&&aniOff&&active==7)}" class="tm-choujiangGrid-item-hover"></view>
  36. </view>
  37. <view @click="clickImgPlay" class="tm-choujiangGrid-item">
  38. <view class="tm-choujiangGrid-item-img flex-center tm-choujiangGrid-item-point">
  39. <tm-images :width="140" :src="them_data.point"></tm-images>
  40. </view>
  41. <view class="tm-choujiangGrid-item-hover"></view>
  42. </view>
  43. <view class="tm-choujiangGrid-item">
  44. <view class="tm-choujiangGrid-item-img flex-center">
  45. <tm-images :width="100" :src="listData[3].img"></tm-images>
  46. </view>
  47. <view :class="{on:(aniOff==false&&active==3)||(choujiangJieGuo&&aniOff&&active==3)}" class="tm-choujiangGrid-item-hover"></view>
  48. </view>
  49. </block>
  50. <block v-if="index==2">
  51. <view class="tm-choujiangGrid-item">
  52. <view class="tm-choujiangGrid-item-img flex-center">
  53. <tm-images :width="100" :src="listData[6].img"></tm-images>
  54. </view>
  55. <view :class="{on:(aniOff==false&&active==6)||(choujiangJieGuo&&aniOff&&active==6)}" class="tm-choujiangGrid-item-hover"></view>
  56. </view>
  57. <view class="tm-choujiangGrid-item">
  58. <view class="tm-choujiangGrid-item-img flex-center">
  59. <tm-images :width="100" :src="listData[5].img"></tm-images>
  60. </view>
  61. <view :class="{on:(aniOff==false&&active==5)||(choujiangJieGuo&&aniOff&&active==5)}" class="tm-choujiangGrid-item-hover"></view>
  62. </view>
  63. <view class="tm-choujiangGrid-item">
  64. <view class="tm-choujiangGrid-item-img flex-center">
  65. <tm-images :previmage="false" :width="100" :src="listData[4].img"></tm-images>
  66. </view>
  67. <view :class="{on:(aniOff==false&&active==4)||(choujiangJieGuo&&aniOff&&active==4)}" class="tm-choujiangGrid-item-hover"></view>
  68. </view>
  69. </block>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. /**
  77. * 转盘抽奖
  78. * @property {Number} theme-index = [] 默认0,主题索引,我已内置两套皮肤。
  79. * @property {Array} theme-list = [] 默认[已内置两套],主题列表数据,已有两套。
  80. * @property {Boolean} disabled = [] 默认 false,是否禁用
  81. * @property {Number} duration = [] 默认 3000,动画时长
  82. * @property {Boolean} disabledCenter = [] 默认 false,是否禁用点中间图片开始。
  83. * @property {Number} turns = [] 默认 2,转的圈数
  84. * @property {Number} specify = [] 默认 null,为整数时,即可指定奖品,始终中奖保持在这个位置。
  85. * @property {Array} list = [] 默认 [测试的数据],奖品数据。
  86. * @property {Function} start 游戏开始时触发。
  87. * @property {Function} end 游戏结束触发,并返回奖品数据。
  88. * @example <tm-choujiangGame ></tm-choujiangGame>
  89. */
  90. import tmImages from "@/tm-vuetify/components/tm-images/tm-images.vue"
  91. export default {
  92. components:{tmImages},
  93. name: 'tm-choujiangGrid',
  94. props: {
  95. themeIndex: {
  96. type: Number,
  97. default: 0
  98. },
  99. themeList: {
  100. type: Array,
  101. default: () => {
  102. return [
  103. {
  104. bg: 'https://jx2d.cn/yuwuimages/choujiangGrid_1.png?k=9',
  105. point: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_0.png'
  106. },
  107. {
  108. bg: 'https://jx2d.cn/yuwuimages/choujiangGrid_2.png?k=9',
  109. point: 'https://jx2d.cn/yuwuimages/choujiangGrid_2_0.png'
  110. }
  111. ];
  112. }
  113. },
  114. disabled: {
  115. type: Boolean,
  116. default: false
  117. },
  118. //是否禁用点中间图片开始。
  119. disabledCenter:{
  120. type: Boolean,
  121. default: false
  122. },
  123. // 动画时长
  124. duration: {
  125. type: Number,
  126. default: 100
  127. },
  128. list: {
  129. type: Array,
  130. default: () => {
  131. return [
  132. { name: '1', gailv: 30, id: 1, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_1.png' },
  133. { name: '2', gailv: 10, id: 2, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_2.png' },
  134. { name: '3', gailv: 10, id: 3, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_1.png' },
  135. { name: '4', gailv: 10, id: 4, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_2.png' },
  136. { name: '5', gailv: 10, id: 5, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_2.png' },
  137. { name: '6', gailv: 10, id: 7, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_1.png' },
  138. { name: '7', gailv: 10, id: 8, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_1.png' },
  139. { name: '8', gailv: 10, id: 9, img: 'https://jx2d.cn/yuwuimages/choujiangGrid_1_2.png' }
  140. ];
  141. }
  142. },
  143. // 转的圈数。
  144. turns:{
  145. type:Number,
  146. default:2
  147. },
  148. //指定中奖的奖品数组索引,从0开始
  149. specify:{
  150. type:Number,
  151. default:null
  152. }
  153. },
  154. data() {
  155. return {
  156. aniOff: true,
  157. active:0,
  158. choujiangJieGuo:null,
  159. listData: [],
  160. timeid:8564
  161. };
  162. },
  163. mounted() {
  164. this.listData = this.list;
  165. },
  166. destroyed() {
  167. clearTimeout(this.timeid)
  168. },
  169. computed: {
  170. them_index: function() {
  171. return this.themeIndex;
  172. },
  173. them_data: function() {
  174. return this.themeList[this.them_index];
  175. }
  176. },
  177. methods: {
  178. clickImgPlay(){
  179. if (this.disabledCenter) return;
  180. this.play();
  181. },
  182. chuliganlv() {
  183. var ml = new this.$tm.choujiang(this.listData);
  184. let zhongle = [];
  185. let xh = ml.getResult();
  186. let index = 0
  187. for(let j=0;j<this.listData.length;j++){
  188. if(this.listData[j].id === xh.id){
  189. index = j;
  190. break;
  191. }
  192. }
  193. let cisu = (this.listData.length-1)*this.turns+this.turns*2 + (this.specify!==null?this.specify:index);
  194. zhongle = {
  195. index:index,
  196. data:xh,
  197. playNum:cisu
  198. }
  199. return zhongle;
  200. },
  201. play() {
  202. let t = this;
  203. if (this.disabled) return;
  204. if (!this.aniOff) {
  205. console.warn('未结束动画,不能连续点击.');
  206. return;
  207. }
  208. // 开始动画.
  209. this.$emit('start');
  210. this.choujiangJieGuo = null;
  211. let zhongjiang = this.chuliganlv();
  212. this.$nextTick(function() {
  213. this.aniOff = false;
  214. let duration = this.duration;
  215. let playcCisu = 0; //总次数的序列。
  216. // 循环的内部索引.
  217. let nbIndex = 0;
  218. function aniOpen(){
  219. if(nbIndex>t.listData.length-1){
  220. nbIndex = 0;
  221. t.active = nbIndex;
  222. }else{
  223. t.active = nbIndex;
  224. nbIndex += 1;
  225. }
  226. clearTimeout(t.timeid);
  227. t.timeid = setTimeout(function() {
  228. playcCisu +=1;
  229. if(playcCisu > zhongjiang.playNum){
  230. t.$emit('end',{index:zhongjiang.index,data:zhongjiang.data})
  231. t.choujiangJieGuo = zhongjiang;
  232. t.aniOff = true;
  233. }else{
  234. aniOpen();
  235. }
  236. }, duration);
  237. }
  238. aniOpen();
  239. });
  240. },
  241. }
  242. };
  243. </script>
  244. <style lang="less" scoped>
  245. .tm-choujiangGrid {
  246. overflow: hidden;
  247. .tm-choujiangGrid-bg{
  248. width:600upx;
  249. height: 600upx;
  250. position: relative;
  251. margin: auto;
  252. background-repeat: no-repeat;
  253. background-size: cover;
  254. .tm-choujiangGrid-con{
  255. position: absolute;
  256. width: 450upx;
  257. height: 450upx;
  258. top: 75upx;
  259. left: 75upx;
  260. .tm-choujiangGrid-grid{
  261. display: flex;
  262. justify-content: space-between;
  263. .tm-choujiangGrid-item{
  264. width: 137upx;
  265. height: 130upx;
  266. border-radius: 20upx;
  267. overflow: hidden;
  268. margin-left: 10upx;
  269. position: relative;
  270. margin-bottom: 28upx;
  271. .tm-choujiangGrid-item-img{
  272. width: 100%;
  273. height: 100%;
  274. color: black;
  275. &.tm-choujiangGrid-item-point{
  276. animation: scalanit 1s linear infinite;
  277. }
  278. }
  279. .tm-choujiangGrid-item-hover{
  280. width: 100%;
  281. height: 100%;
  282. position: absolute;
  283. top: 0;
  284. left: 0;
  285. background-color: #ffaa17;
  286. opacity: 0;
  287. &.on{
  288. opacity: 0.6;
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. @keyframes scalanit {
  297. 0%{
  298. transform: scale(0.9);
  299. }
  300. 50%{
  301. transform: scale(1.1);
  302. }
  303. 100%{
  304. transform: scale(0.9);
  305. }
  306. }
  307. </style>