Skip to content

mp-nav-bar 导航栏

支持大部分场景的顶部导航栏组件,强烈建议始终在项目使用

代码演示

由于组件的参数较多,以下仅演示部分重要的场景。

基本使用

html
<mp-nav-bar title="常见问题" />

不占位

html
<mp-nav-bar background-color="transparent" :placeholder="false" title="详情" />

滚动变化背景颜色

你可以通过 enable-scroll-change-background 属性开启滚动到某个阈值时更新背景颜色,默认阈值为 10px。如果需要修改阈值,可以使用 scroll-threshold 属性:

html
<mp-nav-bar
  background-color="transparent"
  enable-scroll-change-background
  scroll-background-color="#ff0000"
  :placeholder="false"
  title="详情"
/>

默认的滚动模式是在达到阈值后才开始改变颜色,但你也可以通过关闭 scroll-change-background-directly 属性来启用实时变化效果,根据当前滚动距离与阈值之间的百分比来计算透明度,使导航栏的背景颜色能实时从全透明到不透明变化:

html
<mp-nav-bar
  background-color="transparent"
  enable-scroll-change-background
  :placeholder="false"
  :scroll-change-background-directly="false"
  :scroll-threshold="44"
  title="详情"
/>

暂不支持在 webview 页面内使用 mp-nav-bar

当页面内使用 webview 组件时,不支持使用 mp-nav-bar,需将页面的 navigationStyle 设置为 default,启用默认 navbar

js
{
  "pages": {
    {
      "path": "pages/webview/index",
      "style": {
        "navigationStyle": "default"
      }
    },
  }
}

API

Props

Prop nameDescriptionTypeValuesDefault
fixed是否固定在顶部boolean-true
placeholder固定在顶部时是否开启占位boolean-true
titleAlign标题对齐方式stringleft, center, right'center'
mode主题模式
当模式为 auto 的时,会根据 backgroundColor 自动计算对应的主题模式
stringdark, light, auto'auto'
showIcon是否显示左侧 iconboolean-true
backIcon返回上一页图标,默认根据前景色切换string-''
homeIcon返回首页图标,默认根据前景色切换string-''
homePath首页路径string-'/pages/index/index'
leftText左侧文案string-''
leftTextColor左侧文案颜色,默认跟随主题色string-''
title标题string-''
scrollTitle滚动时标题string-''
titleFontSize标题文字大小number-34
titleColor标题文字颜色,默认跟随主题色string-''
backgroundColor背景颜色string-'#ffffff'
scrollBackgroundColor滚动时背景颜色string-'#fff'
underColor当导航栏背景颜色透明时,为确保前景色能够正常自动匹配,可以通过该属性传入页面顶部真实的背景色string-'#fff'
backgroundImage背景图片string-''
scrollBackgroundImage滚动时背景图片string-''
enableScrollChangeBackground是否开启监听滚动实时变更颜色boolean-false
scrollThreshold滚动阈值number-10
scrollChangeBackgroundDirectly用于设置滚动到指定阈值时如何改变背景颜色。若设置为 true,则在触达阈值时立即更新背景;若为 false,则在滚动过程中根据当前滚动距离与阈值的比例实时调整透明度boolean-true
logologo 图片string-''
logoStylelogo 样式object|string-() => ({
width: '80px',
})
backDelta返回上一页时返回到第几层number-1
fadeDuration动画时间number-300
fadeTimingFunc动画函数stringlinear, ease-in, ease-out, none'ease-out'
clickLeft点击左侧回调函数或 nullunion-null
clickTitleunion-null

Slots

NameDescriptionBindings
homeIconhome-icon 位置frontColor string - 前景色
backIconback-icon 位置frontColor string - 前景色
leftTextleft-text 位置frontColor string - 前景色
logologo 位置frontColor string - 前景色
titletitle 位置frontColor string - 前景色

源码

组件示例