微信小程序自定義膠囊樣式
本文實(shí)例為大家分享了微信小程序自定義左上角膠囊樣式的具體代碼,供大家參考,具體內(nèi)容如下
1、 將app.js 中的 window 對象屬性navigationStyle 改為自定義
"window": { "navigationStyle": "custom" },
改完之后的效果:
2、獲取 右上角膠囊的定位信息 設(shè)置
調(diào)用 wx.getMenuButtonBoundingClientRect() 函數(shù)得到右上角膠囊定位信息
所需要的 屬性有 : top,height屬性,用于計(jì)算自定義左上角膠囊定位的位置
拿到 右上角膠囊的 top和height 相加得到 屏幕導(dǎo)航欄的固定高度:
在 data函數(shù)中聲明一個(gè)導(dǎo)航欄高度屬性,和一個(gè) 膠囊具體定位的top屬性:
賦值導(dǎo)航欄的高度 數(shù)據(jù):
// pages/testQ/index.js Page({ /** * 頁面的初始數(shù)據(jù) */ data: { navHeight:0, capsuleTop: 0 }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { }, /** * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面顯示 */ onShow: function () { let dwObj = wx.getMenuButtonBoundingClientRect() let navHeight_ = (dwObj.top + dwObj.height) let capsuleTop_ = dwObj.top this.setData( { navHeight: navHeight_, capsuleTop:capsuleTop_ } ) }, /** * 生命周期函數(shù)--監(jiān)聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數(shù)--監(jiān)聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom: function () { }, /** * 用戶點(diǎn)擊右上角分享 */ onShareAppMessage: function () { } })
在 wxml 中定義 導(dǎo)航欄:
<!--pages/testQ/index.wxml--> <!-- 左上角膠囊開始--> <!--left-capsule 是最上層,可以設(shè)置背景--> <view class="left-capsule"> <!--left-capsule-nav 是用于定位左上角的位置--> <view class="left-capsule-nav" style="height:{{navHeight}}px;"> <!--left-capsule-nav-content 是 膠囊主要內(nèi)容--> <view style="position:relative;top:{{capsuleTop}}px;" class="left-capsule-nav-content"> <!--back 膠囊 返回按鈕--> <view class="back"> <!-- 我這個(gè)圖標(biāo)引入的是 vant庫的icon,如果不是使用vant的話 得自定義一個(gè)icon--> <van-icon name="arrow-left" color="white" size="20"/> </view> <!-- line 膠囊 中間線條--> <view class="line"></view> <!-- home 膠囊 返回首頁按鈕--> <view class="home"> <!-- 我這個(gè)圖標(biāo)引入的是 vant庫的icon,如果不是使用vant的話 得自定義一個(gè)icon--> <van-icon name="wap-home-o" color="white" size="20"/> </view> </view> </view> <!-- 以上 可以 封裝成自定義組件,在引入,這個(gè)地方是 膠囊外的內(nèi)容--> <view class="main-content" style="top:{{navHeight}}px;"> 我是測試左上角膠囊 </view> <!-- 左上角膠囊結(jié)束--> </view>
wxss內(nèi)容:
/* 導(dǎo)航欄css開始*/ .left-capsule{ width: 100vh; height: 100vh; background-color: black; } .left-capsule .left-capsule-nav{ width: 100%; position: fixed; z-index: 2; } .left-capsule-nav .left-capsule-nav-content{ width: 85px; text-align: center; border-radius: 50px; position: relative; top: 26px; left: 20px; box-shadow:0px 0px 1px 0.2px white; background-color: #1d19195c; height: 30px; } .left-capsule-nav-content view{ display: inline; width: 35px; position: relative; } .left-capsule-nav-content .back{ top: 4px;left: -5px; } .left-capsule-nav-content .line{ top: 3px; width: 1px; border-left: solid #cac3c3 thin; height: 17px; display: inline-block; } .left-capsule-nav-content .home{ top: 4px; } /* 導(dǎo)航欄css結(jié)束*/ /* 內(nèi)容*/ .main-content{ background-color: red; position: absolute; width: 100%; z-index: 1; }
效果圖:
如果覺得紅色地方太挨得進(jìn)的話 top 在加大一點(diǎn)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js實(shí)現(xiàn)點(diǎn)擊每個(gè)li節(jié)點(diǎn),都彈出其文本值及修改
本篇文章主要分享了js實(shí)現(xiàn)點(diǎn)擊每個(gè)li節(jié)點(diǎn),都彈出其文本值及修改的實(shí)例代碼,具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12webpack5搭建一個(gè)簡易的react腳手架項(xiàng)目實(shí)踐
本文文章主要介紹了webpack5搭建一個(gè)簡易的react腳手架項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Javascript 判斷兩個(gè)IP是否在同一網(wǎng)段實(shí)例代碼
javascript功能的強(qiáng)大之處是我們大家有目共睹的,幾天小編向大家介紹Javascript 判斷兩個(gè)IP是否在同一網(wǎng)段,感興趣的朋友可以參考一下2016-11-11前端報(bào)錯(cuò)Failed?to?resolve?component:?smile-outlined?If?thi
這篇文章主要為大家介紹了前端報(bào)錯(cuò)?Failed?to?resolve?component:?smile-outlined?If?this?is?a?native?custom?的問題分析解決,有需要的朋友可以借鑒參考下2023-06-06