微信小程序自定義頭部導(dǎo)航欄(組件化)
本文實(shí)例為大家分享了微信小程序自定義頭部導(dǎo)航欄的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
支持 導(dǎo)航欄自定義背景顏色、背景圖片 支持返回文字自定義 支持導(dǎo)航標(biāo)題自定義
首先在app.json window配置項(xiàng)添加
"window": { "navigationStyle": "custom" }
自定義頭部導(dǎo)航欄代碼
wxml部分
<view class="cu-custom" style="height:{{CustomBar}}px"> <view class="cu-bar {{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="{{!isShare && isBack}}"> <text class="icon-back"></text> <slot name="backText"></slot> </view> <view class="action border-custom" wx:if="{{isShare && isBack}}" 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> </view> </view>
wxss部分
.cu-custom { display: block; position: relative; } .cu-custom .cu-bar {display: flex; align-items: center; justify-content: space-between;position: fixed; width: 100%; top: 0; min-height: 100rpx; padding-right: 220rpx; box-shadow: 0rpx 0rpx 0rpx; z-index: 9999; background-color: #fff; } .cu-custom .cu-bar .border-custom { position: relative; background: rgba(255,255,255,0.5); border-radius: 1000rpx; height: 30px; } .cu-custom .cu-bar .action { display: flex; align-items: center; height: 100%; justify-content: center; max-width: 100%; } .cu-custom .cu-bar .action:first-child { margin-left: 30rpx; font-size: 30rpx; } .cu-custom .cu-bar .border-custom text { display: block; flex: 1; margin: auto !important; text-align: center; font-size: 34rpx; } .cu-bar .content { width: calc(100% - 440rpx);position: absolute; text-align: center; left: 0; right: 0; bottom: 0; top: 0; margin: auto; height: 60rpx; font-size: 32rpx; line-height: 60rpx; cursor: none; pointer-events: none; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; }
js部分
const App = getApp(); Component({ options: { addGlobalClass: true, multipleSlots: true }, properties: { isBack: { type: [Boolean, String], default: false }, bgColor: { // 類名 type: String, default: '' }, bgImage: { // 僅支持網(wǎng)絡(luò)地址 type: String, default: '' }, }, data: { StatusBar: App.GlobalData.StatusBar, CustomBar: App.GlobalData.CustomBar, Custom: App.GlobalData.Custom, isShare: App.GlobalData.share }, methods: { BackPage() { wx.navigateBack({ delta: 1 }); }, toHome() { wx.reLaunch({ url: '/pages/index/index', }) } } })
app.js 判斷是否 分享
App({ GlobalData: { share: false, StatusBar: 0, Custom: 0, CustomBar: 0 }, onLaunch: function () { let self = this let systemInfo = wx.getSystemInfoSync() self.GlobalData.StatusBar = systemInfo.statusBarHeight; if (wx.getMenuButtonBoundingClientRect) { let custom = wx.getMenuButtonBoundingClientRect(); self.GlobalData.Custom = custom; self.GlobalData.CustomBar = custom.bottom + custom.top - systemInfo.statusBarHeight; } })
因?yàn)榻M件css里不支持偽類元素 所以寫在 app.css
/* 自定義頭部導(dǎo)航欄圖標(biāo) */ .cu-custom [class*="icon-"] { font-family: "iconfont" !important; font-size: inherit; font-style: normal; } @font-face {font-family: "iconfont"; src: url('iconfont.eot?t=1555316650045'); /* IE9 */ src: url('iconfont.eot?t=1555316650045#iefix') format('embedded-opentype'), /* IE6-IE8 */ url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAANcAAsAAAAAB0wAAAMPAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDBgqCLIIKATYCJAMMCwgABCAFhG0HPxtZBsiemjyKBSigJhHwFACGePCEutd3On3HX0oBgBzCrQwwpxvBCDR2tgVzWmSLL1RJEo0V68AF6Bsg4wdCz0+7oQxuhxBSCRhLPW1+Ay6n/1+JzwPKbaxNY9OcFGCCAY21KZAHLpAEvWHsApdwHAI4amtJBg5+es1UsFjjBJDsDFXIVHImbEu6ECIENUtthuwgQWg2mhJge/h9+UP7QjAkFGvq2LvImf37vq6IVzDTJxDP5wDYKlCgJbAgs2tdU+XjaEuK0xQFPR8CLIKZKxDHhCJdEdh/eGAQkAArNRWApihm4DtXlKqHkrtOVQCCOGmmDDksXcWxtJFWjZOLo+97m+5UXPh55OpD782Kq9UuPmzkdeV+7zfvxfjzj1scPi7qNz+ucfhU3wPOmRUvOjpd+cy5TU+qGnfuXXVF21Tfd7hK2s/5zWcrLfo6uLj0lMky3G7tAf3+Nh96cqTdkZKAvt/mtErvN3SVSr9JXhz7eKT/52f5r37kwxXngld/KlVqW7XKmv39DxVVnratPbW3o0HNScOZw0H1BrtuNR/Q7DBquG5Ukw5bDkb76+2PNh8q15yu+yL1wrlpU887+4VpH7/nSzFXBoi/7WDNJAO6xrz4+7/BNMr8n9Pu+T/0FoAPzb0UyvrHQgww8b+umB4AdjCXWhG1QIvG9vWjTBg5yQAOB8TQV9638ZpKeioIIZVSGFKoBSWkIWlZLZHA0xEBIX3gaGHIak8FhfKITQLNrQUQyroOQ2meQynrNWlZX5Ggsn8IKJvCMbkKW3oal+EGFEIIINP8lDIkdhTHGtSi9GeooSV4WWnIfofn6wFq17ptMXELB94cM4oidVEcKBR5bNNgcBosiynfYwNSMEkPguuyTZuo7k2TBWxnagzgAUEAZGS+FMkgYQ7Fbx+olT7/DFQhi8BrqasJvgOPT++ctNNa2x7krcrpVXcvrxSJqBaKBRQk4mE2Mhg4wLKjjOTXDzKAJDCRPiJ8tdQGtYr6qibra+wPKGRNWHeqGFGx+KJ0DlPY7MEpR/McLdTnAAA=') format('woff2'), url('iconfont.woff?t=1555316650045') format('woff'), url('iconfont.ttf?t=1555316650045') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */ url('iconfont.svg?t=1555316650045#iconfont') format('svg'); /* iOS 4.1- */ } .iconfont { font-family: "iconfont" !important;font-size: 16px;font-style: normal;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale; } .icon-homefill:before { content: "\e6fc"; } .icon-back:before { content: "\e67a"; } .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 rgba(0,0,0,0.3); 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: rgba(0,0,0,0.3); }
開發(fā)工具顯示不正常 主要以真機(jī)顯示為主 具體可根據(jù)自己的需求修改
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中for of和for in的區(qū)別詳解
對(duì)于初學(xué)者,我們或許只知道無論是for of還是for in他們都有一個(gè)功能那就是遍歷,至于具體的細(xì)節(jié)或許我們不是很清楚,那么接下來我們就來詳細(xì)的區(qū)分一下for of和for in他們之間的不同點(diǎn)和相同點(diǎn),需要的朋友可以參考下2023-06-06基于JavaScript實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建表格和增加表格行數(shù)
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建表格和增加表格行數(shù)的相關(guān)資料,需要的朋友可以參考下2015-12-12javascript模板方法模式和職責(zé)鏈模式實(shí)例分析
這篇文章主要介紹了javascript模板方法模式和職責(zé)鏈模式,結(jié)合實(shí)例形式較為詳細(xì)的分析了模板方法模式和職責(zé)鏈模式基本原理、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2023-07-07js仿百度登錄頁實(shí)現(xiàn)拖動(dòng)窗口效果
這篇文章主要為大家詳細(xì)介紹了js仿百度登錄頁實(shí)現(xiàn)拖動(dòng)窗口效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03使用JavaScript實(shí)現(xiàn)響應(yīng)式計(jì)數(shù)器動(dòng)畫
在本文中,我們將學(xué)習(xí)如何使用?HTML?CSS?和?JavaScript創(chuàng)建響應(yīng)式計(jì)數(shù)器動(dòng)畫。?我們?cè)诤芏嗟胤蕉伎梢杂茫热邕m用于不同類型的個(gè)人網(wǎng)站、企業(yè)網(wǎng)站等,感興趣的可以了解一下2022-08-08JavaScript canvas實(shí)現(xiàn)七彩時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)七彩時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05JavaScript中實(shí)現(xiàn)塊作用域的方法
在Javascript中由于沒有作用域的概念,所以很容易發(fā)生標(biāo)識(shí)符名稱的沖突,尤其是在比較大的項(xiàng)目中,這類情況更容易發(fā)生2010-04-04JavaScript 懸浮窗口實(shí)現(xiàn)代碼
主要是window.onscroll的運(yùn)用2009-02-02