Container.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <script setup>
  2. import pages from "@/pages.json";
  3. import { ref, onMounted, watchEffect } from "vue";
  4. import { getRoute, router } from "../../utils/router";
  5. import { getRect } from "../../utils";
  6. import { getCurrentInstance } from "vue";
  7. const props = defineProps({
  8. showBottom: {
  9. type: Boolean,
  10. default: false,
  11. },
  12. bottomText: {
  13. type: String,
  14. default: "~已经到底了~",
  15. },
  16. scrollX: {
  17. type: Boolean,
  18. default: false,
  19. },
  20. scrollY: {
  21. type: Boolean,
  22. default: true,
  23. },
  24. className: {
  25. type: String,
  26. default: "",
  27. },
  28. scrollIntoView: {
  29. type: String,
  30. default: "",
  31. },
  32. scrollStyle: {
  33. type: Object,
  34. default: () => ({}),
  35. },
  36. onBack: Function,
  37. showBack: {
  38. type: Boolean,
  39. default: true,
  40. },
  41. showTitle: {
  42. type: Boolean,
  43. default: true,
  44. },
  45. bgColor: {
  46. type: String,
  47. default: "#fff",
  48. },
  49. title: String,
  50. });
  51. const emit = defineEmits(["onSafeAreaChange"]);
  52. const onBack = async () => {
  53. if (props.onBack) {
  54. await props.onBack();
  55. router.back();
  56. return;
  57. }
  58. router.back();
  59. };
  60. const safeArea = ref({
  61. footer: {},
  62. title: {},
  63. });
  64. const footerHeight = ref(0);
  65. onMounted(() => {
  66. const systemInfo = uni.getWindowInfo();
  67. // 判断是否为tabbar页面
  68. safeArea.value = {
  69. ...systemInfo.safeArea,
  70. source: systemInfo.safeArea,
  71. };
  72. // 解决部分安卓手机安全顶部为0 的bug
  73. if (!safeArea.value.top) {
  74. safeArea.value.top = systemInfo.statusBarHeight || 40;
  75. }
  76. const isTarbarPage = pages.tabBar.list
  77. .map((item) => item.pagePath)
  78. .includes(getRoute().routeList[0].path);
  79. if (isTarbarPage) {
  80. // 高度再减掉tabbar高度
  81. safeArea.value.height -= 50;
  82. // 减去系统导航栏高度
  83. safeArea.value.height -= systemInfo.statusBarHeight - 18;
  84. }
  85. // 24是内边距
  86. safeArea.value.width -= 24;
  87. const instance = getCurrentInstance();
  88. // 获取头部高度
  89. getRect({
  90. name: ".title",
  91. onSuccess(res) {
  92. safeArea.value.title = res;
  93. safeArea.value.height -= props.showTitle
  94. ? safeArea.value.title?.height
  95. : 0;
  96. },
  97. instance,
  98. });
  99. // 获取底部高度
  100. getRect({
  101. name: ".bottom-button",
  102. onSuccess(res) {
  103. safeArea.value.footer = res;
  104. safeArea.value.height -= safeArea.value.footer?.height;
  105. safeArea.value.height += 22; // 剩余高度
  106. },
  107. instance,
  108. });
  109. });
  110. watchEffect(() => {
  111. emit("onSafeAreaChange", safeArea.value);
  112. });
  113. defineExpose({
  114. safeArea: safeArea.value,
  115. });
  116. </script>
  117. <template>
  118. <view class="container">
  119. <view
  120. class="title"
  121. :style="{
  122. paddingTop: `${safeArea.top}px`,
  123. background: bgColor,
  124. }"
  125. v-if="showTitle"
  126. >
  127. <uni-nav-bar
  128. :left-icon="showBack ? 'left' : ''"
  129. @clickLeft="onBack"
  130. :border="false"
  131. :title="title"
  132. fixed
  133. :backgroundColor="bgColor"
  134. />
  135. </view>
  136. <scroll-view
  137. :scroll-y="scrollY"
  138. :scroll-with-animation="true"
  139. :enable-flex="true"
  140. :enable-passive="true"
  141. :enhanced="true"
  142. :paging-enabled="true"
  143. :scroll-anchoring="true"
  144. :show-scrollbar="false"
  145. :scroll-x="scrollX"
  146. :scroll-into-view="scrollIntoView"
  147. enable-back-to-top
  148. scroll-anchoring
  149. >
  150. <view
  151. :class="['scroll-view', scrollX ? 'topic-scroll' : '']"
  152. :style="{
  153. height: `${safeArea.height}px`,
  154. whiteSpace: scrollX ? 'nowrap' : 'normal',
  155. ...scrollStyle,
  156. }"
  157. >
  158. <slot></slot>
  159. </view>
  160. <!-- 底部文字 -->
  161. <view v-if="showBottom && bottomText" class="bottom-text">
  162. {{ bottomText }}
  163. </view>
  164. </scroll-view>
  165. <view
  166. v-if="$slots.footer"
  167. class="bottom-button"
  168. :style="{
  169. width: `${safeArea.width}px`,
  170. background: bgColor,
  171. }"
  172. >
  173. <slot name="footer" :footer-height="footerHeight" />
  174. </view>
  175. </view>
  176. </template>
  177. <style lang="scss" scoped>
  178. @import "@/uni.scss";
  179. .title {
  180. position: sticky;
  181. top: 0;
  182. z-index: 9999;
  183. }
  184. .container {
  185. display: flex;
  186. flex-direction: column;
  187. color: #333;
  188. box-sizing: content-box;
  189. position: relative;
  190. height: 100vh;
  191. }
  192. .scroll-view {
  193. position: relative;
  194. padding: 24rpx 24rpx 0;
  195. box-sizing: content-box;
  196. }
  197. .bottom-text {
  198. display: flex;
  199. align-items: center;
  200. justify-content: center;
  201. width: 100%;
  202. }
  203. .bottom-button {
  204. width: 100%;
  205. padding: 24rpx;
  206. position: absolute;
  207. bottom: 0;
  208. }
  209. .topic-scroll {
  210. white-space: nowrap;
  211. display: inline-flex;
  212. }
  213. </style>