uniapp實現小程序自定義頭部組件


前言:小程序的頂部一般是原生控制的,類似下圖

 但類似電商,游戲類等小程序為求更加美觀,往往會自定義頭部展示,比如

功能實現:要自定義頭部樣式,得先配置全局屬性>>"navigationStyle": "custom",

 

 頭部組件:小程序自定義頭部的高度=狀態欄高度+導航欄高度,其中狀態欄高度不同手機規格會不同,需要適配,導航欄高度則是統一的44px,代碼如下

  1 <template>
  2     <view class="bar-sticky">
  3         <view class="status-line" :style="{height: lineHeight, background: navigationBarStyle.background || normal.background}"></view>
  4         <view class="bar-line" :style="{background: navigationBarStyle.background || normal.background}">
  5             <view class="bar-line container-in" :style="{background: navigationBarStyle.background || normal.background}">
  6                 <view class="bar-font bar-content" v-if="!custom">
  7                     <view class="icon-fanhui bar-back" :style="{color: navigationBarStyle.iconColor || normal.iconColor}" @click="$turnPage('1', 'navigateBack')"
  8                      v-if="showBack">
  9                     </view>
 10                     <view class="bar-title" :style="{color: navigationBarStyle.fontColor || normal.fontColor}">{{navigationBarStyle.iconText}}</view>
 11                 </view>
 12                 <view class="bar-font container-in" v-else>
 13                     <slot></slot>
 14                 </view>
 15             </view>
 16         </view>
 17     </view>
 18 </template>
 19 
 20 <script>
 21     export default {
 22         props: {
 23             custom: {
 24                 type: Boolean,
 25                 default: false
 26             }, //自定義頭部,否則沿用原生的頭部樣式
 27             navigationBarStyle: {
 28                 type: Object,
 29                 default: function() {
 30                     return {
 31                         background: '#FFFFFF',
 32                         fontColor: '#000000',
 33                         iconColor: '#000000',
 34                         iconText: '一米一粉' //導航欄文字
 35                     }
 36                 }
 37             }, //原生頭部自定義樣式
 38             showBack: {
 39                 type: Boolean,
 40                 default: true
 41             }, //是否顯示回退圖標,默認顯示
 42         },
 43         data() {
 44             return {
 45                 normal: {
 46                     background: '#FFFFFF',
 47                     fontColor: '#000000',
 48                     iconColor: '#000000',
 49                 }, //公用樣式,個性化樣式可通過傳值實現
 50                 lineHeight: '' //狀態欄高度
 51             };
 52         },
 53         mounted() {
 54             const systemInfo = uni.getSystemInfoSync();
 55             // px轉換到rpx的比例
 56             let pxToRpxScale = 750 / systemInfo.windowWidth;
 57             let systems = {
 58                 ktxStatusHeight: systemInfo.statusBarHeight * pxToRpxScale, // 狀態欄的高度
 59                 navigationHeight: 44 * pxToRpxScale, // 導航欄的高度
 60                 ktxWindowWidth: systemInfo.windowWidth * pxToRpxScale, // window的寬度
 61                 ktxWindowHeight: systemInfo.windowHeight * pxToRpxScale, // window的高度
 62                 ktxScreentHeight: systemInfo.screenHeight * pxToRpxScale, // 屏幕的高度
 63             }
 64             // 底部tabBar的高度
 65             systems.tabBarHeight = systems.ktxScreentHeight - systems.ktxStatusHeight - systems.navigationHeight - systems.ktxWindowHeight // 底部tabBar的高度
 66             this.lineHeight = systems.ktxStatusHeight + 'rpx';
 67             this.$emit('getHeight', systems)
 68         }
 69     }
 70 </script>
 71 
 72 <style lang="scss">
 73     /*正中*/
 74     .bar-content {
 75         width: 100%;
 76         display: flex;
 77         flex-direction: column;
 78         align-items: center;
 79         justify-content: center;
 80     }
 81 
 82     //導航欄吸頂效果
 83     .bar-sticky {
 84         position: sticky;
 85         position: -webkit-sticky;
 86         top: 0;
 87         z-index: 101;
 88     }
 89 
 90     /*垂直居中*/
 91     .container-in {
 92         display: flex;
 93         -webkit-align-items: center;
 94         align-items: center;
 95         width: 100%;
 96         height: 44px;
 97     }
 98 
 99     .bar-line {
100         height: 44px; //導航欄高度
101 
102         .bar-back {
103             font-size: 52rpx !important;
104             position: absolute;
105             left: 30rpx;
106         }
107 
108         .bar-title {
109             font-size: 32rpx;
110         }
111     }
112 </style>
View Code

組件引用,如果想實現自定義的樣式,可以在規定高度內寫點自己的代碼

<view class="bar-sticky">
  <navigationBar custom="true">
    //自己的代碼
  </navigationBar>
</view>

如果還是跟原來的樣式一樣,就寫

 

<view class="bar-sticky">
     <navigationBar :navigationBarStyle="navigationBarStyle" :showBack="false"></navigationBar>
</view>

//設置導航欄樣式
navigationBarStyle: {
  iconText: '類目' //導航欄文字
},

注意:自定義的頭部組件需要固定在頂部,所以需要全局樣式,sticky的妙用有興趣可以留意一下

.bar-sticky {
    position: sticky;
    position: -webkit-sticky;
    top: 0;
    z-index: 101;
}        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM