Container.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. if (props.scrollStyle?.padding === void 0) {
  123. // 24是内边距
  124. safeArea.value.width -= 24;
  125. }
  126. const instance = getCurrentInstance();
  127. // 获取头部高度
  128. getRect({
  129. name: ".title",
  130. onSuccess(res) {
  131. safeArea.value.title = res;
  132. },
  133. instance,
  134. });
  135. // 获取底部高度
  136. getRect({
  137. name: ".bottom-button",
  138. onSuccess(res) {
  139. safeArea.value.footer = res;
  140. },
  141. instance,
  142. });
  143. });
  144. watchEffect(() => {
  145. emit("onSafeAreaChange", safeArea.value);
  146. });
  147. const onScroll = debounce((e) => {
  148. emit("onScroll", e);
  149. }, 100);
  150. defineExpose({
  151. safeArea: safeArea.value,
  152. });
  153. </script>
  154. <template>
  155. <view class="container" :style="{ background: bgColor }">
  156. <view class="bg" v-if="$slots.bg">
  157. <slot name="bg"></slot>
  158. </view>
  159. <view
  160. class="title"
  161. :style="{
  162. paddingTop: `${safeArea.top}px`,
  163. background: headerColor,
  164. }"
  165. v-if="showTitle"
  166. >
  167. <uni-nav-bar
  168. :left-icon="showBack ? 'left' : ''"
  169. @clickLeft="onBack"
  170. :border="border"
  171. :title="title"
  172. fixed
  173. :backgroundColor="headerColor"
  174. />
  175. </view>
  176. <scroll-view
  177. enable-flex
  178. :show-scrollbar="false"
  179. enable-back-to-top
  180. scroll-with-animation
  181. scroll-anchoring
  182. :scroll-x="scrollX"
  183. :scroll-y="scrollY"
  184. :scroll-top="scrollTop"
  185. @scroll="onScroll"
  186. :class="['scroll-view', scrollX ? 'topic-scroll' : '']"
  187. :style="{
  188. height: `calc(${
  189. safeArea.height - safeArea.top - safeArea.title?.height - 100
  190. }px - env(safe-area-inset-bottom))`,
  191. width: safeArea.width + 'px',
  192. whiteSpace: scrollX ? 'nowrap' : 'normal',
  193. flex: 1,
  194. ...scrollStyle,
  195. }"
  196. v-if="!empty"
  197. >
  198. <slot></slot>
  199. <!-- 底部文字 -->
  200. <view v-if="showBottom && bottomText" class="bottom-text">
  201. {{ bottomText }}
  202. </view>
  203. </scroll-view>
  204. <view class="empty" v-else>
  205. <Empty v-bind="$attrs" />
  206. </view>
  207. <slot name="stick"></slot>
  208. <view
  209. v-if="$slots.footer"
  210. class="bottom-button"
  211. :style="{
  212. width: `${safeArea.width}px`,
  213. background: bottomBgColor,
  214. borderTop: bottomBorder ? '1px solid #eee' : 'none',
  215. }"
  216. >
  217. <slot name="footer" :footer-height="footerHeight" />
  218. </view>
  219. </view>
  220. </template>
  221. <style lang="scss" scoped>
  222. @import "@/uni.scss";
  223. scroll-view ::-webkit-scrollbar {
  224. width: 0;
  225. height: 0;
  226. background-color: transparent;
  227. }
  228. .title {
  229. position: sticky;
  230. top: 0;
  231. z-index: 9999;
  232. }
  233. .bg {
  234. position: absolute;
  235. top: 0;
  236. }
  237. .empty {
  238. display: flex;
  239. align-items: center;
  240. justify-content: center;
  241. flex: 1;
  242. }
  243. .container {
  244. display: flex;
  245. flex-direction: column;
  246. color: #333;
  247. box-sizing: content-box;
  248. position: relative;
  249. overflow: hidden;
  250. height: 100vh;
  251. }
  252. .scroll-view {
  253. position: relative;
  254. padding: 24rpx;
  255. box-sizing: content-box;
  256. }
  257. .bottom-text {
  258. display: flex;
  259. align-items: center;
  260. justify-content: center;
  261. width: 100%;
  262. }
  263. .bottom-button {
  264. width: 100%;
  265. padding: 24rpx;
  266. position: absolute;
  267. bottom: 0;
  268. box-sizing: border-box;
  269. }
  270. .topic-scroll {
  271. white-space: nowrap;
  272. display: inline-flex;
  273. }
  274. </style>