uniapp实现自定义导航栏随页面滚动展示隐藏

文章详细介绍了如何在uni-app中通过监听页面滚动事件,动态计算并改变顶部自定义导航栏的透明度。通过设置navigationStyle为custom,并在页面滚动时调整一个占位元素的透明度,实现了导航栏在页面向上滑动时逐渐变透明,向下滑动时恢复的效果。
  • 效果

功能演示

  • 实现原理
    通过页面滚动高度计算顶部自定义导航的透明度,当页面向上滑时透明度值变大,反之变小

  • 配置pages.json
    先在pages.json中配置navigationStyle

{
  	"path":"pages/cart/cart",
   	"style": {
     	"navigationStyle": "custom"
   	 }
}
  • 状态栏占位元素
    由于自定义导航栏,顶部状态栏会被页面覆盖,因此需要一个元素来占位
    元素高度计算如下:
uni.getSystemInfo({
	success: (res) => {
  		this.statusBarHeight = res.statusBarHeight
	}
})
  • 关于透明度的计算
onPageScroll (res) {
  // res.scrollTop 为页面滚动距离
  let top = res.scrollTop
  // height为状态栏高度+自定义导航栏标题内容高度(这里是44px)
  let height = this.statusBarHeight + 44
  // 判断滚动高度
  // 如果小于顶部自定义导航的高度
  if (top <= height) {
  	// 透明度(op) = 滚动距离/导航栏高度
  	//  (不做操作 直接计算 this.op = top/height 也可以实现)
  	// 由于(滚动距离/导航栏高度)得到0-1之间小数点后n位小数
  	// 四舍五入后得到的只能是0/1
    // 因此做如下操作
    this.op = Math.round(top/height*100)/100
  } else {
    // 如果滚动距离大于导航高度,则透明度值为1(不透明)
    this.op = 1
  }
}

  • 完整代码
<template>
  <view class="">
    <!-- S 自定义导航栏 -->
    <view
      class="nav"
      :style="'background-color: rgba(255, 255, 255,' + op +')'">
      <view class="status-bar" :style="'height:' + statusBarHeight + 'px'"></view>
      <view class="title" :style="'color: rgba(0, 0, 0,' + op +')'">
        XXXX
      </view>
    </view>
    <!-- E 自定义导航栏 -->
    
    <!-- S 页面内容区域 -->
    <view class="content"></view>
    <!-- E 页面内容区域 -->
  </view>
</template> 

<script>
  export default {
    data () {
      return {
        statusBarHeight: 0,
        op: 0
      }
    },
    onLoad () {
      uni.getSystemInfo({
        success: (res) => {
          this.statusBarHeight = res.statusBarHeight
        }
      })
    },
    onPageScroll (res) {
      let top = res.scrollTop
      let height = this.statusBarHeight + 44
      if (top <= height) {
        this.op = Math.round(top/height*100)/100
      } else {
        this.op = 1
      }
    },
  }
</script>

<style>
  .nav {
    position: fixed;
    top: 0rpx;
    left: 0;
    right: 0;
    background: #fff;
  }
  .content {
    height: 150vh;
    background: #f2f2f2;
  }
  .title {
    height: 88rpx;
    line-height: 88rpx;
    text-align: center;
  }
</style>


不喜勿喷!!!
不喜勿喷!!!
不喜勿喷!!!
重要的事情说三遍

<think> 根据用户的问题,用户想要在uniapp中将自定义导航栏固定在页面头部。结合提供的引用信息,以下是解决方案: 1. **使用uni-nav-bar组件**:引用[2]和[3]都提到了`uni-nav-bar`组件,这是uniapp中用于自定义导航栏的组件。 2. **固定导航栏**:引用[3]提供了关键方法,通过一个包裹`view`来设置导航栏的固定位置,并设置`position: fixed`样式。 3. **状态栏高度适配**:引用[3]中还提到了状态栏高度的设置,确保导航栏在不同设备上正确显示。 ### 具体步骤 #### 1. 封装导航栏组件 首先,我们可以封装一个导航栏组件,如引用[2]所示: ```html <uni-nav-bar left-icon="arrow-left" left-text="" title="卡面定制" backgroundColor="#fff" :border="false" color="#333333" @clickLeft="clickLeft"></uni-nav-bar> ``` #### 2. 在页面中固定导航栏 在需要固定导航栏页面,使用一个`view`包裹`uni-nav-bar`,并设置样式: ```html <template> <!-- 固定头部的导航栏 --> <view class="nav-bar" :style="{ height: statusBarHeight + 'px' }"> <uni-nav-bar title="我的" backgroundColor="transparent" :border="false" color="#000" left-icon="back" @clickLeft="back"></uni-nav-bar> </view> <!-- 页面内容 --> <view class="content"> <!-- 其他内容 --> </view> </template> ``` #### 3. 设置CSS样式 在样式中,将包裹导航栏的`view`设置为固定定位(fixed),并设置宽度为100%,同时确保页面内容不会与导航栏重叠: ```css .nav-bar { position: fixed; top: 0; left: 0; width: 100%; z-index: 1000; /* 确保导航栏在最顶层 */ } /* 内容区域需要设置上内边距,避免被导航栏遮挡 */ .content { padding-top: 状态栏高度 + 导航栏高度; /* 实际值需要根据实际情况计算,例如:如果状态栏高度为20px,导航栏高度为44px,则padding-top: 64px; */ } ``` #### 4. 获取状态栏高度 在`script`中获取状态栏高度(通常在`onLoad`生命周期中): ```javascript export default { data() { return { statusBarHeight: 0 } }, onLoad() { // 获取系统状态栏高度 this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight; } } ``` **注意**:如果页面滚动,内容区域需要设置`padding-top`,其值为状态栏高度加上导航栏本身的高度(通常导航栏高度为44px,具体根据设计而定)。例如,如果状态栏高度为20px,导航栏高度为44px,那么内容区域的`padding-top`应为64px。 ### 完整示例代码 ```html <template> <view> <!-- 固定头部的导航栏 --> <view class="nav-bar" :style="{ height: statusBarHeight + 44 + 'px' }"> <uni-nav-bar title="我的" backgroundColor="transparent" :border="false" color="#000" left-icon="back" @clickLeft="back"></uni-nav-bar> </view> <!-- 内容区域 --> <view class="content" :style="{ paddingTop: statusBarHeight + 44 + 'px' }"> <!-- 页面内容 --> </view> </view> </template> <script> export default { data() { return { statusBarHeight: 0 } }, onLoad() { const systemInfo = uni.getSystemInfoSync(); this.statusBarHeight = systemInfo.statusBarHeight; }, methods: { back() { uni.navigateBack(); } } } </script> <style> .nav-bar { position: fixed; top: 0; left: 0; width: 100%; z-index: 1000; } .content { width: 100%; box-sizing: border-box; } </style> ``` ### 说明 - 这里包裹导航栏的`view`的高度设置为`statusBarHeight + 44`,其中44是导航栏的典型高度(可以调整)。 - 内容区域的`padding-top`同样设置为`statusBarHeight + 44`,确保内容从导航栏下方开始显示。 通过以上方法,自定义导航栏将固定在页面头部,并且在页面滚动时不会移动[^3]。 §§相关问题§§ 1. 如何获取uniapp中的状态栏高度? 2. 如何自定义导航栏的样式(如背景色、标题颜色等)? 3. 在固定导航栏后,如何避免页面内容被导航栏遮挡? [^1]: uniapp自定义导航栏 [^2]: uniapp顶部自定义导航栏组件封装 [^3]: uniapp自定义头部导航栏页面整体滚动时固定在顶部 </think>在 UniApp 中将自定义导航栏固定在页面头部的方法如下,核心是通过 **CSS 固定定位** 和 **状态栏高度适配** 实现: ### 解决方案步骤 1. **包裹导航栏组件** 使用 `<view>` 包裹 `<uni-nav-bar>`,并设置固定定位样式: ```html <!-- 固定在头部的导航栏 --> <view class="fixed-nav" :style="{ paddingTop: statusBarHeight + 'px' }"> <uni-nav-bar title="我的页面" backgroundColor="#fff" :border="false" left-icon="back" @clickLeft="navigateBack"> </uni-nav-bar> </view> <!-- 内容区域 (需设置顶部内边距) --> <view class="content" :style="{ paddingTop: navTotalHeight + 'px' }"> <!-- 页面内容 --> </view> ``` 2. **设置关键 CSS 样式** ```css .fixed-nav { position: fixed; /* 固定定位 */ top: 0; left: 0; width: 100%; z-index: 9999; /* 确保在最顶层 */ } ``` 3. **动态计算高度 (JS)** 在页面脚本中获取状态栏高度并计算总高度: ```javascript export default { data() { return { statusBarHeight: 0, // 状态栏高度 navBarHeight: 44 // 导航栏默认高度(单位px) } }, computed: { navTotalHeight() { return this.statusBarHeight + this.navBarHeight; } }, onLoad() { // 获取系统状态栏高度 const systemInfo = uni.getSystemInfoSync(); this.statusBarHeight = systemInfo.statusBarHeight || 20; // 默认20px } } ``` ### 完整示例代码 ```html <template> <view> <!-- 固定导航栏 --> <view class="fixed-nav" :style="{ paddingTop: statusBarHeight + 'px' }"> <uni-nav-bar title="我的页面" backgroundColor="#fff" :border="false" left-icon="back" @clickLeft="navigateBack"> </uni-nav-bar> </view> <!-- 内容区域 --> <view class="content" :style="{ paddingTop: navTotalHeight + 'px' }"> <scroll-view scroll-y> <!-- 页面内容 --> <view v-for="i in 50" :key="i">内容项 {{ i }}</view> </scroll-view> </view> </view> </template> <script> export default { data() { return { statusBarHeight: 20, // 默认值 navBarHeight: 44 // 导航栏高度 } }, computed: { navTotalHeight() { return this.statusBarHeight + this.navBarHeight; } }, onLoad() { const systemInfo = uni.getSystemInfoSync(); this.statusBarHeight = systemInfo.statusBarHeight || 20; }, methods: { navigateBack() { uni.navigateBack(); } } } </script> <style> .fixed-nav { position: fixed; top: 0; left: 0; width: 100%; z-index: 9999; background-color: #fff; /* 与导航栏背景色一致 */ } .content { box-sizing: border-box; } </style> ``` ### 关键点说明 1. **双高度适配**: - `statusBarHeight`:适配刘海屏/异形屏 - `navBarHeight`:导航栏本身高度(默认44px) - 总高度 = 状态栏高度 + 导航栏高度 2. **定位原理**: - `position: fixed` 使导航栏脱离文档流 - `top:0; left:0` 定位到页面顶部 - `z-index` 确保覆盖其他内容 3. **内容区域处理**: 通过 `paddingTop` 预留导航栏空间,避免内容被遮挡 4. **多端兼容**: - 使用 `uni.getSystemInfoSync()` 获取的状态栏高度在 H5/小程序/App 均有效 - 透明背景需设置 `backgroundColor="transparent"`(引用[3]) > 实际效果:页面滚动导航栏始终固定在顶部,内容区域从导航栏下方开始显示[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值