<template>
<view class="">
<view
class="u-navbar"
:style="[navbarStyle]"
:class="{ 'u-navbar-fixed': isFixed, 'u-border-bottom': borderBottom }"
>
<view
class="u-status-bar"
:style="{ height: statusBarHeight + 'px' }"
></view>
<view class="u-navbar-inner" :style="[navbarInnerStyle]">
<view class="u-back-wrap" v-if="isBack" @tap="goBack">
<view class="u-icon-wrap back-icon-wrap">
<view class="back-home" v-if="backHome"> </view>
<view class="back-icon" v-else> </view>
</view>
<view
class="u-icon-wrap u-back-text u-line-1"
v-if="backText"
:style="[backTextStyle]"
>{{ backText }}</view
>
</view>
<view class="u-navbar-content-title" v-if="title" :style="[titleStyle]">
<view
class="u-title u-line-1"
:style="{
color: titleColor,
fontSize: titleSize + 'rpx',
}"
>
{{ title }}
</view>
</view>
<view class="u-slot-content"><slot></slot></view>
<view class="u-slot-right"><slot name="right"></slot></view>
</view>
</view>
<view
class="u-navbar-placeholder"
v-if="isFixed"
:style="{
width: '100%',
height: Number(navbarHeight) + statusBarHeight + 'px',
}"
></view>
</view>
</template>
<script>
let systemInfo = uni.getSystemInfoSync();
let menuButtonInfo = {};
menuButtonInfo = uni.getMenuButtonBoundingClientRect();
export default {
name: "u-navbar",
props: {
height: {
type: [String, Number],
default: uni.upx2px(146 / 3.64),
},
backIconColor: {
type: String,
default: "#606266",
},
backIconName: {
type: String,
default: "arrow-left",
},
backIconSize: {
type: [String, Number],
default: "30",
},
backText: {
type: String,
default: "",
},
backTextStyle: {
type: Object,
default() {
return {
color: "#606266",
};
},
},
title: {
type: String,
default: "",
},
titleWidth: {
type: [String, Number],
default: "250",
},
titleColor: {
type: String,
default: "rgb(0, 0, 0)",
},
titleSize: {
type: [String, Number],
default: 14.83,
},
isBack: {
type: [Boolean, String],
default: true,
},
backHome: {
type: [Boolean, String],
default: false,
},
background: {
type: Object,
default() {
return {
"font-weight": "bold",
};
},
},
isFixed: {
type: Boolean,
default: true,
},
borderBottom: {
type: Boolean,
default: false,
},
zIndex: {
type: [String, Number],
default: "",
},
customBack: {
type: Function,
default: null,
},
},
data() {
return {
menuButtonInfo: menuButtonInfo,
statusBarHeight: systemInfo.statusBarHeight,
};
},
computed: {
navbarInnerStyle() {
let style = {};
style.height = this.navbarHeight + "px";
let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
style.marginRight = rightButtonWidth + "px";
return style;
},
navbarStyle() {
let style = {};
style.zIndex = this.zIndex ? this.zIndex : this.$u.zIndex.navbar;
Object.assign(style, this.background);
return style;
},
titleStyle() {
let style = {};
style.left =
(systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + "px";
style.right =
(systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + "px";
let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
style.left =
(systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + "px";
style.right =
rightButtonWidth -
(systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 +
rightButtonWidth +
"px";
style.width = uni.upx2px(this.titleWidth) + "px";
return style;
},
navbarHeight() {
return this.height ? this.height : 44;
let height = systemInfo.platform == "ios" ? 44 : 48;
return this.height ? this.height : height;
},
},
created() {},
methods: {
goBack() {
if (typeof this.customBack === "function") {
this.customBack();
} else {
uni.navigateBack();
window.history.back();
}
},
},
};
</script>
<style scoped lang="scss">
@import "../../libs/css/style.components.scss";
.u-navbar {
width: 100%;
}
.u-navbar-fixed {
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 991;
}
.u-status-bar {
width: 100%;
}
.u-back-wrap {
display: flex;
align-items: center;
width:20%;
margin-left: rpx(30);
}
.u-back-text {
padding-left: 4rpx;
font-size: 30rpx;
}
.u-navbar-content-title {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
position: absolute;
height: inherit;
line-height: inherit;
left: 0;
right: 0;
text-align: center;
flex-shrink: 0;
}
.u-navbar-centent-slot {
flex: 1;
}
.u-title {
font-size: rpx(52);
flex: 1;
}
.u-navbar-right {
flex: 1;
display: flex;
align-items: center;
justify-content: flex-end;
}
.u-slot-content {
flex: 1;
display: flex;
align-items: center;
}
.back-icon {
width: rpx(96);
height: rpx(96);
border-width: rpx(14);
border-style: solid;
border-image-slice: 14 fill;
margin: 0 auto;
border-image-source: url("@/static/base-module/common/fanhui.png");
}
.back-home{
width: rpx(96);
height: rpx(96);
border-width: rpx(0);
border-style: solid;
border-image-slice: 0 fill;
margin: auto auto auto rpx(61);
border-image-source: url("@/static/base-module/common/shouye.png");
}
.back-icon-wrap {
width: rpx(207);
height: rpx(100);
display: flex;
border-width: rpx(2);
border-style: solid;
border-image-slice: 2 fill;
border-image-source: url("@/static/base-module/common/fanhui-bg.png");
&:active {
border-image-source: url("@/static/base-module/common/fanhui-actice-bg.png");
}
}
.u-border-bottom{
border-bottom: rpx(2) solid #fafafa;
}
</style>