treasureChest.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <cover-view key="treasureBox" class="treasure-box" :style="{display: showBox&&isfull?'flex':'none'}"
  3. :class="[{opening}, {fadeOut}, {dropIn},{swing}]" @click="open">
  4. <cover-image :class="opening ? 'box-img openingGif' : 'box-img boxPng'"
  5. :src="currentIdx >=0 ? opening ? boxList[currentIdx].openChestUrl : boxList[currentIdx].closeChestUrl :''"></cover-image>
  6. <cover-view class="countdown" v-if="!opening">{{count}}s</cover-view>
  7. </cover-view>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. boxList: {
  13. type: Array,
  14. default: []
  15. },
  16. isfull: {
  17. type: Boolean,
  18. default: false
  19. },
  20. onHide: {
  21. type: Boolean,
  22. default: false
  23. },
  24. },
  25. data() {
  26. return {
  27. showBox: false, // 控制宝箱显隐
  28. opening: false, // 是否正在打开
  29. fadeOut: false, // 控制自动消失的动画类
  30. dropIn: false,
  31. swing: false,
  32. count: 20, // 倒计时
  33. timer: null, // 倒计时句柄
  34. timeout: null, // 20s 未点击自动消失句柄
  35. currentIdx: -1,
  36. flagTime: 0
  37. }
  38. },
  39. watch: {
  40. onHide(val) {
  41. if (!this.showBox) return
  42. if (val) { // 暂停
  43. clearInterval(this.timer)
  44. clearTimeout(this.timeout)
  45. this.timer = null
  46. this.timeout = null
  47. } else { // 继续
  48. if (!this.opening) {
  49. const flag = this.boxList.some(item => this.flagTime >= item.time && this.flagTime <= item.time + 20);
  50. if(flag) {
  51. this.countDown()
  52. this.setAutoHide() // 重新计算剩余时长
  53. } else {
  54. this.count = 20
  55. clearInterval(this.timer)
  56. clearTimeout(this.timeout)
  57. this.timer = null
  58. this.timeout = null
  59. }
  60. }
  61. }
  62. },
  63. },
  64. methods: {
  65. close() {
  66. this.swing = false
  67. this.opening = true
  68. this.showBox = false
  69. },
  70. showTreasure(flagTime) {
  71. this.flagTime = flagTime
  72. const idx = this.boxList.findIndex(v => v.time == flagTime && v.status == 0)
  73. if (idx == -1) return
  74. this.currentIdx = idx
  75. this.showBox = true
  76. this.opening = false
  77. this.swing = false
  78. this.fadeOut = false
  79. this.dropIn = true
  80. this.count = 20
  81. clearInterval(this.timer)
  82. clearTimeout(this.timeout)
  83. this.countDown()
  84. this.setAutoHide()
  85. },
  86. /* 倒计时 */
  87. countDown() {
  88. clearInterval(this.timer) // 防止重复启动
  89. this.timer = setInterval(() => {
  90. if (this.count > 0) {
  91. this.count--
  92. } else {
  93. clearInterval(this.timer)
  94. this.timer = null
  95. }
  96. }, 1000)
  97. },
  98. /* 用户点击宝箱 */
  99. open() {
  100. if (this.opening || this.swing) return
  101. clearTimeout(this.timeout)
  102. clearInterval(this.timer)
  103. this.swing = true
  104. this.claim(1)
  105. },
  106. claimSuccess(status) {
  107. setTimeout(()=>{
  108. this.swing = false
  109. this.opening = true
  110. },400)
  111. this.boxList[this.currentIdx].status = status
  112. setTimeout(() => {
  113. this.showBox = false
  114. }, 3000)
  115. },
  116. /* 设置/重置 autoHide 定时器 */
  117. setAutoHide() {
  118. clearTimeout(this.timeout)
  119. const remainMs = this.count * 1000
  120. if (remainMs <= 0) { // 已经过期
  121. this.autoHide()
  122. return
  123. }
  124. this.timeout = setTimeout(() => this.autoHide(), remainMs)
  125. },
  126. /* 20s 未点击,自动消失 */
  127. autoHide() {
  128. clearInterval(this.timer)
  129. this.fadeOut = true
  130. if(this.isfull) {
  131. this.claim(2)
  132. } else {
  133. this.claim(0)
  134. }
  135. setTimeout(() => {
  136. this.showBox = false
  137. }, 600)
  138. },
  139. claim(status) {
  140. this.$emit('claimFun', {
  141. rewardId: this.boxList[this.currentIdx].rewardVideoRelation.rewardId,
  142. status: status,
  143. time: this.boxList[this.currentIdx].time
  144. })
  145. }
  146. },
  147. beforeDestroy() {
  148. clearInterval(this.timer)
  149. clearTimeout(this.timeout)
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. @keyframes dropIn {
  155. 0% {
  156. opacity: 0;
  157. transform: translateY(-160px);
  158. }
  159. 60% {
  160. opacity: 1;
  161. transform: translateY(12px);
  162. }
  163. 100% {
  164. opacity: 1;
  165. transform: translateY(0);
  166. }
  167. }
  168. .treasure-box.dropIn {
  169. animation: dropIn 0.7s cubic-bezier(.25, .85, .45, 1.25) forwards,
  170. pulse 1s ease-in-out infinite 0.7s;
  171. }
  172. @keyframes swing {
  173. 20% {
  174. transform: rotate(-8deg);
  175. }
  176. 40% {
  177. transform: rotate(8deg);
  178. }
  179. 60% {
  180. transform: rotate(-6deg);
  181. }
  182. 80% {
  183. transform: rotate(6deg);
  184. }
  185. 100% {
  186. transform: rotate(0deg);
  187. }
  188. }
  189. .treasure-box.swing {
  190. animation: swing 0.4s ease-in-out infinite forwards;
  191. }
  192. /* 宝箱 */
  193. .treasure-box {
  194. position: fixed;
  195. left: 100px;
  196. bottom: 80px;
  197. width: 192px;
  198. /* 128 × 1.5 */
  199. height: 138px;
  200. /* 92 × 1.5 */
  201. animation: pulse 0.4s ease-in-out infinite;
  202. // z-index: 999;
  203. display: flex;
  204. align-items: flex-end;
  205. }
  206. .openingGif {
  207. width: 192px;
  208. /* 128 × 1.5 */
  209. height: 138px;
  210. /* 92 × 1.5 */
  211. }
  212. .boxPng {
  213. width: 135px;
  214. height: 96px;
  215. margin-left: 20px;
  216. }
  217. .countdown {
  218. position: absolute;
  219. top: 50%;
  220. left: 50%;
  221. transform: translate(-50%, -50%);
  222. color: #fff;
  223. font-size: 20px;
  224. font-weight: bold;
  225. text-shadow: 0 1px 3px rgba(0, 0, 0, .6);
  226. }
  227. /* 动画 */
  228. @keyframes pulse {
  229. 0%,
  230. 100% {
  231. transform: scale(1);
  232. }
  233. 50% {
  234. transform: scale(1.15);
  235. }
  236. }
  237. /* 打开后停止 pulse */
  238. .treasure-box.opening {
  239. animation: none;
  240. }
  241. /* 自动消失 */
  242. .treasure-box.fadeOut {
  243. animation: fadeOutUp .6s ease-out forwards;
  244. }
  245. @keyframes fadeOutUp {
  246. to {
  247. opacity: 0;
  248. transform: translate(-50%, -40px);
  249. }
  250. }
  251. </style>