Container.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. const isTarbarPage = pages.tabBar.list
  73. .map((item) => item.pagePath)
  74. .includes(getRoute().routeList[0].path);
  75. if (isTarbarPage) {
  76. // 高度再减掉tabbar高度
  77. safeArea.value.height -= 50;
  78. // 减去系统导航栏高度
  79. safeArea.value.height -= systemInfo.statusBarHeight - 18;
  80. }
  81. // 24是内边距
  82. safeArea.value.width -= 24;
  83. const instance = getCurrentInstance();
  84. // 获取头部高度
  85. getRect({
  86. name: ".title",
  87. onSuccess(res) {
  88. safeArea.value.title = res;
  89. safeArea.value.height -= props.showTitle
  90. ? safeArea.value.title?.height
  91. : 0;
  92. },
  93. instance,
  94. });
  95. // 获取底部高度
  96. getRect({
  97. name: ".bottom-button",
  98. onSuccess(res) {
  99. safeArea.value.footer = res;
  100. safeArea.value.height -= safeArea.value.footer?.height;
  101. safeArea.value.height += 22; // 剩余高度
  102. },
  103. instance,
  104. });
  105. });
  106. watchEffect(() => {
  107. emit("onSafeAreaChange", safeArea.value);
  108. });
  109. defineExpose({
  110. safeArea: safeArea.value,
  111. });
  112. </script>
  113. <template>
  114. <view class="container">
  115. <view
  116. class="title"
  117. :style="{
  118. paddingTop: `${safeArea.top}px`,
  119. background: bgColor,
  120. }"
  121. v-if="showTitle"
  122. >
  123. <uni-nav-bar
  124. :left-icon="showBack ? 'left' : ''"
  125. @clickLeft="onBack"
  126. :border="false"
  127. :title="title"
  128. fixed
  129. :backgroundColor="bgColor"
  130. />
  131. </view>
  132. <scroll-view
  133. :scroll-y="scrollY"
  134. :scroll-with-animation="true"
  135. :enable-flex="true"
  136. :enable-passive="true"
  137. :enhanced="true"
  138. :paging-enabled="true"
  139. :scroll-anchoring="true"
  140. :show-scrollbar="false"
  141. :scroll-x="scrollX"
  142. :scroll-into-view="scrollIntoView"
  143. enable-back-to-top
  144. scroll-anchoring
  145. >
  146. <view
  147. :class="['scroll-view', scrollX ? 'topic-scroll' : '']"
  148. :style="{
  149. height: `${safeArea.height}px`,
  150. whiteSpace: scrollX ? 'nowrap' : 'normal',
  151. ...scrollStyle,
  152. }"
  153. >
  154. <slot></slot>
  155. </view>
  156. <!-- 底部文字 -->
  157. <view v-if="showBottom && bottomText" class="bottom-text">
  158. {{ bottomText }}
  159. </view>
  160. </scroll-view>
  161. <view
  162. v-if="$slots.footer"
  163. class="bottom-button"
  164. :style="{
  165. width: `${safeArea.width}px`,
  166. background: bgColor,
  167. }"
  168. >
  169. <slot name="footer" :footer-height="footerHeight" />
  170. </view>
  171. </view>
  172. </template>
  173. <style lang="scss" scoped>
  174. @import "@/uni.scss";
  175. .title {
  176. position: sticky;
  177. top: 0;
  178. z-index: 9999;
  179. }
  180. .container {
  181. display: flex;
  182. flex-direction: column;
  183. color: #333;
  184. box-sizing: content-box;
  185. position: relative;
  186. height: 100vh;
  187. }
  188. .scroll-view {
  189. position: relative;
  190. padding: 24rpx 24rpx 0;
  191. box-sizing: content-box;
  192. }
  193. .bottom-text {
  194. display: flex;
  195. align-items: center;
  196. justify-content: center;
  197. width: 100%;
  198. }
  199. .bottom-button {
  200. width: 100%;
  201. padding: 24rpx;
  202. position: absolute;
  203. bottom: 0;
  204. }
  205. .topic-scroll {
  206. white-space: nowrap;
  207. display: inline-flex;
  208. }
  209. </style>