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