温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

微信小程序怎么自定义导航

发布时间:2022-08-29 11:29:42 来源:亿速云 阅读:121 作者:iii 栏目:开发技术

这篇文章主要介绍“微信小程序怎么自定义导航”,在日常操作中,相信很多人在微信小程序怎么自定义导航问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”微信小程序怎么自定义导航”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

在app.js中获取状态栏信息和胶囊按钮信息

onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 获取系统信息
    this.globalData.systemInfo = wx.getSystemInfoSync();
    // 获取状态栏高度
    this.globalData.statusBarHeight = this.globalData.systemInfo.statusBarHeight
    // 胶囊按钮位置信息
    this.globalData.menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 获取导航栏高度
    this.globalData.navBarHeight = this.globalData.menuButtonInfo.bottom + (this.globalData.menuButtonInfo.top - this.globalData.statusBarHeight)
},
globalData: {
    statusBarHeight: '',
    menuButtonInfo: {},
    navBarHeight:'',
    systemInfo:''
  },

导航栏高度为胶囊底部位置+(胶囊顶部位置-状态栏高度)

将导航栏封装成组件

微信小程序怎么自定义导航

navigation-bar.js

properties: {
    // 是否显示返回箭头
    showBackArrow: {
        type: Boolean,
        value: true
    },
    // 是否自定义导航栏标题
    customTitle: {
        type: Boolean,
        value: false
    },
    // 导航栏标题
    title: {
        type: String,
        value: 'weixin'
    },
    // 是否自定义返回方法
    customBack: {
        type: Boolean,
        value: false
    }
},
data: {
    navBarHeight:getApp().globalData.navBarHeight,
    statusBarHeight:getApp().globalData.statusBarHeight,
    menuButtonInfo:getApp().globalData.menuButtonInfo
},
methods: {
    /**  点击返回按钮 */
    back() {
        if (this.data.customBack) {
            this.triggerEvent('back')
        } else {
            wx.navigateBack({
                delta: 0,
            })
        }
    },
    /** 点击导航栏标题事件 */
    clickTitle(){
        this.triggerEvent('clickTitle')
    },
}

navigation-bar.wxml

<view class="nav-bar" >
    <view ></view>
    <view  class="nav-box">
        <view class="back-arrow" wx:if="{{showBackArrow}}">
            <van-icon name="arrow-left" color="#262626" size="40rpx" bindtap="back"></van-icon>
        </view>
        <view class="nav-title" >
            <text wx:if="{{!customTitle}}" bindtap="clickTitle">{{title}}</text>
            <slot wx:if="{{customTitle}}"></slot>
        </view>
    </view>
</view>
<view ></view>

navigation-bar.wxss

.nav-bar{
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    background-color: #ffffff;
    z-index: 1000000;
}

.nav-box{
    padding: 0 20rpx;
    display: flex;
    align-items: center;
}

.back-arrow{
    width: 60rpx;
    height: 100%;
    display: flex;
    align-items: center;
    padding-top: 4rpx;
}

.nav-title{
    height: 100%;
    display: flex;
    align-items: center;
    font-size: 36rpx;
    color: #262626;
    font-weight: 600;
}

使用

app.js

"window": {
        "navigationStyle": "custom"
    },
    "usingComponents": {
        "navigation-bar":"/components/navigation-bar/navigation-bar",
    }

.wxml

<navigation-bar title="打卡" customBack bind:back="backWorkPage"></navigation-bar>

微信小程序怎么自定义导航

到此,关于“微信小程序怎么自定义导航”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI