like.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <view class="like-container">
  3. <image class="image" @click="handleLike" src="/static/images/zan1.png" ref="likeBtn"></image>
  4. <view v-for="(icon, index) in icons" :key="icon.id" class="animated-icon" :style="{
  5. top: icon.top + 'rpx',
  6. left: icon.left + 'rpx', // 补充left属性的单位
  7. fontSize: icon.size + 'rpx',
  8. color: icon.color,
  9. opacity: icon.opacity,
  10. transform: 'translateY(' + icon.y + 'rpx) 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. btnRect: null,
  71. // 可使用的图标集合
  72. iconSymbols: ['❤', '★', '🎁', '🔥', '👍', '✨', '💖', '🌟'],
  73. // 用于生成唯一ID
  74. iconId: 0
  75. };
  76. },
  77. mounted() {
  78. // 获取按钮位置信息
  79. this.getBtnRect();
  80. },
  81. methods: {
  82. /**
  83. * 获取按钮位置信息
  84. */
  85. getBtnRect() {
  86. const query = uni.createSelectorQuery().in(this);
  87. query.select('.image').boundingClientRect(data => {
  88. this.btnRect = data;
  89. }).exec();
  90. },
  91. /**
  92. * 处理点击事件
  93. */
  94. handleLike(e) {
  95. // 更新点赞状态
  96. this.isLiked = !this.isLiked;
  97. this.count += this.isLiked ? 1 : -1;
  98. this.$emit('change', {
  99. isLiked: this.isLiked,
  100. count: this.count
  101. });
  102. // 生成图标动画
  103. this.createIcons(e);
  104. },
  105. /**
  106. * 创建多个图标动画
  107. */
  108. createIcons(e) {
  109. if (!this.btnRect) {
  110. this.getBtnRect();
  111. return;
  112. }
  113. // 获取点击位置(相对于按钮)
  114. const rect = this.btnRect;
  115. const x = e.detail.x - rect.left;
  116. const y = e.detail.y - rect.top;
  117. // 生成多个图标,带轻微延迟形成连续效果
  118. for (let i = 0; i < this.iconAmount; i++) {
  119. this.createIcon(x, y, i * 40);
  120. }
  121. },
  122. /**
  123. * 创建单个图标动画
  124. * @param {Number} x 初始X坐标
  125. * @param {Number} y 初始Y坐标
  126. * @param {Number} delay 延迟时间(ms)
  127. */
  128. createIcon(x, y, delay) {
  129. // 随机属性
  130. const size = this.getRandom(...this.sizeRange);
  131. const color = this.iconColors[Math.floor(Math.random() * this.iconColors.length)];
  132. const duration = this.getRandom(...this.durationRange);
  133. const riseDistance = -this.getRandom(...this.riseRange);
  134. const floatOffset = this.getRandom(...this.floatRange);
  135. const rotate = this.getRandom(-30, 30);
  136. // 随机选择一个图标
  137. const symbol = this.iconSymbols[Math.floor(Math.random() * this.iconSymbols.length)];
  138. // 生成唯一ID
  139. const iconId = this.iconId++;
  140. // 创建图标对象
  141. const icon = {
  142. id: iconId,
  143. left: x,
  144. top: y,
  145. size,
  146. color,
  147. opacity: 1,
  148. y: 0,
  149. rotate,
  150. duration,
  151. delay,
  152. symbol
  153. };
  154. // 添加到数组
  155. this.icons.push(icon);
  156. // 触发动画
  157. setTimeout(() => {
  158. // 使用Vue的$set方法确保响应式更新
  159. this.$set(this.icons, this.icons.findIndex(item => item.id === iconId), {
  160. ...this.icons.find(item => item.id === iconId),
  161. y: riseDistance,
  162. left: x + floatOffset,
  163. opacity: 0
  164. });
  165. }, delay);
  166. // 动画结束后移除
  167. setTimeout(() => {
  168. const index = this.icons.findIndex(item => item.id === iconId);
  169. if (index !== -1) {
  170. this.icons.splice(index, 1);
  171. }
  172. }, duration + delay);
  173. },
  174. /**
  175. * 生成范围内的随机数
  176. */
  177. getRandom(min, max) {
  178. return min + Math.random() * (max - min);
  179. }
  180. }
  181. };
  182. </script>
  183. <style scoped lang="scss">
  184. .like-icon {
  185. font-size: 28rpx;
  186. color: #999;
  187. transition: all 0.3s ease;
  188. }
  189. /* 动画容器 */
  190. .like-container {
  191. position: relative;
  192. .image {
  193. width: 80rpx;
  194. height: 80rpx;
  195. }
  196. /* 动画图标样式 */
  197. .animated-icon {
  198. position: absolute;
  199. will-change: transform, opacity, left;
  200. text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2);
  201. z-index: 10;
  202. animation-timing-function: cubic-bezier(0.2, 0.8, 0.2, 1);
  203. }
  204. /* 点赞按钮动画 */
  205. @keyframes pulse {
  206. 0% {
  207. transform: scale(1);
  208. }
  209. 50% {
  210. transform: scale(1.4);
  211. }
  212. 100% {
  213. transform: scale(1);
  214. }
  215. }
  216. }
  217. </style>