Container.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <script setup>
  2. import Empty from "../Empty/Empty.vue";
  3. import { ref, onMounted, watchEffect } from "vue";
  4. import { router, getRoute, normalizeParams } 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. if (typeof res === "string") {
  82. router.reLaunch(normalizeParams(res));
  83. return;
  84. }
  85. router.back();
  86. return;
  87. }
  88. router.back();
  89. };
  90. const safeArea = ref({
  91. footer: {},
  92. title: {},
  93. });
  94. const footerHeight = ref(0);
  95. const menuButtonInfo = ref({
  96. top: 0,
  97. height: 0,
  98. width: 0,
  99. });
  100. onMounted(() => {
  101. const _menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  102. menuButtonInfo.value = {
  103. top: _menuButtonInfo.top,
  104. height: _menuButtonInfo.height,
  105. width: _menuButtonInfo.width,
  106. };
  107. const systemInfo = uni.getWindowInfo();
  108. // 判断是否为tabbar页面
  109. safeArea.value = {
  110. ...systemInfo.safeArea,
  111. source: systemInfo.safeArea,
  112. };
  113. // 解决部分安卓手机安全顶部为0 的bug
  114. if (!safeArea.value.top) {
  115. safeArea.value.top = systemInfo.statusBarHeight;
  116. }
  117. const isTarbarPage = pages.tabBar.list
  118. .map((item) => item.pagePath)
  119. .includes(getRoute().routeList[0].path);
  120. if (
  121. safeArea.value.source.bottom === safeArea.value.source.height &&
  122. isTarbarPage
  123. ) {
  124. safeArea.value.height -= 50;
  125. }
  126. if (props.scrollStyle?.padding === void 0) {
  127. // 24是内边距
  128. safeArea.value.width -= 24;
  129. }
  130. const instance = getCurrentInstance();
  131. // 获取头部高度
  132. getRect({
  133. name: ".title",
  134. onSuccess(res) {
  135. safeArea.value.title = res;
  136. },
  137. instance,
  138. });
  139. // 获取底部高度
  140. getRect({
  141. name: ".bottom-button",
  142. onSuccess(res) {
  143. safeArea.value.footer = res;
  144. },
  145. instance,
  146. });
  147. });
  148. watchEffect(() => {
  149. emit("onSafeAreaChange", safeArea.value);
  150. });
  151. const onScroll = debounce((e) => {
  152. emit("onScroll", e);
  153. }, 100);
  154. defineExpose({
  155. safeArea: safeArea.value,
  156. });
  157. </script>
  158. <template>
  159. <view class="container" :style="{ background: bgColor }">
  160. <view class="bg" v-if="$slots.bg">
  161. <slot name="bg"></slot>
  162. </view>
  163. <view
  164. class="title"
  165. :style="{
  166. paddingTop: `${safeArea.top}px`,
  167. background: headerColor,
  168. }"
  169. v-if="showTitle"
  170. >
  171. <uni-nav-bar
  172. :left-icon="showBack ? 'left' : ''"
  173. @clickLeft="onBack"
  174. :border="border"
  175. :title="title"
  176. fixed
  177. :backgroundColor="headerColor"
  178. />
  179. </view>
  180. <scroll-view
  181. enable-flex
  182. :show-scrollbar="false"
  183. enable-back-to-top
  184. scroll-with-animation
  185. scroll-anchoring
  186. :scroll-x="scrollX"
  187. :scroll-y="scrollY"
  188. :scroll-top="scrollTop"
  189. @scroll="onScroll"
  190. :class="['scroll-view', scrollX ? 'topic-scroll' : '']"
  191. :style="{
  192. height: `calc(${
  193. safeArea.height - safeArea.top - safeArea.title?.height - 100
  194. }px - env(safe-area-inset-bottom))`,
  195. width: safeArea.width + 'px',
  196. whiteSpace: scrollX ? 'nowrap' : 'normal',
  197. flex: 1,
  198. ...scrollStyle,
  199. }"
  200. v-if="!empty"
  201. >
  202. <slot></slot>
  203. <!-- 底部文字 -->
  204. <view v-if="showBottom && bottomText" class="bottom-text">
  205. {{ bottomText }}
  206. </view>
  207. </scroll-view>
  208. <view class="empty" v-else>
  209. <Empty v-bind="$attrs" />
  210. </view>
  211. <slot name="stick"></slot>
  212. <view
  213. v-if="$slots.footer"
  214. class="bottom-button"
  215. :style="{
  216. width: `${safeArea.width}px`,
  217. background: bottomBgColor,
  218. borderTop: bottomBorder ? '1px solid #eee' : 'none',
  219. }"
  220. >
  221. <slot name="footer" :footer-height="footerHeight" />
  222. </view>
  223. </view>
  224. </template>
  225. <style lang="scss" scoped>
  226. @import "@/uni.scss";
  227. scroll-view ::-webkit-scrollbar {
  228. width: 0;
  229. height: 0;
  230. background-color: transparent;
  231. }
  232. .title {
  233. position: sticky;
  234. top: 0;
  235. z-index: 9999;
  236. }
  237. .bg {
  238. position: absolute;
  239. top: 0;
  240. }
  241. .empty {
  242. display: flex;
  243. align-items: center;
  244. justify-content: center;
  245. flex: 1;
  246. }
  247. .container {
  248. display: flex;
  249. flex-direction: column;
  250. color: #333;
  251. box-sizing: content-box;
  252. position: relative;
  253. overflow: hidden;
  254. height: 100vh;
  255. }
  256. .scroll-view {
  257. position: relative;
  258. padding: 24rpx;
  259. box-sizing: content-box;
  260. }
  261. .bottom-text {
  262. display: flex;
  263. align-items: center;
  264. justify-content: center;
  265. width: 100%;
  266. }
  267. .bottom-button {
  268. width: 100%;
  269. padding: 24rpx;
  270. position: absolute;
  271. bottom: 0;
  272. box-sizing: border-box;
  273. }
  274. .topic-scroll {
  275. white-space: nowrap;
  276. display: inline-flex;
  277. }
  278. </style>