Container.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <script setup>
  2. import Empty from "../Empty/Empty.vue";
  3. import { ref, onMounted, watchEffect } from "vue";
  4. import { router, getRoute } from "../../utils/router";
  5. import { getRect, debounce } from "../../utils";
  6. import { getCurrentInstance } from "vue";
  7. import pages from "@/pages.json";
  8. const props = defineProps({
  9. showBottom: {
  10. type: Boolean,
  11. default: false,
  12. },
  13. bottomText: {
  14. type: String,
  15. default: "~已经到底了~",
  16. },
  17. scrollX: {
  18. type: Boolean,
  19. default: false,
  20. },
  21. scrollY: {
  22. type: Boolean,
  23. default: true,
  24. },
  25. className: {
  26. type: String,
  27. default: "",
  28. },
  29. scrollIntoView: {
  30. type: String,
  31. default: "",
  32. },
  33. scrollStyle: {
  34. type: Object,
  35. default: () => ({}),
  36. },
  37. onBack: Function,
  38. showBack: {
  39. type: Boolean,
  40. default: true,
  41. },
  42. showTitle: {
  43. type: Boolean,
  44. default: true,
  45. },
  46. bgColor: {
  47. type: String,
  48. default: "#fff",
  49. },
  50. title: String,
  51. empty: {
  52. type: [Boolean],
  53. default: false,
  54. },
  55. border: {
  56. type: Boolean,
  57. default: false,
  58. },
  59. scrollTop: {
  60. type: Number,
  61. default: 0,
  62. },
  63. headerColor: {
  64. type: String,
  65. default: "transparent",
  66. },
  67. bottomBgColor: {
  68. type: String,
  69. default: "transparent",
  70. },
  71. bottomBorder: {
  72. type: Boolean,
  73. default: false,
  74. },
  75. });
  76. const emit = defineEmits(["onSafeAreaChange", "onScroll"]);
  77. const onBack = async () => {
  78. if (props.onBack) {
  79. const res = await props.onBack();
  80. if (!res) return;
  81. router.back();
  82. return;
  83. }
  84. router.back();
  85. };
  86. const safeArea = ref({
  87. footer: {},
  88. title: {},
  89. });
  90. const footerHeight = ref(0);
  91. const menuButtonInfo = ref({
  92. top: 0,
  93. height: 0,
  94. width: 0,
  95. });
  96. onMounted(() => {
  97. const _menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  98. menuButtonInfo.value = {
  99. top: _menuButtonInfo.top,
  100. height: _menuButtonInfo.height,
  101. width: _menuButtonInfo.width,
  102. };
  103. const systemInfo = uni.getWindowInfo();
  104. // 判断是否为tabbar页面
  105. safeArea.value = {
  106. ...systemInfo.safeArea,
  107. source: systemInfo.safeArea,
  108. };
  109. // 解决部分安卓手机安全顶部为0 的bug
  110. if (!safeArea.value.top) {
  111. safeArea.value.top = systemInfo.statusBarHeight;
  112. }
  113. const isTarbarPage = pages.tabBar.list
  114. .map((item) => item.pagePath)
  115. .includes(getRoute().routeList[0].path);
  116. if (
  117. safeArea.value.source.bottom === safeArea.value.source.height &&
  118. isTarbarPage
  119. ) {
  120. safeArea.value.height -= 50;
  121. }
  122. // 24是内边距
  123. safeArea.value.width -= 24;
  124. const instance = getCurrentInstance();
  125. // 获取头部高度
  126. getRect({
  127. name: ".title",
  128. onSuccess(res) {
  129. safeArea.value.title = res;
  130. safeArea.value.height -= props.showTitle
  131. ? isTarbarPage
  132. ? safeArea.value.title?.height + safeArea.value.top
  133. : safeArea.value.title?.height
  134. : 0;
  135. },
  136. instance,
  137. });
  138. // 获取底部高度
  139. getRect({
  140. name: ".bottom-button",
  141. onSuccess(res) {
  142. safeArea.value.footer = res;
  143. safeArea.value.height -= safeArea.value.footer?.height;
  144. safeArea.value.height += 22; // 剩余高度
  145. },
  146. instance,
  147. });
  148. });
  149. watchEffect(() => {
  150. emit("onSafeAreaChange", safeArea.value);
  151. });
  152. const onScroll = debounce((e) => {
  153. emit("onScroll", e);
  154. }, 100);
  155. defineExpose({
  156. safeArea: safeArea.value,
  157. });
  158. </script>
  159. <template>
  160. <view class="container" :style="{ background: bgColor }">
  161. <view class="bg" v-if="$slots.bg">
  162. <slot name="bg"></slot>
  163. </view>
  164. <view
  165. class="title"
  166. :style="{
  167. paddingTop: `${safeArea.top}px`,
  168. background: headerColor,
  169. }"
  170. v-if="showTitle"
  171. >
  172. <uni-nav-bar
  173. :left-icon="showBack ? 'left' : ''"
  174. @clickLeft="onBack"
  175. :border="border"
  176. :title="title"
  177. fixed
  178. :backgroundColor="headerColor"
  179. />
  180. </view>
  181. <scroll-view
  182. enable-flex
  183. show-scrollbar
  184. enable-back-to-top
  185. scroll-with-animation
  186. scroll-anchoring
  187. :scroll-x="scrollX"
  188. :scroll-y="scrollY"
  189. :scroll-top="scrollTop"
  190. @scroll="onScroll"
  191. v-if="!empty"
  192. >
  193. <view
  194. :class="['scroll-view', scrollX ? 'topic-scroll' : '']"
  195. :style="{
  196. height: `calc(100vh - ${
  197. menuButtonInfo.top +
  198. menuButtonInfo.height +
  199. 40
  200. }px - env(safe-area-inset-bottom))`,
  201. whiteSpace: scrollX ? 'nowrap' : 'normal',
  202. ...scrollStyle,
  203. }"
  204. >
  205. <slot></slot>
  206. </view>
  207. <!-- 底部文字 -->
  208. <view v-if="showBottom && bottomText" class="bottom-text">
  209. {{ bottomText }}
  210. </view>
  211. </scroll-view>
  212. <view class="empty" v-else>
  213. <Empty v-bind="$attrs" />
  214. </view>
  215. <slot name="stick"></slot>
  216. <view
  217. v-if="$slots.footer"
  218. class="bottom-button"
  219. :style="{
  220. width: `${safeArea.width}px`,
  221. background: bottomBgColor,
  222. borderTop: bottomBorder ? '1px solid #eee' : 'none',
  223. }"
  224. >
  225. <slot name="footer" :footer-height="footerHeight" />
  226. </view>
  227. </view>
  228. </template>
  229. <style lang="scss" scoped>
  230. @import "@/uni.scss";
  231. .title {
  232. position: sticky;
  233. top: 0;
  234. z-index: 9999;
  235. }
  236. .bg {
  237. position: absolute;
  238. top: 0;
  239. }
  240. .empty {
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. flex: 1;
  245. }
  246. .container {
  247. display: flex;
  248. flex-direction: column;
  249. color: #333;
  250. box-sizing: content-box;
  251. position: relative;
  252. overflow: hidden;
  253. height: 100vh;
  254. }
  255. .scroll-view {
  256. position: relative;
  257. padding: 24rpx;
  258. box-sizing: content-box;
  259. }
  260. .bottom-text {
  261. display: flex;
  262. align-items: center;
  263. justify-content: center;
  264. width: 100%;
  265. }
  266. .bottom-button {
  267. width: 100%;
  268. padding: 24rpx;
  269. position: absolute;
  270. bottom: 0;
  271. }
  272. .topic-scroll {
  273. white-space: nowrap;
  274. display: inline-flex;
  275. }
  276. </style>