微信小程序自定義頂部導(dǎo)航組件
本文實(shí)例為大家分享了微信小程序自定義頂部導(dǎo)航組件,供大家參考,具體內(nèi)容如下
在components中新建文件夾navbar
components/navbar.wxml
<!--components/navbar.wxml-->
<view class="navbar" style="height:{{navHeight+5}}px;background-image:url('{{imgUrl}}') ">
? <!-- 左上角 返回按鈕 和 home按鈕 wx:if="{{showNav}}" 是控制左上角按鈕的顯示隱藏,首頁不顯示 -->
? <view class="navbar_left" style="top:{{navTop}}px;height:{{jnheight-1}}px;width:{{jnwidth-3}}px" wx:if="{{showNav}}">
? ? <!-- 控制返回按鈕的顯示 -->
? ? <view class="navBack" bindtap="navBack">
? ? ? <image src="/images/back.png" mode="widthFix"></image>
? ? </view>
? ? <!-- home按鈕 wx:if="{{showHome}}" 是控制左上角 home按鈕的顯示隱藏-->
? ? <view class="nav_line" bindtap="navHome" wx:if="{{showHome}}">
? ? ? <image src="/images/index.png" mode="widthFix" style="width:50%"></image>
? ? </view>
? </view>
? <!-- 中間標(biāo)題 -->
? <view class="navbar_title" style="top:{{navTop}}px;">{{pageName}}</view>
</view>components/navbar.js
const App = getApp();
Component({
? // 組件的屬性列表
? properties: {
? ? pageName: String, //中間的title
? ? showNav: { //判斷是否顯示左上角的按鈕 ? ?
? ? ? type: Boolean,
? ? ? value: true
? ? },
? ? showHome: { //判斷是否顯示左上角的home按鈕
? ? ? type: Boolean,
? ? ? value: true
? ? },
? ? imgUrl:{//圖片背景路徑
? ? ? type: String,
? ? ? value: App.globalData.imgLink+'index.jpg',
? ? },
? },
? // 組件的初始數(shù)據(jù)
? data: {
? ? showNav: true, //判斷是否顯示左上角的home按鈕
? ? showHome: true, //判斷是否顯示左上角的按鈕
? },
? lifetimes: {
? ? // 生命周期函數(shù),可以為函數(shù),或一個(gè)在methods段中定義的方法名
? ? attached: function() {
? ? ? this.setData({
? ? ? ? navHeight: App.globalData.navHeight, //導(dǎo)航欄高度
? ? ? ? navTop: App.globalData.navTop, //膠囊按鈕與頂部的距離
? ? ? ? jnheight: App.globalData.jnheight, //膠囊高度
? ? ? ? jnwidth: App.globalData.jnwidth //膠囊寬度
? ? ? })
? ? }
? },
? // 組件的方法列表
? methods: {
? ? //回退
? ? navBack: function() {
? ? ? wx.navigateBack()
? ? },
? ? //回主頁
? ? navHome: function() {
? ? ? wx.switchTab({
? ? ? ? url: '/pages/index/index'
? ? ? })
? ? },
? }
})components/navbar.wxss
/* components/navbar.wxss */
.navbar {
? width: 100%;
? overflow: hidden;
? position: sticky;
? top: 0;
? left: 0;
? z-index: 10;
? flex-shrink: 0;
? background: #fff;
?
}
.navbar_left {
? display: -webkit-flex;
? display: flex;
? -webkit-box-align: center;
? -ms-flex-align: center;
? -webkit-align-items: center;
? align-items: center;
? position: absolute;
? left: 10px;
? z-index: 11;
? line-height: 1;
? border: 1rpx solid #f0f0f0;
? border-radius: 40rpx;
? overflow: hidden;
? background: rgba(255, 255, 255, 0.6);
}
.navBack image{
? width: 60%;
}
?
.navbar_left view {
? width: 50%;
? display: flex;
? align-items: center;
? justify-content: center;
}
?
.nav_line {
? border-left: 1rpx solid #f0f0f0;
}
?
.navbar_title {
? width: 100%;
? box-sizing: border-box;
? padding-left: 115px;
? padding-right: 115px;
? height: 32px;
? line-height: 32px;
? text-align: center;
? position: absolute;
? left: 0;
? z-index: 10;
? color: #333;
? font-size: 16px;
? font-weight: bold;
? text-overflow: ellipsis;
? overflow: hidden;
? white-space: nowrap;
}在公共組件app.js中獲取導(dǎo)航高度等信息
// app.js
App({
? onLaunch() {
? ? //設(shè)置導(dǎo)航欄
? ? //獲取菜單按鈕的布局位置信息
? ? let menuButtonObject = wx.getMenuButtonBoundingClientRect();
? ? //獲取系統(tǒng)信息
? ? wx.getSystemInfo({
? ? ? success: res => {
? ? ? ? console.log('xxxx', res)
? ? ? ? //狀態(tài)欄的高度
? ? ? ? let statusBarHeight = res.statusBarHeight,
? ? ? ? ? //膠囊按鈕與頂部的距離
? ? ? ? ? navTop = menuButtonObject.top,
? ? ? ? ? navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
? ? ? ? let globalData = this.globalData;
? ? ? ? globalData.navHeight = navHeight;//導(dǎo)航欄高度
? ? ? ? globalData.navTop = navTop;//膠囊按鈕與頂部的距離
? ? ? ? globalData.jnheight = menuButtonObject.height;//膠囊的高度
? ? ? ? globalData.jnwidth = menuButtonObject.width;//膠囊的寬度
? ? ? ? globalData.screenHeight = res.screenHeight;//屏幕高度
? ? ? },
? ? ? fail(err) {
? ? ? ? console.log(err);
? ? ? }
? ? });
? },
? //設(shè)置全局對象
? globalData: {
? ? navHeight: 0,
? ? navTop: 0,
? ? jnheight: 0,
? ? jnwidth: 0,
? ? screenHeight: 0,
? }
})在app.json中設(shè)置導(dǎo)航自定義,去除默認(rèn)的導(dǎo)航 “navigationStyle”: "custom"
?"window": {
? ? "backgroundTextStyle": "light",
? ? "navigationBarBackgroundColor": "#fff",
? ? "navigationBarTitleText": "Weixin",
? ? "navigationBarTextStyle": "black",
? ? "navigationStyle": "custom"
? },引用到pages中的文件里
json文件中引用組件****
{
? "usingComponents":{
? ? "navbar":"/components/navbar/navbar"
? }
}html文件中
<navbar page-name="{{navbar.shoppingName}}"></navbar>js文件中
Page({
? data:{
? ? navbar:{
? ? ? shoppingName:'首頁',
? ? },
? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript實(shí)現(xiàn)延時(shí)顯示提示框特效代碼
本文給大家分享的是javascript通過setTimeout實(shí)現(xiàn)延時(shí)顯示提示框的特效代碼,效果非常棒,這里推薦給大家2016-04-04
javascript設(shè)計(jì)模式 – 工廠模式原理與應(yīng)用實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 工廠模式,結(jié)合實(shí)例形式分析了javascript工廠模式基本概念、原理、定義、應(yīng)用場景及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
基于JavaScript實(shí)現(xiàn)移動(dòng)端TAB觸屏切換效果
我們使用移動(dòng)端時(shí)可以通過觸屏手勢左右滑動(dòng)來切換TAB欄目,如網(wǎng)易新聞等APP欄目切換。我們說的TAB一般由導(dǎo)航條和TAB對應(yīng)的內(nèi)容組成,切換導(dǎo)航條上的標(biāo)簽同時(shí)標(biāo)簽對應(yīng)的內(nèi)容也會(huì)跟著切換。本文將結(jié)合實(shí)例給大家介紹一個(gè)移動(dòng)端TAB觸屏切換效果。2015-10-10
深入淺出ES6新特性之函數(shù)默認(rèn)參數(shù)和箭頭函數(shù)
js簡單實(shí)現(xiàn)讓文本框內(nèi)容逐個(gè)字的顯示出來

