help-choose.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <view class="flex-common-css iPhone-bottom box-border-box">
  3. <Nav :title="school_name"></Nav>
  4. <image class="bg-img" src="/pages-takeout/static/bg.png" mode=""></image>
  5. <view class="q-box z-2">
  6. <view class="qp-box" v-for="(item,index) in shopData">
  7. <image class="qp-img" src="/pages-takeout/static/qp.png" mode=""></image>
  8. </view>
  9. </view>
  10. <view class="but-box">
  11. <image class="but-img" src="/pages-takeout/static/but-bg.png" mode=""></image>
  12. <view class="fs-38 fc-f fw-b text-align-center z-2 pt-49">
  13. 君子帮你选,今日吃点啥?
  14. </view>
  15. </view>
  16. <view class="fs-28 fc-1089D1 text-align-center">
  17. 全部商家随机抽取
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. import {
  23. shopAll
  24. } from "@/api/shop.js"
  25. export default {
  26. components: {},
  27. data() {
  28. return {
  29. school_name: '',
  30. shopId: 0,
  31. shopData: [],
  32. animationFrames: [], // 存储每个小球的动画帧信息
  33. };
  34. },
  35. onLoad(data) {
  36. this.school_name = uni.getStorageSync('school_name');
  37. this.shopAll()
  38. },
  39. mounted() {},
  40. methods: {
  41. scrolltolower() {
  42. console.log('触底');
  43. },
  44. initBalls() {
  45. const numBalls = 10; // 小球数量
  46. for (let i = 0; i < numBalls; i++) {
  47. this.shopData.push({
  48. colorClass: this.getRandomColorClass(), // 随机颜色类
  49. content: `Ball ${i + 1}`, // 自定义内容
  50. style: {
  51. position: 'absolute',
  52. left: `${Math.random() * window.innerWidth}px`, // 随机初始位置X
  53. top: `${Math.random() * window.innerHeight}px`, // 随机初始位置Y
  54. transform: `translate(-50%, -50%)`, // 居中内容
  55. },
  56. direction: { x: Math.random() * 2 - 1, y: Math.random() * 2 - 1 }, // 随机方向
  57. speed: Math.random() * 2 + 1, // 随机速度
  58. });
  59. }
  60. },
  61. getRandomColorClass() {
  62. // 根据需要返回随机颜色类名,这里简化为几个预设类
  63. const colors = ['red', 'blue', 'green', 'yellow', 'purple', 'orange'];
  64. return colors[Math.floor(Math.random() * colors.length)];
  65. },
  66. startAnimations() {
  67. // 为每个小球启动动画
  68. this.shopData.forEach((ball, index) => {
  69. setTimeout(() => {
  70. this.animateBall(index);
  71. }, Math.random() * 2000); // 随机延迟开始动画
  72. });
  73. },
  74. animateBall(index) {
  75. const ball = this.shopData[index];
  76. let newLeft = parseFloat(ball.style.left) + ball.direction.x * ball.speed;
  77. let newTop = parseFloat(ball.style.top) + ball.direction.y * ball.speed;
  78. // 碰撞检测(边界反弹)
  79. if (newLeft < 0 || newLeft > window.innerWidth - 118) { // 50是假设的小球宽度
  80. ball.direction.x = -ball.direction.x;
  81. newLeft += ball.direction.x * ball.speed * 2; // 反弹后调整位置,避免抖动
  82. }
  83. if (newTop < 0 || newTop > window.innerHeight - 118) { // 50是假设的小球高度
  84. ball.direction.y = -ball.direction.y;
  85. newTop += ball.direction.y * ball.speed * 2; // 反弹后调整位置,避免抖动
  86. }
  87. ball.style.left = `${newLeft}px`;
  88. ball.style.top = `${newTop}px`;
  89. // 使用requestAnimationFrame进行持续动画
  90. this.animationFrames[index] = requestAnimationFrame(() => {
  91. this.animateBall(index);
  92. });
  93. },
  94. shopAll() {
  95. shopAll().then((res) => {
  96. if (res.code === 1) {
  97. this.shopData = res.data
  98. }
  99. })
  100. }
  101. },
  102. beforeDestroy() {
  103. // 组件销毁前取消所有动画帧请求
  104. this.animationFrames.forEach(cancelAnimationFrame);
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .qp-box-ccc {
  110. background: #ccc;
  111. }
  112. .z-2 {
  113. position: relative;
  114. z-index: 2;
  115. }
  116. .q-box {
  117. width: 100%;
  118. height: 71%;
  119. padding-bottom: constant(safe-area-inset-bottom);
  120. padding-bottom: env(safe-area-inset-bottom);
  121. position: relative;
  122. }
  123. .bg-img {
  124. width: 100%;
  125. height: 89%;
  126. position: absolute;
  127. left: 0;
  128. top: 0;
  129. }
  130. .but-box {
  131. width: 100%;
  132. height: 170rpx;
  133. position: relative;
  134. }
  135. .but-img {
  136. position: absolute;
  137. left: 0;
  138. top: 0;
  139. width: 100%;
  140. height: 100%;
  141. }
  142. .pt-49 {
  143. padding-top: 49rpx;
  144. }
  145. .qp-img {
  146. width: 118rpx;
  147. height: 118rpx;
  148. }
  149. </style>