123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- <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-icon-wrap">
- <u-icon :name="backIconName" :color="backIconColor" :size="backIconSize"></u-icon>
- </view> -->
- <view class="u-back-wrap" v-if="isBack" @tap="goBack">
- <view class="u-icon-wrap">
- <u-icon :name="backIconName" :color="backIconColor" :size="backIconSize"></u-icon>
- </view>
- <!-- <view class="u-icon-wrap u-back-text u-line-1" v-if="backText" :style="[backTextStyle]">
- {{ backText }}</view> -->
- </view>
- <div :class="[isSearchStatus?'search active':'search']" v-if="isShowSearch">
- <image class="searchBtn" src="../../static/search.png" @click="startSearch" mode=""></image>
- <u-input v-if="isSearchStatus" placeholder="请输入内容" prefixIcon="search" :value="searchKeywords"
- @input="searchInputhandle" prefixIconStyle="font-size: 22px;color: #909399,"></u-input>
- <!-- <image v-if="isSearchStatus" @click="clearHandle" class="clearBtn" src="../../static/clearIcon.png" mode=""></image> -->
- <div v-if="isSearchStatus" @click="cancelSearchHandle" class="cancelSearchBtn">取消</div>
- </div>
- <div v-if="!isSearchStatus" class="navTitle">
- <text v-if="titleMod == 'Normal'">{{title}}</text>
- <view v-if="titleMod == 'Filter'" class="filterTitle" @click="selecterHandle">
- <text>{{filterStr}}</text>
- <image src="../../static/close-icon.png" mode=""></image>
- </view>
- </div>
- <view class="navNoticeIcon">
- <image v-if="messageStatus" class="navNoticeIcon"
- :src="`../../static/message-${messageStatus?'unread':'read'}.png`" mode=""
- @click="checkMessageHandle"></image>
- </view>
- </view>
- </view>
- <!-- 解决fixed定位后导航栏塌陷的问题 -->
- <view class="u-navbar-placeholder" v-if="isFixed && !immersive"
- :style="{ width: '100%', height: Number(navbarHeight) + statusBarHeight + 'px' }"></view>
- <uni-popup ref="popup" type="top" :maskClick="true">
- <view class="selectableList">
- <scroll-view scroll-y="true" class="listWrap">
- <view :class="getClass(v.text)" v-for="(v,i) in filterKeyList" :key="i" @click="listClickHandle(v)">
- {{`${v.text}`}}
- </view>
- </scroll-view>
- <!-- <view class="btnGroup">
- <view class="cancelBtn btn" @click="btnGroupClickHandle(false)">取消</view>
- <view class="confirmBtn btn" @click="btnGroupClickHandle(true)">确定</view>
- </view> -->
- </view>
- </uni-popup>
- </view>
- </template>
- <script>
- // 获取系统状态栏的高度
- let systemInfo = uni.getSystemInfoSync();
- let menuButtonInfo = {};
- // 如果是小程序,获取右上角胶囊的尺寸信息,避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API,尚未兼容)
- // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
- menuButtonInfo = uni.getMenuButtonBoundingClientRect();
- // #endif
- /**
- * navbar 自定义导航栏
- * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uniapp自带的导航栏。
- * @tutorial https://www.uviewui.com/components/navbar.html
- * @property {String Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上),注意这里的单位是px(默认44)
- * @property {String} back-icon-color 左边返回图标的颜色(默认#606266)
- * @property {String} back-icon-name 左边返回图标的名称,只能为uView自带的图标(默认arrow-left)
- * @property {String Number} back-icon-size 左边返回图标的大小,单位rpx(默认30)
- * @property {String} back-text 返回图标右边的辅助提示文字
- * @property {Object} back-text-style 返回图标右边的辅助提示文字的样式,对象形式(默认{ color: '#606266' })
- * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
- * @property {String Number} title-width 导航栏标题的最大宽度,内容超出会以省略号隐藏,单位rpx(默认250)
- * @property {String} title-color 标题的颜色(默认#606266)
- * @property {String Number} title-size 导航栏标题字体大小,单位rpx(默认32)
- * @property {Function} custom-back 自定义返回逻辑方法
- * @property {String Number} z-index 固定在顶部时的z-index值(默认980)
- * @property {Boolean} is-back 是否显示导航栏左边返回图标和辅助文字(默认true)
- * @property {Object} background 导航栏背景设置,见官网说明(默认{ background: '#ffffff' })
- * @property {Boolean} is-fixed 导航栏是否固定在顶部(默认true)
- * @property {Boolean} immersive 沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效(默认false)
- * @property {Boolean} border-bottom 导航栏底部是否显示下边框,如定义了较深的背景颜色,可取消此值(默认true)
- * @example <u-navbar back-text="返回" title="剑未配妥,出门已是江湖"></u-navbar>
- */
- export default {
- name: "tm-navbar",
- props: {
- isShowFilter: {
- type: Boolean,
- default: false,
- },
- backTextStyle: {
- type: Object,
- default () {
- return {
- color: '#606266'
- }
- }
- },
- // 左边返回图标的大小,rpx
- backIconSize: {
- type: [String, Number],
- default: '40'
- },
- // 返回箭头的颜色
- backIconColor: {
- type: String,
- default: '#606266'
- },
- isShowSearch: {
- type: Boolean,
- default: false,
- },
- titleMod: {
- type: String,
- default: 'Normal', //Filter 标题为筛选,Normal 普通标题
- },
- messageStatus: {
- type: Boolean,
- default: false, //消息Icon状态,true有消息,false 无
- },
- filterKeys: {
- type: Array,
- default: () => [], //筛选可选项 {text:string,checked:boolean}[]
- },
- // defaultFilterKey: {
- // type: Array,
- // default: () => [], //默认选中筛选项
- // },
- // 导航栏标题
- title: {
- type: String,
- default: ''
- },
- // 标题的宽度,如果需要自定义右侧内容,且右侧内容很多时,可能需要减少这个宽度,单位rpx
- titleWidth: {
- type: [String, Number],
- default: '250'
- },
- // 标题的颜色
- titleColor: {
- type: String,
- default: '#606266'
- },
- // 标题字体是否加粗
- titleBold: {
- type: Boolean,
- default: false
- },
- // 标题的字体大小
- titleSize: {
- type: [String, Number],
- default: 32
- },
- isBack: {
- type: [Boolean, String],
- default: true
- },
- // 左边返回的图标
- backIconName: {
- type: String,
- default: 'nav-back'
- },
- // 对象形式,因为用户可能定义一个纯色,或者线性渐变的颜色
- background: {
- type: Object,
- default () {
- return {
- background: '#ffffff'
- }
- }
- },
- // 导航栏是否固定在顶部
- isFixed: {
- type: Boolean,
- default: true
- },
- // 是否沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效
- immersive: {
- type: Boolean,
- default: false
- },
- // 是否显示导航栏的下边框
- borderBottom: {
- type: Boolean,
- default: true
- },
- zIndex: {
- type: [String, Number],
- default: ''
- },
- // 自定义返回逻辑
- customBack: {
- type: Function,
- default: null
- }
- },
- data() {
- return {
- searchKeywords: '',
- isSearchStatus: false,
- filterStr: '', //筛选关键字
- menuButtonInfo: menuButtonInfo,
- filterKeyList: [],
- checkedItem: null, //筛选选中项
- statusBarHeight: systemInfo.statusBarHeight,
- };
- },
- watch: {
- searchKeywords(prev, current) {
- this.$emit('onsearchChange', prev);
- },
- },
- computed: {
- // 导航栏内部盒子的样式
- navbarInnerStyle() {
- let style = {};
- // 导航栏宽度,如果在小程序下,导航栏宽度为胶囊的左边到屏幕左边的距离
- style.height = this.navbarHeight + 'px';
- // // 如果是各家小程序,导航栏内部的宽度需要减少右边胶囊的宽度
- // #ifdef MP
- let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
- style.marginRight = rightButtonWidth + 'px';
- // #endif
- 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 = {};
- // #ifndef MP
- style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
- style.right = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
- // #endif
- // #ifdef MP
- // 此处是为了让标题显示区域即使在小程序有右侧胶囊的情况下也能处于屏幕的中间,是通过绝对定位实现的
- 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';
- // #endif
- style.width = uni.upx2px(this.titleWidth) + 'px';
- return style;
- },
- // 转换字符数值为真正的数值
- navbarHeight() {
- // #ifdef APP-PLUS || H5
- return this.height ? this.height : 44;
- // #endif
- // #ifdef MP
- // 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
- // 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
- // return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
- let height = systemInfo.platform == 'ios' ? 44 : 48;
- return this.height ? this.height : height;
- // #endif
- }
- },
- mounted() {
- this.filterKeyList = this.filterKeys
- let index = this.filterKeys.findIndex(item => item.checked);
-
- if (index != -1) {
-
- this.checkedItem = this.filterKeys[index];
- this.filterStr = this.filterKeys[index].text
- this.$emit('filterClick',{key:this.checkedItem.key,text:this.checkedItem.text});
- }
- // else {
- // //默认已第一个为选中项
- // this.filterKeyList = this.filterKeys.map((item, index) => {
- // if (index == 0) {
- // this.checkedItem = item;
- // return {
- // text: item.text,
- // checked: true
- // }
- // }
- // })
- // }
- },
- methods: {
- startSearch() {
- this.isSearchStatus = true;
- },
- selecterHandle() {
- this.$refs.popup.open();
- },
- listClickHandle(item) {
- //筛选条件点击
- this.checkedItem = item;
- this.filterStr = item.text;
- this.$refs.popup.close();
- this.$emit('filterClick',{key:item.key,text:item.text});
- },
- getClass(text) {
- if (this.checkedItem && text == this.checkedItem.text) {
- return "list on"
- }
- return "list"
- },
- clearHandle() {
- this.searchKeywords = '';
- },
- cancelSearchHandle() {
- this.searchKeywords = '';
- this.isSearchStatus = false;
- },
- searchInputhandle(e) {
- this.searchKeywords = e;
- },
- checkMessageHandle() {
- this.$emit('checkMessage');
- },
- goBack() {
- // 如果自定义了点击返回按钮的函数,则执行,否则执行返回逻辑
- if (typeof this.customBack === 'function') {
- // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
- // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
- this.customBack.bind(this.$u.$parent.call(this))();
- } else {
- uni.navigateBack();
- }
- }
- }
- };
- </script>
- <style scoped lang="scss">
- @import "./style.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-navbar-inner {
- @include vue-flex;
- flex-direction: row;
- justify-content: space-between;
- position: relative;
- align-items: center;
- padding: 0 30rpx;
- .search {
- display: flex;
- flex-direction: row;
- align-items: center;
- width: 40rpx;
- .searchBtn {
- width: 30rpx;
- height: 30rpx;
- margin-right: 12.5rpx;
- }
- .clearBtn {
- width: 25rpx;
- height: 25rpx;
- }
- .cancelSearchBtn {
- font-size: 25rpx;
- font-family: SourceHanSansCN-Bold, SourceHanSansCN;
- color: #292C33;
- margin-left: 25rpx;
- }
- &.active {
- width: 85vw;
- }
- }
- .navTitle {
- font-size: 25rpx;
- font-family: SourceHanSansCN-Bold, SourceHanSansCN;
- font-weight: bold;
- color: #292C33;
- .filterTitle {
- display: flex;
- &>image {
- position: relative;
- top: 12rpx;
- left: 5rpx;
- width: 12rpx;
- height: 12rpx;
- }
- }
- }
- .navNoticeIcon {
- width: 27.5rpx;
- height: 28.75rpx;
- }
- }
- .u-back-wrap {
- position: relative;
- left: -10rpx;
- @include vue-flex;
- align-items: center;
- flex: 1;
- flex-grow: 0;
- // padding: 14rpx 14rpx 14rpx 24rpx;
- }
- .u-back-text {
- // padding-left: 4rpx;
- font-size: 30rpx;
- }
- .u-navbar-content-title {
- @include vue-flex;
- align-items: center;
- justify-content: center;
- flex: 1;
- position: absolute;
- left: 0;
- right: 0;
- height: 60rpx;
- text-align: center;
- flex-shrink: 0;
- }
- .u-navbar-centent-slot {
- flex: 1;
- }
- .u-title {
- line-height: 60rpx;
- font-size: 32rpx;
- flex: 1;
- }
- .u-navbar-right {
- flex: 1;
- @include vue-flex;
- align-items: center;
- justify-content: flex-end;
- }
- .u-slot-content {
- flex: 1;
- @include vue-flex;
- align-items: center;
- }
- //底部弹窗
- .selectableList {
- display: flex;
- width: 100%;
- flex-direction: column;
- height: 50vh;
- padding-top: 100rpx;
- box-sizing: border-box;
- border-radius: 25rpx 25rpx 0px 0px;
- background-color: #FFFFFF;
- .listWrap {
- height: calc(50vh - 75rpx);
- overflow-y: scroll;
- .list {
- height: 87.5rpx;
- line-height: 87.5rpx;
- text-align: center;
- font-size: 30rpx;
- font-family: SourceHanSansCN-Normal, SourceHanSansCN;
- font-weight: 400;
- color: #8A8F99;
- &.on {
- font-weight: 500;
- color: #3377FF;
- }
- }
- }
- .btnGroup {
- display: flex;
- width: 100%;
- flex-direction: row;
- justify-content: center;
- align-items: center;
- .btn {
- width: 50%;
- height: 75rpx;
- line-height: 75rpx;
- text-align: center;
- font-size: 22.5rpx;
- font-family: SourceHanSansCN-Normal, SourceHanSansCN;
- font-weight: 400;
- color: #3377FF;
- }
- .cancelBtn {
- border-top: 0.62rpx solid #DADEE6;
- }
- .confirmBtn {
- color: #FFFFFF;
- background: #3377FF;
- }
- }
- }
- </style>
|