nav-top-bar.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view>
  3. <!-- 固定在顶部:状态栏占位 + 页面自己的头部内容(插槽),滚动时内容从其下方穿过 -->
  4. <view class="nav-top-bar" :style="{ background: background }">
  5. <uni-status-bar />
  6. <view class="nav-top-bar__content">
  7. <slot />
  8. </view>
  9. </view>
  10. <!-- 占位:高度自动等于固定栏总高(状态栏 + 插槽内容),保证滚动前布局不变 -->
  11. <view class="nav-top-bar__placeholder" :style="{ height: placeholderHeight + 'px' }"></view>
  12. </view>
  13. </template>
  14. <script>
  15. import uniStatusBar from '../uni-status-bar/uni-status-bar.vue';
  16. export default {
  17. name: 'nav-top-bar',
  18. components: {
  19. uniStatusBar
  20. },
  21. props: {
  22. // 固定栏背景(需不透明才能盖住滚上去的内容),可传颜色或渐变
  23. background: {
  24. type: String,
  25. default: '#FFFFFF'
  26. }
  27. },
  28. data() {
  29. return {
  30. placeholderHeight: 0
  31. };
  32. },
  33. mounted() {
  34. this.$nextTick(this.measure);
  35. },
  36. methods: {
  37. // 插槽内容高度变化后(如异步数据渲染出头部)可调用 this.$refs.xxx.measure()
  38. measure() {
  39. uni.createSelectorQuery().in(this).select('.nav-top-bar').boundingClientRect(rect => {
  40. if (rect && rect.height) {
  41. this.placeholderHeight = rect.height;
  42. }
  43. }).exec();
  44. }
  45. }
  46. };
  47. </script>
  48. <style lang="scss" scoped>
  49. .nav-top-bar {
  50. position: fixed;
  51. top: 0;
  52. left: 0;
  53. right: 0;
  54. z-index: 999;
  55. }
  56. .nav-top-bar__content {
  57. width: 100%;
  58. }
  59. </style>