【微信小程序】之自定义顶部导航页签

article/2025/11/4 18:53:15

小程序系统提供的导航页签,只能设置字体,却不能自定义字体图片之类的,所以自己写了一个示例。

废话不多说,直接上代码
效果:
在这里插入图片描述
在这里插入图片描述

app.js

onLaunch: function() {wx.getSystemInfo({success: e => {this.globalData.StatusBar = e.statusBarHeight;let custom = wx.getMenuButtonBoundingClientRect();this.globalData.Custom = custom;this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;this.globalData.PageHeight = wx.getSystemInfoSync().windowHeight - this.globalData.CustomBar;}})
}

custom.js

如果你的组件无法使用公共样式,设置如下两个参数即可
addGlobalClass: true,
multipleSlots: true

const app = getApp();
Component({/*** 组件的一些选项*/options: {addGlobalClass: true,multipleSlots: true},/*** 组件的对外属性*/properties: {bgColor: {type: String,default: ''},isCustom: {type: [Boolean, String],default: false},isBack: {type: [Boolean, String],default: false},bgImage: {type: String,default: ''},isType: {type: String,default: ''}},/*** 组件的初始数据*/data: {StatusBar: app.globalData.StatusBar,CustomBar: app.globalData.CustomBar,Custom: app.globalData.Custom},/*** 组件的方法列表*/methods: {BackPage() {// wx.redirectTo({//   url: '/pages/main/home/home',// })wx.navigateBack({detal: 1})},toHome() {var pages = getCurrentPages();var currPage = pages[pages.length - 1]; //当前页面,引用组件的页面if (currPage.data.userType) {wx.reLaunch({url: '/pages/main/share/share',})} else {wx.showModal({title: '提示',content: '请您先授权再使用!',showCancel: false,confirmText: '确认',success: function(res) {if (res.confirm) {// 用户点击了确认} else if (res.cancel) {// 用户点击了取消}}});}}},attached: function() {},ready: function() {},pageLifetimes: {show() {}}
})

custom.json

{"component": true,"usingComponents": {}
}

custom.wxml

<view class="cu-custom" style="height:{{CustomBar}}px"><view class="cu-bar fixed {{bgImage!=''?'none-bg text-white bg-img':''}} {{bgColor}}" style="height:{{CustomBar}}px;padding-top:{{StatusBar}}px;{{bgImage?'background-image:url(' + bgImage+')':''}}"><view class="action" bindtap="BackPage" wx:if="{{isBack}}"><text class="icon-back"></text><slot name="backText"></slot></view><view class="action border-custom" wx:if="{{isCustom}}" style="width:{{Custom.width}}px;height:{{Custom.height}}px;margin-left:calc(750rpx - {{Custom.right}}px)"><text class="icon-back" bindtap="BackPage"></text><text class="icon-homefill" bindtap="toHome"></text></view><view class="content" style="top:{{StatusBar}}px"><slot name="content"></slot></view><slot name="right"></slot></view>
</view>

custom.wxss

page {width: 100%;height: 100%;background-color: #f1f1f1;background-color: #FFFFFF;font-size: 28rpx;color: #333;font-family: Helvetica Neue, Helvetica, sans-serif;
}.cu-custom {display: block;position: relative;z-index: 999;
}.cu-custom .cu-bar .content {width: calc(100% - 440rpx);
}.cu-custom .cu-bar {min-height: 0px;padding-right: 200rpx;box-shadow: 0rpx 0rpx 0rpx;
}.cu-custom .cu-bar .content image {height: 60rpx;width: 240rpx;/* height: 45rpx;width: 170rpx; */vertical-align: middle;
}.cu-custom .cu-bar .border-custom {position: relative;background: rgba(0, 0, 0, 0.15);border-radius: 1000rpx;height: 30px;
}.cu-custom .cu-bar .border-custom::after {content: " ";width: 200%;height: 200%;position: absolute;top: 0;left: 0;border-radius: inherit;transform: scale(0.5);transform-origin: 0 0;pointer-events: none;box-sizing: border-box;border: 1rpx solid #fff;opacity: 0.5;
}.cu-custom .cu-bar .border-custom::before {content: " ";width: 1rpx;height: 110%;position: absolute;top: 22.5%;left: 0;right: 0;margin: auto;transform: scale(0.5);transform-origin: 0 0;pointer-events: none;box-sizing: border-box;opacity: 0.6;background-color: #fff;
}.cu-custom .cu-bar .border-custom text {display: block;flex: 1;margin: auto !important;text-align: center;font-size: 34rpx;
}view, scroll-view, swiper, button, input, textarea, label, navigator, image {box-sizing: border-box;
}.text-bold {font-weight: bold;
}.bg-gradual-white {background-color: #fff;color: #000;
}.cu-bar {display: flex;position: relative;align-items: center;min-height: 100rpx;justify-content: space-between;
}.cu-bar.fixed, .nav.fixed {position: fixed;width: 100%;top: 0;z-index: 1024;box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0.1);
}.cu-bar .content {position: absolute;text-align: center;width: calc(100% - 340rpx);left: 0;right: 0;bottom: 0;top: 0;margin: auto;height: 60rpx;/* font-size: 32rpx; */font-size: 36rpx;line-height: 60rpx;cursor: none;pointer-events: none;text-overflow: ellipsis;white-space: nowrap;overflow: hidden;
}.cu-custom .cu-bar .content image {height: 60rpx;width: 240rpx;/* height: 45rpx;width: 170rpx; */vertical-align: middle;
}image {max-width: 100%;display: inline-block;position: relative;z-index: 0;
}

页面引用

app.json"usingComponents": {"custom": "/components/custom/custom"
}自己的页面wxml
<custom bgColor="bg-gradual-white text-bold" isBack="{{false}}" isType=""><view slot="content"><image class='title-img' src='/images/title.png'></image></view>
</custom>

供参考!


http://chatgpt.dhexx.cn/article/vUjp3iaK.shtml

相关文章

ABAP:多页签的选择屏幕

在程序中创建一个100屏幕&#xff0c;然后设定子屏幕区域&#xff0c;可通过屏幕号指定页签&#xff0c;让选择屏幕更多样式&#xff0c;效果如下图&#xff1a; 实现方式如下 定义不同的选择屏幕 * 基本条件屏幕 SELECTION-SCREEN BEGIN OF SCREEN 1100 AS SUBSCREEN. SELE…

【实战】1096- React 中后台系统多页签实现

在中后台管理类系统中&#xff0c;多页签的需求非常普遍&#xff0c;用户常常需要在多个页签内跳转&#xff0c;比如填写表单时去查询某个列表获取一些字段信息再回到表单页面填写。这样的需求在 Vue 中使用 keep-alive 即可实现&#xff0c;但是在 React 中&#xff0c;React …

多种方式带你玩转 javascript 实现关闭浏览器页签

你知道的越多&#xff0c;你不知道的越多 点赞再看&#xff0c;养成习惯 如果您有疑问或者见解&#xff0c;或者没有积分想获取项目&#xff0c;欢迎指教&#xff1a; 企鹅&#xff1a;869192208 文章目录 前言方法一方法二方法三方法四方法五方法六附录 前言 近日&#xff0c…

vue 后台系统中多页面标签

在后台开发中&#xff0c;常用一种页面标签工具&#xff0c;每次点击菜单栏时&#xff0c;会在页面区域上方增加一个【标签页】如下图&#xff0c;可关闭&#xff0c;可切换页面等功能&#xff0c;常见于后台管理系统中。 以前&#xff0c;我以为这个是利用tabs组件开发的&…

js关闭浏览器页签

兼容性 js实现 function closeWebPage(){if (navigator.userAgent.indexOf("MSIE") > 0) {if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {window.opener null;window.close();}else {window.open(, _top);window.top.close();}}else if (nav…

layui————一个页面展示两个页签

html页面 <!DOCTYPE html> <html> <head><meta charset"utf-8"><link rel"stylesheet" href"../../../build/css/base.css" media"all"> </head> <body> <div class"layui-tab la…

SAP BP屏幕增强页签

导语&#xff1a;最近收到了BP的需求&#xff0c;要增加页签&#xff0c;找了一些资料&#xff0c;发现BP的增强页签可是真麻烦啊&#xff0c;下面把我梳理出来的分享一下。 &#x1f449;【增强记录清单…】 需求&#xff1a; 需求是在供应商界面增加一个页签&#xff0c;用…

修改浏览器页签名称

第一种若是整个系统要统一修改为一个名称 在public文件夹下index.html下直接修改或者在相应配置文件package.json或者其他&#xff08;看项目配置&#xff09; 第二种某一个路由或者菜单页签不一样的名称 可以配置到后置路由中或者组件内 语句为:document.title 测试

vue实现tagsview多页签导航功能

文章目录 前言一、效果图二、实现思路1. 新建 tags-view.js2. 在Vuex里面引入 tags-view.js3. 新建 tabsView 组件4. 新建 ScrollPane 组件5. 引入 tabsView 组件6. 使用 keep-alive 组件&#xff0c;进行页签的缓存 总结 前言 基本上后台管理系统都需要有多页签的功能&#x…

基于微前端qiankun的多页签缓存方案实践

作者&#xff1a;vivo 互联网前端团队- Tang Xiao 本文梳理了基于阿里开源微前端框架qiankun&#xff0c;实现多页签及子应用缓存的方案&#xff0c;同时还类比了多个不同方案之间的区别及优劣势&#xff0c;为使用微前端进行多页签开发的同学&#xff0c;提供一些参考。 一、…

Sublime Text的命令行工具subl

在sublime的安装目录下有个subl.exe&#xff0c;是sublime编辑器为用户提供的命令行工具。 修改Windows系统的环境变量&#xff0c;将sublime的安装路径添加到环境变量里&#xff1b; 打开win的命令行提示符程序&#xff0c;输入subl -version,看到结果如下图所示&#xff1a;…

Macbook Pro下安装subl命令,快速使用sublime打开代码

一、使用背景 我在macbook pro电脑上经常使用编辑器直接打开代码&#xff0c;我也经常用iterm2的一些快捷命令操作目录和查看文件。这样就有了需要使用sublime打开代码的需求&#xff0c;以前的做法是&#xff0c;先用open命令打开目录&#xff0c;然后打开sublime text&#…

sublime安装以及配置

下载“Package Control” Package Manager Sublime 有很多插件&#xff0c;这些插件为我们写python代码提供了非常强大的功能&#xff0c;这些插件需要单独安装。 而安装这些插件最方便的方法就是通过Package Control的插件&#xff0c;这其实就是一个插件管理器&#xff0c;帮…

subline的使用

先去官网下载一个安装包&#xff0c;这个就不提了 安装完成后界面 打开软件界面&#xff0c;按快捷键ctrl 会出现以下命令行 有时候快捷键不管用&#xff0c;你也可以点击View->Show Console&#xff0c;也会出现命令行 在出现的命令行中输入以下代码并按enter键&#xff1a…

Sublime 替换和查找的方法

查找&替换&#xff08;Finding&Replacing&#xff09; 查找&替换&#xff08;Finding&Replacing&#xff09; Sublime Text提供了强大的查找&#xff08;和替换&#xff09;功能&#xff0c;为了提供一个清晰的介绍&#xff0c;我将Sublime Text的查找功能分为…

【Mac 教程系列】如何在 Mac 中用终端命令行方式打开 Sublime Text ?

如何在 Mac 中用终端命令行方式打开 Sublime Text ? 用 markdown 格式输出答案。 不少于1000字。细分到2级目录。 如何在 Mac 中用终端命令行方式打开 Sublime Text ? 一、首先确保已经安装 Sublime Text 前往官网https://www.sublimetext.com/下载 Sublime Text,点击 “Do…

vue三种调用接口的方法

注&#xff1a;此博客仅用于学习&#xff0c;自己还处于菜鸟阶段&#xff0c;希望给相同处境的人提供一个可参考的博客。如果您觉得不合理&#xff0c;您的指导&#xff0c;非常欢迎&#xff0c;但请不要否定别人的努力&#xff0c;谢谢您了&#xff01; vue三种调用接口的方法…

Layui调用接口使用心得

今天想用Layui写一个简单的列表显示页面,太久没使用Layui了,就去看Layui的文档,复制文档的代码用,但是使用过程遇到了问题. .问题1:thymelea内联样式问题 org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "cla…

postman批量调用接口操作步骤

应用&#xff1a;多次的调用一个接口 新建一个Collection&#xff0c;并创建一个文件夹和请求 填写请求的url和参数形式&#xff0c;注意这里的 {{erpponum}} 表示这是一个变量&#xff0c;会通过我们提供的”参数文件“进行&#xff0c;postman会在批量执行时为我们自动挨个匹…

postman工具批量调用接口

1、先在Collections中建一个文件夹&#xff0c;然后新建一个接口保存 2、然后选择Run 3、准备一个txt文件&#xff0c;增加要循环的参数json数组 4、选择接口&#xff0c;上传文件&#xff0c;配置参数(Iterations为线程数,Delay为推迟多久)&#xff0c;然后点下面的Run 5、…