微信小程序,app端自定义导航栏动态设置高度

第一种方法:

定义:


<script>
    import Vue from 'vue'
    export default {
        onLaunch: function() {
            uni.getSystemInfo({
                success: function(e) {
                    // #ifndef MP
                    Vue.prototype.StatusBar = e.statusBarHeight;
                    if (e.platform == 'android') {
                        Vue.prototype.CustomBar = e.statusBarHeight + 50;
                    } else {
                        Vue.prototype.CustomBar = e.statusBarHeight + 45;
                    };
                    // #endif

                    // #ifdef MP-WEIXIN || MP-QQ
                    Vue.prototype.StatusBar = e.statusBarHeight;
                    let capsule = wx.getMenuButtonBoundingClientRect();
                    if (capsule) {
                        Vue.prototype.Custom = capsule;
                        // Vue.prototype.capsuleSafe = uni.upx2px(750) - capsule.left + uni.upx2px(750) - capsule.right;
                        Vue.prototype.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
                    } else {
                        Vue.prototype.CustomBar = e.statusBarHeight + 50;
                    }
                    // #endif        

                    // #ifdef MP-ALIPAY
                    Vue.prototype.StatusBar = e.statusBarHeight;
                    Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
                    // #endif
                }
            })
        },
        onShow: function() {
            console.log('App Show')
        },
        onHide: function() {
            console.log('App Hide')
        }
    }
</script>

<style>
    /* 引入colorUI css */
    @import "colorui/main.css";
    @import "colorui/icon.css";
    /*每个页面公共css */

    page {}
</style>

//使用


<template>
    <view>
        <view class="cu-custom" :style="[{height:CustomBar + 'px'}]">
            <view class="cu-bar fixed" :style="style" :class="[bgImage!=''?'none-bg text-white bg-img':'',bgColor]">
                <view class="action" @tap="BackPage" v-if="isBack">
                    <text class="cuIcon-back"></text>
                    <slot name="backText"></slot>
                </view>
                <view class="content" :style="[{top:StatusBar + 'px'}]">
                    <slot name="content"></slot>
                </view>
                <slot name="right"></slot>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                StatusBar: this.StatusBar,
                CustomBar: this.CustomBar
            };
        },
        name: 'cu-custom',
        computed: {
            style() {
                var StatusBar= this.StatusBar;
                var CustomBar= this.CustomBar;
                var bgImage = this.bgImage;
                var style = `height:${CustomBar}px;padding-top:${StatusBar}px;`;
                if (this.bgImage) {
                    style = `${style}background-image:url(${bgImage});`;
                }
                return style
            }
        },
        props: {
            bgColor: {
                type: String,
                default: ''
            },
            isBack: {
                type: [Boolean, String],
                default: false
            },
            bgImage: {
                type: String,
                default: ''
            },
            backPagePath: {
                type: String,
                default: ''
            }
        },
        methods: {
            BackPage() {
                if(this.backPagePath) {
                    uni.redirectTo({
                        url: this.backPagePath
                    });
                } else {
                    uni.navigateBack({
                        delta: 1
                    });
                }
            }
        }
    }
</script>

<style>

</style>
第二种方法

<!-- 顶部组件 -->
        <u-navbar :is-back="false" :background="searchBarBg" :border-bottom="false" id="nav-bar">
            <view class="top u-flex">
                <image src="../../../static/navi_title_v6.png" class="mini-logo"></image>
                <view class="search-box u-flex" :style="{ height: menuHeight + 'px' }" @click="search()">
                    <image src="../../../static/icon/search.png"></image>

<!--搜索框中的词动画效果-->
                    <swiper :indicator-dots="false" autoplay :interval="3000" :duration="200" vertical circular>
                        <swiper-item class="u-flex" v-for="(item, index) in searchHotWords" :key="index">
                            <text>{{ item.queryName }}</text>
                        </swiper-item>
                    </swiper>
                </view>
            </view>
        </u-navbar>


<script>

// #ifndef H5
let menuInfo = uni.getMenuButtonBoundingClientRect()
// #endif

data(){

return{

// #ifndef H5
  menuHeight: menuInfo.height
   // #endif

}

}

</script>

导航栏下的tab栏,动态获取与上面的距离

<!-- tab栏 -->
        <view class="tabs" :style="{ top: navBarHeight + 'px' }" id="tabs">
            <u-tabs
                :list="tabList"
                :is-scroll="false"
                :current="tabCurrent"
                :duration="0.2"
                :gutter="80"
                
                name="title"
                font-size="32"
                bg-color="#f2b739"
                active-color="#fff"
                inactive-color="#fcf4e2"
                :bar-style="{ height: '8rpx' }"
                @change="changeTabs"
            ></u-tabs>
        </view>

data(){

return{

navBarHeight: 0,

}

}

methods:{

getViewHeight() {  //动态获取
            uni.createSelectorQuery()
                .in(this)
                .select('#nav-bar')
                .boundingClientRect(data => {
                    this.navBarHeight = data.height
                    uni.createSelectorQuery()
                        .in(this)
                        .select('#tabs')
                        .boundingClientRect(data => {
                            this.tabsHeight = data.height
                            uni.getSystemInfo({
                                success: res => {
                                    this.loadingHeight = res.windowHeight - this.navBarHeight - this.tabsHeight
                                }
                            })
                        })
                        .exec()
                })
                .exec()
        },

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中可以通过自定义导航实现更加个性化的界面设计。以下是一个简单的实现步骤: 1. 在 app.json 文件中设置 "navigationStyle": "custom",表示使用自定义导航。 2. 在页面的 WXML 文件中编写自定义导航的 HTML 代码,例如: ``` <view class="navbar"> <view class="navbar-title">自定义导航</view> </view> ``` 其中,navbar 表示导航的样式,navbar-title 表示标题的样式,可以根据需要进行自定义。 3. 在页面的 wxss 文件中设置导航的样式,例如: ``` .navbar { height: 44px; background-color: #fff; border-bottom: 1px solid #ccc; display: flex; align-items: center; justify-content: center; } .navbar-title { font-size: 18px; font-weight: bold; color: #000; } ``` 其中,height 表示导航高度,background-color 表示导航的背景颜色,border-bottom 表示导航底部的边框线,display、align-items 和 justify-content 表示导航标题的布局方式,navbar-title 表示标题的样式,可以根据需要进行自定义。 4. 在页面的 JS 文件中设置导航的返回按钮,例如: ``` wx.showModal({ title: '提示', content: '确认返回上一页?', success: function (res) { if (res.confirm) { wx.navigateBack({ delta: 1 }) } } }) ``` 其中,wx.showModal 表示显示一个模态框,title 表示模态框的标题,content 表示模态框的内容,success 表示点击确定按钮后执行的回调函数,wx.navigateBack 表示返回上一页。 通过以上步骤,就可以实现微信小程序的自定义导航

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值