首页 > 其他分享 >uniapp自定义导航栏

uniapp自定义导航栏

时间:2024-03-11 15:34:56浏览次数:15  
标签:uniapp 自定义 navbar systemInfo height common nVal 导航 wx

uniapp项目自定义顶部导航栏

1.创建组件完整代码navigation.vue

<template>
    <view class="navbar-header" :style="isfixed?'padding-top'+navHeight+'px':'padding-top:0px'">
        <view class="navbar custom-class" :class="{'fixed-top':isfixed}"
            :style="{height:navHeight+'px',backgroundColor:bgColor}">
            <view class="navbar-action-wrap"
                :style="{top:navTop+'px',height:titleHeight+'px',lineHeight:titleHeight+'px'}"
                v-if="showBack || showHome">
                <view class="navbar-back" style="margin-right:20rpx" @click="navBack" v-if="showBack">
                    <u-icon name="arrow-left" size="40rpx" color="#000"></u-icon>
                </view>
                <view class="navbar-home" @click="toIndex" v-if="showHome">
                    <image :src="common.getViewImgUrl('/image/icon-add-back.png')" mode="widthFix"></image>
                </view>
            </view>
            <view class="navbar-title"
                :style="{top:navTop+'px',height:titleHeight+'px',lineHeight:titleHeight+'px'}">
                {{pageName}}
            </view>
        </view>
    </view>
</template>

<script>
    const app = getApp();
    import common from "@/utils/common";
    export default {
        name: 'navigation',
        options: {
            styleIsolation: 'apply-shared'
        },
        props: {
            pageName: {
                type: String,
                value: null
            },
            showBack: {
                type: Boolean,
                value: true
            },
            showHome: {
                type: Boolean,
                value: true
            },
            isfixed: {
                type: Boolean,
                value: true
            },
            bgColor: {
                type: String,
                value: "#fff"
            },
            isbacklogin: {
                type: Boolean,
                value: false
            },
        },
        watch: {
            show(nVal, oVal) { //是否显示弹窗
                this.isShow = nVal;
                if (nVal) {
                    this.queryGroupNotice();
                }
            },
            isfixed(nVal, oVal) { //是否固定
                this.isShow = nVal;
                if (nVal) {
                    this.queryGroupNotice();
                }
            },
        },
        data() {
            return {
                common: common,
                systemInfo: null, //设备信息
                navHeight: null, //导航栏高度
                navTop: null, //导航栏顶部距离
                titleHeight: 32, //标题高度
            }
        },
        created() {
            this.init();
        },
        methods: {
            init() {
                var systemInfo = app.globalData.systemInfo;
                this.systemInfo = systemInfo;
                this.navHeight = systemInfo.navHeight;
                this.navTop = systemInfo.navTop;
                if (systemInfo.Custom) {
                    this.titleHeight = systemInfo.Custom.height;
                }
            },
            navBack: function() { //回退
                const url = wx.getStorageSync('local_url');
                if (this.isbacklogin) {
                    wx.reLaunch({
                        url: '/pages/login/login'
                    });
                } else {
                    wx.navigateBack({
                        delta: 1
                    })
                }
            },
            toIndex: function() { //回主页
                //返回
                const source = wx.getStorageSync('iSSourceNc');
                const curUrl = common.getCurrentPageUrl();
                console.log(curUrl);
                if (source && curUrl.indexOf("pages/group/memberList") == '-1') {
                    wx.removeStorageSync('iSSourceNc');
                    wx.exitMiniProgram({
                        success: (res) => {}
                    })
                } else {
                    common.getWxBack();
                }
            },
            toSelectGroup: function() { //回主页
                this.$emit('toSelectGroup');
            },
        }
    }
</script>

<style>
    .navbar-header {
        overflow: hidden;
        width: 100%;
    }

    .fixed-top {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 10;
    }

    .navbar {
        width: 100%;
        min-height: 100rpx;
        overflow: hidden;
        flex-shrink: 0;
        position: relative;
    }

    .navbar-title {
        width: 100%;
        box-sizing: border-box;
        padding-left: 120rpx;
        padding-right: 120rpx;
        height: 64rpx;
        line-height: 64rpx;
        text-align: center;
        position: absolute;
        left: 0;
        z-index: 10;
        color: #333;
        font-size: 32rpx;
        font-weight: bold;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
    }

    .navbar-action-wrap {
        display: -webkit-flex;
        display: flex;
        -webkit-box-align: center;
        -ms-flex-align: center;
        -webkit-align-items: center;
        align-items: center;
        position: absolute;
        left: 20rpx;
        z-index: 11;
        line-height: 1;
        padding-top: 8rpx;
        padding-bottom: 8rpx;
    }

    .navbar-home {
        width: 40rpx;
        height: 40rpx;
    }

    .navbar-home image {
        display: block;
        width: 100%;
    }

    .navi-inimg {
        margin-left: 10rpx;
        display: inline-block;
        width: 36rpx;
        vertical-align: middle;
    }

    .navi-inimg image {
        margin-top: -1rpx;
    }
</style>

2.pages.json 配置自定义

"pages": [{
    "path": "home",
    "style": {
        "navigationStyle": "custom"
    }
}]

3.页面使用导航栏组件

<template>
  <view>
        <navigation :isfixed="false" :show-back="false" :show-home="false" page-name="标题"></navigation>
  </view>
</template>
<script>
    import navigation from '@/components/navigation';
    export default {
    components: {
      navigation,
    },
}
</script>    

 

标签:uniapp,自定义,navbar,systemInfo,height,common,nVal,导航,wx
From: https://www.cnblogs.com/czhowe/p/18066168

相关文章

  • jsPlumb导航器
    开源项目地址:https://gitee.com/easyxaf/jsplumb-navigator前言jsPlumb可用于连接DOM元素,它不依赖框架,所以与主流框架都可以无缝的集成。但比较遗憾的是社区版中没有平移、缩放等功能,如果用它来开发工作流等项目,用户体验会大打折扣。我的项目是用Blazor开发的,在比较了多款开源流......
  • RAG实战5-自定义prompt
    RAG实战5-自定义prompt在阅读本文之前,先阅读RAG实战4。在RAG实战4中我们分析了LlamaIndex中RAG的执行过程,同时留下了一个尚待解决的问题:LlamaIndex中提供的prompttemplate都是英文的,该如何使用中文的prompttemplate呢?直接看以下代码:importloggingimportsysimporttorchf......
  • 自定义事件提醒程序
    因为各大软件无法实现每隔自定义固定天数进行事件提醒的功能,在此用Python写了一个简易的Windows端事件提醒弹窗程序。注意没有做界面,只是把事件写死在了程序中,设置为开机自启动,挂在后台一直运行(cpu占用率几乎为0%),到点就会进行弹窗提醒。Step0.环境配置建议新建一个环境,后面用......
  • github 搭建个人导航网
    最近搭建了个个人的导航网,具体内容见下图,欢迎大家访问吖,点我访问 (首次访问较慢) 具体实现是使用vue3编写,白嫖github的page部署首先在github上创建一个仓库:name.github.io#name是你github的名字然后在本地创建一个vue3项目 然后把刚创建的仓库clone到......
  • vue项目引入自定义svg
    图标可以使用element-ui的图标库、第三方的图标库或者引入svg使用,这里是讲如何使用自定义的svg。将SVG图标放入项目 自定义的svg可以访问 https://www.iconfont.cn地址,搜索你想要的图标,下载SVG格式,放入项目的src/assets/icons/svg文件夹中。并在src/assets/icons/index.js......
  • uniapp清除指定key缓存
    清除缓存onLaunch:function(){console.log('AppLaunch')constpreservedKeys=['ishowFixPre','readTime']try{const{keys}=uni.getStorageInfoSync()keys.forEach((key)=>......
  • 向TreeView添加自定义信息
    可在Windows窗体TreeView控件中创建派生节点,或在ListView控件中创建派生项。通过派生可添加任何所需字段,以及添加处理这些字段的自定义方法和构造函数。此功能的用途之一是将Customer对象附加到每个树节点或列表项。虽然此处的示例是关于TreeView控件的,但该方法同样......
  • Mysql和Oracle自定义函数区别
    1.Mysql自定义函数dropfunctionifexistsget_date;createfunctionget_date(v_datetimedatetime,v_timevarchar(50))returnsvarchar(50)begin declarev_datevarchar(50); declarecDatevarchar(50); declarebeginDatevarchar(50); setcDate=date_format(......
  • 关于uniapp调用支付宝登录问题
    uniapp的uni.login的provider不支持支付宝但是呢,我看插件市场都要钱这个还需要钱吗你直接硬写呗,支付宝比如登录官方时这么写的ok 我们在uniapp上也这么写就行了button调用一键登录uniapp不会报错,然后我们发行打包到支付宝的开发工具里经测试可以正常使用 返回 ......
  • Mysql自定义函数报错合集
    参考:Mysql自定义函数报错解决方法1.在MySql中创建自定义函数报错信息如下:1.1错误显示ERROR1418(HY000):ThisfunctionhasnoneofDETERMINISTIC,NOSQL,orREADSSQLDATAinitsdeclarationandbinaryloggingisenabled(youmightwanttousethelesssafel......