小程序自定义并自适应顶部标题

文章介绍了如何在微信小程序中通过wxml、wxss和js实现顶部标题的自定义,以适应不同机型,如iphoneX。组件化的方法允许全局注册,通过获取系统信息和菜单按钮的布局来计算导航栏高度,确保显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

wxml:

<view class="nav" style="padding-top: {{ statusBarHeight }}px; height: {{ navBarHeight }}px;">
    <text>自定义标题</text>
</view>

wxss:

.nav {
    text-align: center;
}

.nav text {
    line-height: 72rpx;
    font-size: 36rpx;
    font-weight: 600;
    color: #2A2929;
}

js:使用顶部标题适配各大机型

Component({
    properties: {},
    data: {
        statusBarHeight: wx.getSystemInfoSync().statusBarHeight,
        navBarHeight: 0
    },
    methods: {
    },
    attached() {
        const wxMenu = wx.getMenuButtonBoundingClientRect()
        const statusBar = wx.getSystemInfoSync().statusBarHeight
        const navigationBar = (wxMenu.top - statusBar) * 2 + wxMenu.height
        this.setData({ navBarHeight: navigationBar })
    }
})

注册并使用:(我一般是全局注册)

"usingComponents": {
    "nav":"/components/nav/nav/nav"
  },

  在页面json文件中配置 "navigationStyle": "custom"

 {
  "usingComponents": {},
  "navigationStyle": "custom"
}

使用

<nav></nav>

 机型:iphoneX

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值