Container.vue 5.8 KB

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