uniapp 自制头部左侧胶囊组件
// 组件页面
<template>
<view class="navigation-bar" :style="{height:navBarH}">
<view class="navigation-bar-fixed" :style="{height:navBarH,background:(!config.transparent&&!config.linear)?config.bgcolor:'transparent'}">
<view v-if="[1,2].indexOf(config.type)!= -1" :class="config.type==1?'navigation-bar-capsule':'navigation-bar-textbox'"
:style="{
top:marTop,
height:capsuleH,
lineHeight:capsuleH,
borderRadius:capsuleBr,
width:width
}">
<view class="button-action" hover-class="button-action-active" @click="back_">
<image :src="backIcon.icon" mode="">image>
view>
<view class="button-v-line">view>
<view v-if="!config.share" class="button-action" hover-class="button-action-active" @click="home_">
<image :src="homeIcon.icon" mode="">image>
view>
<view v-if="config.share" class="button-action" hover-class="button-action-active">
<button class="button_clear" open-type="share" hover-class="none">
<image :src="shareIcon.icon" mode="">image>
button>
view>
view>
<view v-if="[3,4].indexOf(config.type)!= -1" @click="conduct_" :class="config.type == 3?'navigation-bar-custom-capsule':'navigation-bar-custom-textbox'"
:style="{
top:marTop,
backgroundColor :BGcolor,
border:BGborder,
height:capsuleH,
lineHeight:capsuleH,
borderRadius:capsuleBr
}">
<image v-if="config.menuIcon" class="custom-icon" :src="config.menuIcon">image>
<text class="custom-describe" style="color: #ff4200;">{{config.menuText}}text>
view>
<view class="navigation-bar-title" :style="'margin-top:'+titTop+';color:'+config.fontcolor">{{config.title}}view>
<view v-if="config.linear" class="navigation-bar-linear" :style="{height:navBarH,background:config.bgcolor,opacity:scrollTop/scrollMaxHeight}">view>
view>
<view :style="{height:navBarH}">view>
view>
template>
<script>
export default {
data() {
return {
backIcon: {
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+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A2weH/SFawES6P1BtT2uADGOs4/BqWgCjwBjaAN9AZDwDSwAIwHraAmGgZkgGugrzh3BxSA2+3c00Cq+NaGcevUR9qAwaCC+1nJL5AHDvOXditPoxGF1J4vEYtXUWQJuB4vmXgXw6OSFjDXwOIvgf6RMCAdbBHHm8FdAz/ifFAL1jFfhM0AFec7UVeOeEwzwe1gEmuhr7QJn1HJ7ii4BYaGw4AeYLk4LtBw2JEMlXsYvZIDXKtC7EOw3a1/ujVA7fVu1Gv4BtxICigHAzUTnI9vxJgBs4R+XmMxm8AA6ipp5YN+TGo2TmlmWF5vMoyOFnqlhgHLhL4b7PvH9arIWwNemvaBdKHXaSY9W84GuO4jWA1m6yxex4AUoTdqPF867M92rimloxc5AoOH5z2x3lIuYo3klHPMD2UmDPgi9D4RMCqZCc5icxSyAS+EPjwCBowQa6s3YUC1I9m4lVahf9O4b7LQ75swoELoCzUWspcV6xFN51/sCK0hG3BF/IIqJ2S7vM8LeoENGosfJ/b9b3DRhAFN4LQ49gVTObqUnUK/DN6aCqP7RSzPZhNvWnKIxZxQaLKcVpHogDg+CLIMLl6V0SfF8QnwwHRHVshy124vrzL9hyqjwE36i8VyIi8cLWULe+B3ohEpZ9UZrE+o2dA9Vql2n51juZzaBVNKPGd53SBqlkOsUqdrPCeTxhexYbKLunmcLYV1LqS+YIpjsDWVLaE92Kpk5fqe4TCVs6As5pKJjmc+48CrNhKDLXufTuKkQjX4ncXQK0PjOWqudJzNTlO4xyr+fCKf9ZFaxFeNe3+AU2AME11TMAswNZ1+xbHIVvbPM7m10hhZOoIP4A14ZP0dr38K9YtN/z+g3kAJiYgENMDfXzqxJol/KRMGJAyIc/kjwADgq6x0iVWy2QAAAABJRU5ErkJggg=="
},
navBarH: '',
marTop: '',
titTop: '',
capsuleH: '',
capsuleBr: '',
left: '',
width: ''
};
},
computed: {
},
created() {
var menuButtonInfo = uni.getMenuButtonBoundingClientRect()
var systeminfo = uni.getSystemInfoSync()
this.capsuleH = menuButtonInfo.height + 'px',
this.capsuleBr = (menuButtonInfo.height) / 2 + 'px',
this.width = menuButtonInfo.width + 'px'
this.left = systeminfo.screenWidth - menuButtonInfo.right + 'px'
if (systeminfo.system.indexOf('Android') !== -1) {
this.navBarH = 48 + systeminfo.statusBarHeight + 'px'
this.marTop = systeminfo.statusBarHeight + (48 - menuButtonInfo.height) / 2 + 'px'
this.titTop = systeminfo.statusBarHeight + 24 - 16 + 'px'
} else if (systeminfo.system.indexOf('iOS') !== -1) {
this.navBarH = 44 + systeminfo.statusBarHeight + 'px'
this.marTop = systeminfo.statusBarHeight + (44 - menuButtonInfo.height) / 2 + 'px'
this.titTop = systeminfo.statusBarHeight + 22 - 16 + 'px'
} else {
this.navBarH = 44 + 20 + 'px'
this.marTop = 20 -1 + (44 - menuButtonInfo.height) / 2 + 'px'
this.titTop = 20 + 22 - 16 + 'px'
this.capsuleH = menuButtonInfo.height - 2 + 'px'
this.capsuleBr = (menuButtonInfo.height - 2) / 2 + 'px'
this.width = menuButtonInfo.width + 'px'
}
this.navBarH = systeminfo.navigationBarHeight + systeminfo.statusBarHeight + 'px'
this.marTop = systeminfo.statusBarHeight + (systeminfo.navigationBarHeight - menuButtonInfo.height) / 2 + 'px'
this.titTop = systeminfo.statusBarHeight + systeminfo.navigationBarHeight / 2 - 16 + 'px'
this.navBarH = systeminfo.statusBarHeight + systeminfo.titleBarHeight + 'px'
this.marTop = systeminfo.statusBarHeight + (systeminfo.titleBarHeight - menuButtonInfo.height) / 2 + 'px'
this.titTop = systeminfo.statusBarHeight + systeminfo.titleBarHeight / 2 - 16 + 'px'
},
updated() {
console.log(this.config)
},
mounted() {
console.log(this.config)
},
props: {
BGcolor: {
type: String,
default: 'rgba(255,255,255,.5)'
},
BGborder: {
type: String,
default: 'none'
},
config: {
type: Object,
default () {
return {
title: "",
bgcolor: "",
type: 2,
linear: false,
transparent: false,
fontcolor: "#222",
menuIcon: "",
menuText: ""
}
}
},
scrollTop: {
type: Number,
default: 0
},
scrollMaxHeight: {
type: Number,
default: 288
}
},
methods: {
back_() {
uni.navigateBack({
delta: 1
});
},
home_() {
uni.switchTab({
url: "/pages/tabBar/home/home"
})
},
conduct_() {
this.$emit("customConduct")
}
}
}
script>
<style lang="scss">
.navigation-bar {
box-sizing: border-box;
.navigation-bar-fixed {
width: 100%;
position: fixed;
top: 0px;
box-sizing: border-box;
z-index: 999;
background: #fff;
display: flex;
justify-content: center;
.navigation-bar-linear {
width: 100%;
position: absolute;
box-sizing: border-box;
z-index: -1;
}
.navigation-bar-title {
width: 280rpx;
line-height: 32px;
height: 32px;
color: #fff;
font-size: 16px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
text-align: center;
}
.navigation-bar-custom-capsule {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
height: 32px;
left: 10px;
padding: 0px 10px;
// background-color: rgba(255,255,255,.5);
border: .5px solid #eaeaea;
border-radius: 16px;
// padding: 0 23upx;
// border:0.5px solid rgba(255,255,255,.3);
box-sizing: border-box;
overflow: hidden;
z-index: 9;
.custom-icon {
width: 24px;
height: 24px;
}
.custom-describe {
font-size: 14px;
}
}
.navigation-bar-custom-textbox {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
height: 32px;
left: 0px;
padding: 0 23upx;
// border:0.5px solid rgba(255,255,255,.3);
box-sizing: border-box;
overflow: hidden;
z-index: 9;
.custom-icon {
width: 24px;
height: 24px;
}
.custom-describe {
font-size: 32upx;
}
}
.navigation-bar-textbox {
position: absolute;
// height: 32px;
display: flex;
align-items: center;
justify-content: space-between;
line-height: 32px;
height: 32px;
font-size: 30upx;
width: 85px;
left: 0px;
// border:0.5px solid rgba(255,255,255,.3);
box-sizing: border-box;
overflow: hidden;
z-index: 9;
.button-action {
flex: 1;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
.button_clear {
line-height: 1;
background-color: transparent;
border: none;
margin: 0;
padding: 0;
}
.button_clear::after {
border: none;
}
image {
width: 24px;
height: 24px;
// padding: 6upx;
box-sizing: border-box;
}
}
.button-v-line {
width: 1rpx;
height: 16rpx;
background-color: rgba(0, 0, 0, .2);
}
}
.navigation-bar-capsule {
position: absolute;
display: flex;
align-items: center;
justify-content: space-between;
line-height: 32px;
height: 32px;
font-size: 30rpx;
left: 10px;
border: .5px solid #ececec;
background-color: rgba(255, 255, 255, .5);
border-radius: 16px;
// border:0.5px solid rgba(255,255,255,.3);
box-sizing: border-box;
overflow: hidden;
z-index: 9;
.button-action {
flex: 1;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
image {
width: 24px;
height: 24px;
box-sizing: border-box;
}
.button_clear {
line-height: 1;
background-color: transparent;
border: none;
margin: 0;
padding: 0;
text-decoration: none;
}
.button_clear::after {
border: none;
}
}
.button-action-active {
background-color: rgba(0, 0, 0, .3)
}
.button-v-line {
width: 0.5px;
height: 18px;
background-color: rgba(0, 0, 0, .2);
}
}
}
}
style>
// 页面
<template>
<view class="content">
<navigation-custom :config="config" :scrollTop="scrollTop" @customConduct="customConduct" :scrollMaxHeight="scrollMaxHeight" />
view>
template>
<script>
import navigationCustom from '@/components/my-navigation/my-navigation.vue'
export default {
components: {
navigationCustom
},
data() {
return {
config: {
title: "自定义标题",
bgcolor: "#ff4200",
type: 1,
transparent: true,
linear: true,
share: true,
},
scrollTop: 0,
scrollMaxHeight: 200
}
},
onLoad() {
if(getCurrentPages().length === 1){
this.config.type = 4
this.config.menuIcon = ''
}else{
this.config.type = 1
};
},
onPageScroll(e) {
this.scrollTop = e.scrollTop;
},
methods: {
customConduct() {
console.log('自定义点击事件')
},
}
}
script>
<style>
style>
<script>
export default {
globalData: {
menuButtonInfo:'',
systeminfo:''
},
onLaunch: function() {
this.globalData.menuButtonInfo = uni.getMenuButtonBoundingClientRect()
this.globalData.systeminfo = uni.getSystemInfoSync()
console.log('App Launch')
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
}
}
</script>