1212.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="container flex-common-css iPhone-bottom box-border-box">
  3. <Nav :title="school_name"></Nav>
  4. <image class="bg-img" src="https://sjz-school.oss-cn-chengdu.aliyuncs.com/webImages/bg.png" mode=""></image>
  5. <!-- 泡泡容器 -->
  6. <view class="q-box z-2">
  7. <view v-for="(bubble, index) in bubbles" :key="index" :style="{
  8. transform: `translate(${bubble.x}px, ${bubble.y}px)`,
  9. width: bubble.size + 'px',
  10. height: bubble.size + 'px',
  11. backgroundColor: selectedBubbleIndex === index ? '#ffcc00' : 'rgba(255, 255, 255, 0.7)', // 选中时高亮
  12. }" class="bubble"></view>
  13. </view>
  14. <!-- 选择按钮 -->
  15. <view class="button" @click="selectRandomBubble">随机选择一个泡泡</view>
  16. <!-- 弹窗 -->
  17. <view v-if="showPopup" class="popup">
  18. <view class="popup-content">
  19. <text>选中的泡泡编号:{{ selectedBubbleIndex + 1 }}</text>
  20. <view class="close-button" @click="closePopup">关闭</view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. school_name: '',
  30. bubbles: [], // 存储泡泡信息
  31. screenWidth: 0, // 屏幕宽度
  32. screenHeight: 0, // 屏幕高度
  33. animationTimer: null, // 动画定时器
  34. selectedBubbleIndex: null, // 当前选中的泡泡索引
  35. showPopup: false, // 是否显示弹窗
  36. isSelecting: false, // 是否正在选择中
  37. };
  38. },
  39. onLoad(data) {
  40. this.school_name = uni.getStorageSync('school_name');
  41. },
  42. mounted() {
  43. this.getScreenSize(); // 获取屏幕尺寸
  44. this.generateBubbles(20); // 生成 20 个泡泡
  45. this.startAnimation(); // 开始动画
  46. },
  47. beforeDestroy() {
  48. // 组件销毁时停止动画
  49. if (this.animationTimer) {
  50. clearTimeout(this.animationTimer);
  51. }
  52. },
  53. methods: {
  54. // 获取屏幕尺寸
  55. getScreenSize() {
  56. // uni.createSelectorQuery().in(this).select('.q-box').boundingClientRect((rect) => {
  57. // if (rect) {
  58. // this.screenHeight = rect.height;
  59. // }
  60. // }).exec();
  61. const systemInfo = uni.getSystemInfoSync();
  62. this.screenWidth = systemInfo.windowWidth; // 屏幕宽度
  63. this.screenHeight = systemInfo.windowHeight; // 屏幕高度
  64. },
  65. // 生成随机泡泡
  66. generateBubbles(count) {
  67. const containerHeight = this.screenHeight * 0.7; // 泡泡容器高度为屏幕高度的 70%
  68. for (let i = 0; i < count; i++) {
  69. this.bubbles.push({
  70. x: Math.random() * (this.screenWidth - 50), // 随机水平位置
  71. y: Math.random() * (this.screenHeight - 50), // 随机垂直位置
  72. size: Math.random() * 40 + 20, // 随机大小(20px - 60px)
  73. speedX: (Math.random() - 0.5) * 2, // 水平速度(-1 到 1)
  74. speedY: (Math.random() - 0.5) * 2, // 垂直速度(-1 到 1)
  75. });
  76. }
  77. },
  78. // 开始动画
  79. startAnimation() {
  80. const animate = () => {
  81. this.updateBubblesPosition();
  82. this.animationTimer = setTimeout(animate, 16); // 模拟 60 FPS
  83. };
  84. animate(); // 启动动画
  85. },
  86. // 更新泡泡位置
  87. updateBubblesPosition() {
  88. const containerHeight = this.screenHeight * 0.7; // 泡泡容器高度为屏幕高度的 70%
  89. this.bubbles.forEach((bubble) => {
  90. // 更新位置
  91. bubble.x += bubble.speedX;
  92. bubble.y += bubble.speedY;
  93. // 碰到左右边框时反转水平速度
  94. if (bubble.x <= 0 || bubble.x + bubble.size >= this.screenWidth) {
  95. bubble.speedX = -bubble.speedX;
  96. }
  97. // 碰到上下边框时反转垂直速度
  98. if (bubble.y <= 0 || bubble.y + bubble.size >= this.screenHeight) {
  99. bubble.speedY = -bubble.speedY;
  100. }
  101. });
  102. },
  103. // 随机选择一个泡泡
  104. selectRandomBubble() {
  105. if (this.isSelecting) return; // 防止重复点击
  106. this.isSelecting = true;
  107. // 随机选择过程动画
  108. const totalSteps = 20; // 随机闪烁次数
  109. let step = 0;
  110. const interval = setInterval(() => {
  111. this.selectedBubbleIndex = Math.floor(Math.random() * this.bubbles.length);
  112. step++;
  113. if (step >= totalSteps) {
  114. clearInterval(interval);
  115. this.isSelecting = false;
  116. this.showPopup = true; // 显示弹窗
  117. }
  118. }, 100); // 每 100 毫秒闪烁一次
  119. },
  120. // 关闭弹窗
  121. closePopup() {
  122. this.showPopup = false;
  123. this.selectedBubbleIndex = null; // 重置选中状态
  124. },
  125. },
  126. };
  127. </script>
  128. <style>
  129. .bg-img {
  130. width: 100%;
  131. height: 89%;
  132. position: absolute;
  133. left: 0;
  134. top: 0;
  135. }
  136. .container {
  137. position: relative;
  138. overflow: hidden;
  139. }
  140. .q-box {
  141. /* width: 100%;
  142. height: 71vh;
  143. padding-bottom: constant(safe-area-inset-bottom);
  144. padding-bottom: env(safe-area-inset-bottom);
  145. position: relative; */
  146. }
  147. .bubble {
  148. position: absolute;
  149. background-color: rgba(255, 255, 255, 0.7);
  150. /* 泡泡颜色 */
  151. border-radius: 50%;
  152. will-change: transform;
  153. /* 优化性能 */
  154. }
  155. .button {
  156. position: absolute;
  157. bottom: 20px;
  158. left: 50%;
  159. transform: translateX(-50%);
  160. padding: 10px 20px;
  161. background-color: #007aff;
  162. color: white;
  163. border-radius: 20px;
  164. text-align: center;
  165. font-size: 16px;
  166. cursor: pointer;
  167. }
  168. .popup {
  169. position: fixed;
  170. top: 0;
  171. left: 0;
  172. width: 100%;
  173. height: 100%;
  174. background-color: rgba(0, 0, 0, 0.5);
  175. display: flex;
  176. justify-content: center;
  177. align-items: center;
  178. }
  179. .popup-content {
  180. background-color: white;
  181. padding: 20px;
  182. border-radius: 10px;
  183. text-align: center;
  184. }
  185. .close-button {
  186. margin-top: 10px;
  187. padding: 5px 10px;
  188. background-color: #007aff;
  189. color: white;
  190. border-radius: 5px;
  191. cursor: pointer;
  192. }
  193. </style>