like.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="like-container">
  3. <image class="image" @click="handleLike" src="/static/images/live/zan1.png" ref="likeBtn"></image>
  4. <view v-for="(icon, index) in icons" :key="icon.id" class="animated-icon" :style="{
  5. top: icon.top + 'px',
  6. left: icon.left + 'px',
  7. fontSize: icon.size + 'px',
  8. color: icon.color,
  9. opacity: icon.opacity,
  10. transform: 'translate(-50%, calc(-50% + ' + icon.y + 'px)) rotate(' + icon.rotate + 'deg)',
  11. transition: 'all ' + icon.duration + 'ms ' + icon.delay + 'ms ease-out'
  12. }">
  13. {{ icon.symbol }}
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'DouyinLike',
  20. props: {
  21. // 初始点赞数
  22. initialCount: {
  23. type: Number,
  24. default: 0
  25. },
  26. // 单次点击生成的图标数量
  27. iconAmount: {
  28. type: Number,
  29. default: 6
  30. },
  31. // 图标颜色集合
  32. iconColors: {
  33. type: Array,
  34. default: () => [
  35. '#ff798b', '#ffc54f', '#ffaefb',
  36. '#87f37f', '#6eb9ff', '#6d86d6',
  37. '#bb7df5', '#eeffc5'
  38. ]
  39. },
  40. // 图标大小范围
  41. sizeRange: {
  42. type: Array,
  43. default: () => [40, 78]
  44. },
  45. // 动画持续时间范围(ms)
  46. durationRange: {
  47. type: Array,
  48. default: () => [700, 1300]
  49. },
  50. // 上升距离范围(px)
  51. riseRange: {
  52. type: Array,
  53. default: () => [200, 420]
  54. },
  55. // 左右飘动范围(px)
  56. floatRange: {
  57. type: Array,
  58. default: () => [-50, 50]
  59. }
  60. },
  61. data() {
  62. return {
  63. // 当前点赞数
  64. count: this.initialCount,
  65. // 是否已点赞
  66. isLiked: false,
  67. // 动画图标数组
  68. icons: [],
  69. // 可使用的图标集合
  70. iconSymbols: ['❤', '★', '🎁', '🔥', '👍', '✨', '💖', '🌟'],
  71. // 用于生成唯一ID
  72. iconId: 0
  73. };
  74. },
  75. methods: {
  76. /**
  77. * 处理点击事件
  78. */
  79. handleLike() {
  80. // 更新点赞状态
  81. this.isLiked = !this.isLiked;
  82. this.count += this.isLiked ? 1 : -1;
  83. this.$emit('change', {
  84. isLiked: this.isLiked,
  85. count: this.count
  86. });
  87. // 生成图标动画
  88. this.createIcons();
  89. },
  90. /**
  91. * 创建多个图标动画(以点赞按钮中心为起点,避免真机触摸坐标偏差)
  92. */
  93. createIcons() {
  94. const query = uni.createSelectorQuery().in(this);
  95. query.select('.like-container').boundingClientRect();
  96. query.select('.image').boundingClientRect();
  97. query.exec((res) => {
  98. const containerRect = res[0];
  99. const imageRect = res[1];
  100. if (!containerRect || !imageRect) return;
  101. const x = imageRect.left - containerRect.left + imageRect.width / 2;
  102. const y = imageRect.top - containerRect.top + imageRect.height / 2;
  103. for (let i = 0; i < this.iconAmount; i++) {
  104. this.createIcon(x, y, i * 40);
  105. }
  106. });
  107. },
  108. /**
  109. * 创建单个图标动画
  110. * @param {Number} x 初始X坐标
  111. * @param {Number} y 初始Y坐标
  112. * @param {Number} delay 延迟时间(ms)
  113. */
  114. createIcon(x, y, delay) {
  115. // 随机属性(sizeRange 为 rpx,转为 px 与定位单位一致)
  116. const pxRatio = uni.getSystemInfoSync().windowWidth / 750;
  117. const size = this.getRandom(...this.sizeRange) * pxRatio;
  118. const color = this.iconColors[Math.floor(Math.random() * this.iconColors.length)];
  119. const duration = this.getRandom(...this.durationRange);
  120. const riseDistance = -this.getRandom(...this.riseRange);
  121. const floatOffset = this.getRandom(...this.floatRange);
  122. const rotate = this.getRandom(-30, 30);
  123. // 随机选择一个图标
  124. const symbol = this.iconSymbols[Math.floor(Math.random() * this.iconSymbols.length)];
  125. // 生成唯一ID
  126. const iconId = this.iconId++;
  127. // 创建图标对象
  128. const icon = {
  129. id: iconId,
  130. left: x,
  131. top: y,
  132. size,
  133. color,
  134. opacity: 1,
  135. y: 0,
  136. rotate,
  137. duration,
  138. delay,
  139. symbol
  140. };
  141. // 添加到数组
  142. this.icons.push(icon);
  143. // 触发动画
  144. setTimeout(() => {
  145. // 使用Vue的$set方法确保响应式更新
  146. this.$set(this.icons, this.icons.findIndex(item => item.id === iconId), {
  147. ...this.icons.find(item => item.id === iconId),
  148. y: riseDistance,
  149. left: x + floatOffset,
  150. opacity: 0
  151. });
  152. }, delay);
  153. // 动画结束后移除
  154. setTimeout(() => {
  155. const index = this.icons.findIndex(item => item.id === iconId);
  156. if (index !== -1) {
  157. this.icons.splice(index, 1);
  158. }
  159. }, duration + delay);
  160. },
  161. /**
  162. * 生成范围内的随机数
  163. */
  164. getRandom(min, max) {
  165. return min + Math.random() * (max - min);
  166. }
  167. }
  168. };
  169. </script>
  170. <style scoped lang="scss">
  171. .like-icon {
  172. font-size: 28rpx;
  173. color: #999;
  174. transition: all 0.3s ease;
  175. }
  176. /* 动画容器 */
  177. .like-container {
  178. position: relative;
  179. .image {
  180. width: 80rpx;
  181. height: 80rpx;
  182. }
  183. /* 动画图标样式 */
  184. .animated-icon {
  185. position: absolute;
  186. will-change: transform, opacity, left;
  187. text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2);
  188. z-index: 10;
  189. animation-timing-function: cubic-bezier(0.2, 0.8, 0.2, 1);
  190. }
  191. /* 点赞按钮动画 */
  192. @keyframes pulse {
  193. 0% {
  194. transform: scale(1);
  195. }
  196. 50% {
  197. transform: scale(1.4);
  198. }
  199. 100% {
  200. transform: scale(1);
  201. }
  202. }
  203. }
  204. </style>