uniapp顶部导航部分

1、动态获取状态栏高度

 

2. 编写自定义顶部导航栏组件

首先需要在 pase.json 的 globalStyle 里增加 "navigationStyle": "custom"的属性。

然后cv大法

<template>
  <view
    class="custom-nav-head"
    :style="{
      height: headHeight + 'rpx',
      background: backgroundColor,
      color: textColor,
    }"
  >
    <view
      :style="{
        height: navHeight + 'rpx',
        'padding-top': navTop + 'rpx',
        'padding-right': navPaddingRight + 'rpx',
      }"
    >
      <!-- 小程序目前不支持插槽里放默认值,因此通过样式来实现分发的功能  -->
      <view class="default-slot">
        <slot></slot>
      </view>
      <view class="nav-body">
        <view class="left-slot">
          <slot name="left"></slot>
        </view>
        <view class="nav-body-left">
          <van-icon
            v-if="isShowBackBtn"
            name="arrow-left"
            color="#000000"
            size="32rpx"
            @click="backPage()"
          />
        </view>
        <view class="conent-slot">
          <slot name="conent"></slot>
        </view>
        <view
          class="nav-body-conent"
          :style="{ paddingLeft: textPaddingLeft + 'rpx' }"
        >
          {{ titleText }}
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "navigationBar",
  props: {
    // 导航栏的背景颜色
    backgroundColor: {
      type: String,
      default: "#fff",
    },
    // 文字颜色
    textColor: {
      type: String,
      default: "black",
    },
    // 显示的文本
    titleText: {
      type: String,
      default: "小锦",
    },
    // 文字的左边距
    textPaddingLeft: {
      type: Number,
      default: 222,
    },
    // 返回的页数
    deltaPageNum: {
      type: Number,
      default: 1,
    },
    // 是否显示返回按钮
    isShowBackBtn: {
      type: Boolean,
      default: true,
    },
  },
  mounted() {
    // 获取胶囊的位置
    const menuButtonObject = uni.getMenuButtonBoundingClientRect();
    // 获取系统信息
    uni.getSystemInfo({
      success: (res) => {
        // 获取状态栏的高度
        const statusBarHeight = res.statusBarHeight;
        // 获取胶囊顶部的距离【拿到的是px需要转化为rpx * 2】
        this.navTop = menuButtonObject.top * 2;
        this.navHeight = menuButtonObject.height * 2;
        this.navPaddingRight = (menuButtonObject.width + 6) * 2;
        console.log(menuButtonObject);
        // 头部的高度 = 状态栏的高度 + 胶囊的高度 + (胶囊顶部的距离 - 状态栏的高度 ) * 2 【胶囊顶部的距离 - 状态栏的高度 * 2  = 胶囊的上下外边距】
        this.headHeight =
          (statusBarHeight +
            menuButtonObject.height +
            (menuButtonObject.top - statusBarHeight) * 2) *
          2;
      },
    });
  },
  data() {
    return {
      // 头部的高度
      headHeight: "",
      // 导航栏的右边距
      navPaddingRight: "",
      // 导航栏的高度
      navHeight: "",
      // 导航栏距离顶部的距离
      navTop: "",
    };
  },
  methods: {
    backPage() {
      uni.navigateBack({
        delta: this.deltaPageNum,
        success: (res) => {
          this.$emit("backSuccess", res);
        },
        fail: () => {
          this.$emit("backFail", res);
        },
        complete: () => {
          this.$emit("backComplete", res);
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.custom-nav-head {
  width: 750rpx;
  color: #fff;
  top: 0;
  z-index: 999;
  .default-slot:empty + .nav-body {
    display: flex;
  }
  .nav-body {
    display: none;
    align-items: center;
    padding-left: 70rpx;  // 导航栏左边距
    height: 100%;
    // 左部的标签
    .left-slot:empty + .nav-body-left {
      display: block;
    }
    .nav-body-left {
      display: none;
    }
    // 中间部分
    .conent-slot:empty + .nav-body-conent {
      display: block;
    }
    .nav-body-conent {
      display: none;
      flex: 1;
      padding-left: 227rpx;
      font-family: "PingFang SC";
      font-style: normal;
      font-weight: 500;
      font-size: 20px;
    }
  }
}
</style>

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp中自定义顶部滑动导航的步骤如下: 1. 首先,在`page.json`文件中定义自定义导航栏的样式。你可以设置`navigationStyle`为`custom`来启用自定义导航栏样式。例如: ``` { "path": "pages/charts/charts", "style": { "navigationBarTitleText": "", "enablePullDownRefresh": false, "navigationStyle": "custom" } } ``` 2. 接下来,在页面对应的`style`部分中定义导航栏样式和滑动内容区的样式。例如: ``` <style lang="scss"> // 顶部导航栏 .chartsTop { width: 750rpx; position: fixed; top: 0; z-index: 999; background-color: blue; } // 导航栏里边文字 .text { position: absolute; left: 20rpx; bottom: 10rpx; color: #ffffff; } // 内容区顶部背景 .top { height: 300rpx; width: 100%; background-color: blue; position: fixed; z-index: -1; display: flex; justify-content: center; .top-img { width: 240px; height: 180px; } } // scroll-view 里边内容可滑动 包裹chartsContent 透明 .test { position: fixed; background-color: rgba(0, 0, 0, 0); height: 1400rpx; border: 0; box-sizing: border-box; } // 可滑动的view .chartsContent { width: 100%; height: 1400rpx; position: absolute; margin-top: 290rpx; border: 0; box-sizing: border-box; border-radius: 20rpx; background-color: #fff; } </style> ``` 3. 最后,在自定义页面的模板中,使用对应的类名来应用定义的样式。例如: ``` <view class="container"> <!-- 导航栏 --> <view class="chartsTop" :style="{ height: contentTop }"> <text class="text" :style="{ fontSize: contentFontSize }">排行</text> </view> <!-- 顶部背景 --> <view class="top" :style="{ top: contentTop }"> <img class="top-img" src="/static/images/othericons/正在开发中.png" alt="" /> </view> <scroll-view scroll-y="true" class="test" :style="{ top: contentTop }"> <!-- 可滑动view --> <view class="chartsContent"> <!-- 内容 --> </view> </scroll-view> </view> ``` 通过以上步骤,你可以在UniApp中自定义顶部滑动导航。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [uniapp小程序自定义导航栏,可滑动view](https://blog.csdn.net/qq_44184452/article/details/131044118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值