tm-navbar.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <template>
  2. <view class="">
  3. <view class="u-navbar" :style="[navbarStyle]"
  4. :class="{ 'u-navbar-fixed': isFixed, 'u-border-bottom': borderBottom }">
  5. <view class="u-status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
  6. <view class="u-navbar-inner" :style="[navbarInnerStyle]">
  7. <!-- <view class="u-icon-wrap">
  8. <u-icon :name="backIconName" :color="backIconColor" :size="backIconSize"></u-icon>
  9. </view> -->
  10. <view class="u-back-wrap" v-if="isBack" @tap="goBack">
  11. <view class="u-icon-wrap">
  12. <u-icon :name="backIconName" :color="backIconColor" :size="backIconSize"></u-icon>
  13. </view>
  14. <!-- <view class="u-icon-wrap u-back-text u-line-1" v-if="backText" :style="[backTextStyle]">
  15. {{ backText }}</view> -->
  16. </view>
  17. <div :class="[isSearchStatus?'search active':'search']" v-if="isShowSearch">
  18. <image class="searchBtn" src="../../static/search.png" @click="startSearch" mode=""></image>
  19. <u-input v-if="isSearchStatus" placeholder="请输入内容" prefixIcon="search" :value="searchKeywords"
  20. @input="searchInputhandle" prefixIconStyle="font-size: 22px;color: #909399,"></u-input>
  21. <!-- <image v-if="isSearchStatus" @click="clearHandle" class="clearBtn" src="../../static/clearIcon.png" mode=""></image> -->
  22. <div v-if="isSearchStatus" @click="cancelSearchHandle" class="cancelSearchBtn">取消</div>
  23. </div>
  24. <div v-if="!isSearchStatus" class="navTitle">
  25. <text v-if="titleMod == 'Normal'">{{title}}</text>
  26. <view v-if="titleMod == 'Filter'" class="filterTitle" @click="selecterHandle">
  27. <text>{{filterStr}}</text>
  28. <image src="../../static/close-icon.png" mode=""></image>
  29. </view>
  30. </div>
  31. <view class="navNoticeIcon">
  32. <image v-if="messageStatus" class="navNoticeIcon"
  33. :src="`../../static/message-${messageStatus?'unread':'read'}.png`" mode=""
  34. @click="checkMessageHandle"></image>
  35. </view>
  36. </view>
  37. </view>
  38. <!-- 解决fixed定位后导航栏塌陷的问题 -->
  39. <view class="u-navbar-placeholder" v-if="isFixed && !immersive"
  40. :style="{ width: '100%', height: Number(navbarHeight) + statusBarHeight + 'px' }"></view>
  41. <uni-popup ref="popup" type="top" :maskClick="true">
  42. <view class="selectableList">
  43. <scroll-view scroll-y="true" class="listWrap">
  44. <view :class="getClass(v.text)" v-for="(v,i) in filterKeyList" @click="listClickHandle(v)">
  45. {{`${v.text}`}}
  46. </view>
  47. </scroll-view>
  48. <!-- <view class="btnGroup">
  49. <view class="cancelBtn btn" @click="btnGroupClickHandle(false)">取消</view>
  50. <view class="confirmBtn btn" @click="btnGroupClickHandle(true)">确定</view>
  51. </view> -->
  52. </view>
  53. </uni-popup>
  54. </view>
  55. </template>
  56. <script>
  57. // 获取系统状态栏的高度
  58. let systemInfo = uni.getSystemInfoSync();
  59. let menuButtonInfo = {};
  60. // 如果是小程序,获取右上角胶囊的尺寸信息,避免导航栏右侧内容与胶囊重叠(支付宝小程序非本API,尚未兼容)
  61. // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
  62. menuButtonInfo = uni.getMenuButtonBoundingClientRect();
  63. // #endif
  64. /**
  65. * navbar 自定义导航栏
  66. * @description 此组件一般用于在特殊情况下,需要自定义导航栏的时候用到,一般建议使用uniapp自带的导航栏。
  67. * @tutorial https://www.uviewui.com/components/navbar.html
  68. * @property {String Number} height 导航栏高度(不包括状态栏高度在内,内部自动加上),注意这里的单位是px(默认44)
  69. * @property {String} back-icon-color 左边返回图标的颜色(默认#606266)
  70. * @property {String} back-icon-name 左边返回图标的名称,只能为uView自带的图标(默认arrow-left)
  71. * @property {String Number} back-icon-size 左边返回图标的大小,单位rpx(默认30)
  72. * @property {String} back-text 返回图标右边的辅助提示文字
  73. * @property {Object} back-text-style 返回图标右边的辅助提示文字的样式,对象形式(默认{ color: '#606266' })
  74. * @property {String} title 导航栏标题,如设置为空字符,将会隐藏标题占位区域
  75. * @property {String Number} title-width 导航栏标题的最大宽度,内容超出会以省略号隐藏,单位rpx(默认250)
  76. * @property {String} title-color 标题的颜色(默认#606266)
  77. * @property {String Number} title-size 导航栏标题字体大小,单位rpx(默认32)
  78. * @property {Function} custom-back 自定义返回逻辑方法
  79. * @property {String Number} z-index 固定在顶部时的z-index值(默认980)
  80. * @property {Boolean} is-back 是否显示导航栏左边返回图标和辅助文字(默认true)
  81. * @property {Object} background 导航栏背景设置,见官网说明(默认{ background: '#ffffff' })
  82. * @property {Boolean} is-fixed 导航栏是否固定在顶部(默认true)
  83. * @property {Boolean} immersive 沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效(默认false)
  84. * @property {Boolean} border-bottom 导航栏底部是否显示下边框,如定义了较深的背景颜色,可取消此值(默认true)
  85. * @example <u-navbar back-text="返回" title="剑未配妥,出门已是江湖"></u-navbar>
  86. */
  87. export default {
  88. name: "tm-navbar",
  89. props: {
  90. isShowFilter: {
  91. type: Boolean,
  92. default: false,
  93. },
  94. backTextStyle: {
  95. type: Object,
  96. default () {
  97. return {
  98. color: '#606266'
  99. }
  100. }
  101. },
  102. // 左边返回图标的大小,rpx
  103. backIconSize: {
  104. type: [String, Number],
  105. default: '40'
  106. },
  107. // 返回箭头的颜色
  108. backIconColor: {
  109. type: String,
  110. default: '#606266'
  111. },
  112. isShowSearch: {
  113. type: Boolean,
  114. default: false,
  115. },
  116. titleMod: {
  117. type: String,
  118. default: 'Normal', //Filter 标题为筛选,Normal 普通标题
  119. },
  120. messageStatus: {
  121. type: Boolean,
  122. default: false, //消息Icon状态,true有消息,false 无
  123. },
  124. filterKeys: {
  125. type: Array,
  126. default: () => [], //筛选可选项 {text:string,checked:boolean}[]
  127. },
  128. // defaultFilterKey: {
  129. // type: Array,
  130. // default: () => [], //默认选中筛选项
  131. // },
  132. // 导航栏标题
  133. title: {
  134. type: String,
  135. default: ''
  136. },
  137. // 标题的宽度,如果需要自定义右侧内容,且右侧内容很多时,可能需要减少这个宽度,单位rpx
  138. titleWidth: {
  139. type: [String, Number],
  140. default: '250'
  141. },
  142. // 标题的颜色
  143. titleColor: {
  144. type: String,
  145. default: '#606266'
  146. },
  147. // 标题字体是否加粗
  148. titleBold: {
  149. type: Boolean,
  150. default: false
  151. },
  152. // 标题的字体大小
  153. titleSize: {
  154. type: [String, Number],
  155. default: 32
  156. },
  157. isBack: {
  158. type: [Boolean, String],
  159. default: true
  160. },
  161. // 左边返回的图标
  162. backIconName: {
  163. type: String,
  164. default: 'nav-back'
  165. },
  166. // 对象形式,因为用户可能定义一个纯色,或者线性渐变的颜色
  167. background: {
  168. type: Object,
  169. default () {
  170. return {
  171. background: '#ffffff'
  172. }
  173. }
  174. },
  175. // 导航栏是否固定在顶部
  176. isFixed: {
  177. type: Boolean,
  178. default: true
  179. },
  180. // 是否沉浸式,允许fixed定位后导航栏塌陷,仅fixed定位下生效
  181. immersive: {
  182. type: Boolean,
  183. default: false
  184. },
  185. // 是否显示导航栏的下边框
  186. borderBottom: {
  187. type: Boolean,
  188. default: true
  189. },
  190. zIndex: {
  191. type: [String, Number],
  192. default: ''
  193. },
  194. // 自定义返回逻辑
  195. customBack: {
  196. type: Function,
  197. default: null
  198. }
  199. },
  200. data() {
  201. return {
  202. searchKeywords: '',
  203. isSearchStatus: false,
  204. filterStr: '', //筛选关键字
  205. menuButtonInfo: menuButtonInfo,
  206. filterKeyList: [],
  207. checkedItem: null, //筛选选中项
  208. statusBarHeight: systemInfo.statusBarHeight,
  209. };
  210. },
  211. watch: {
  212. searchKeywords(prev, current) {
  213. this.$emit('onsearchChange', prev);
  214. },
  215. },
  216. computed: {
  217. // 导航栏内部盒子的样式
  218. navbarInnerStyle() {
  219. let style = {};
  220. // 导航栏宽度,如果在小程序下,导航栏宽度为胶囊的左边到屏幕左边的距离
  221. style.height = this.navbarHeight + 'px';
  222. // // 如果是各家小程序,导航栏内部的宽度需要减少右边胶囊的宽度
  223. // #ifdef MP
  224. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  225. style.marginRight = rightButtonWidth + 'px';
  226. // #endif
  227. return style;
  228. },
  229. // 整个导航栏的样式
  230. navbarStyle() {
  231. let style = {};
  232. style.zIndex = this.zIndex ? this.zIndex : this.$u.zIndex.navbar;
  233. // 合并用户传递的背景色对象
  234. Object.assign(style, this.background);
  235. return style;
  236. },
  237. // 导航中间的标题的样式
  238. titleStyle() {
  239. let style = {};
  240. // #ifndef MP
  241. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  242. style.right = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  243. // #endif
  244. // #ifdef MP
  245. // 此处是为了让标题显示区域即使在小程序有右侧胶囊的情况下也能处于屏幕的中间,是通过绝对定位实现的
  246. let rightButtonWidth = systemInfo.windowWidth - menuButtonInfo.left;
  247. style.left = (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 + 'px';
  248. style.right = rightButtonWidth - (systemInfo.windowWidth - uni.upx2px(this.titleWidth)) / 2 +
  249. rightButtonWidth +
  250. 'px';
  251. // #endif
  252. style.width = uni.upx2px(this.titleWidth) + 'px';
  253. return style;
  254. },
  255. // 转换字符数值为真正的数值
  256. navbarHeight() {
  257. // #ifdef APP-PLUS || H5
  258. return this.height ? this.height : 44;
  259. // #endif
  260. // #ifdef MP
  261. // 小程序特别处理,让导航栏高度 = 胶囊高度 + 两倍胶囊顶部与状态栏底部的距离之差(相当于同时获得了导航栏底部与胶囊底部的距离)
  262. // 此方法有缺陷,暂不用(会导致少了几个px),采用直接固定值的方式
  263. // return menuButtonInfo.height + (menuButtonInfo.top - this.statusBarHeight) * 2;//导航高度
  264. let height = systemInfo.platform == 'ios' ? 44 : 48;
  265. return this.height ? this.height : height;
  266. // #endif
  267. }
  268. },
  269. mounted() {
  270. this.filterKeyList = this.filterKeys
  271. let index = this.filterKeys.findIndex(item => item.checked);
  272. if (index != -1) {
  273. this.checkedItem = this.filterKeys[index];
  274. this.filterStr = this.filterKeys[index].text
  275. this.$emit('filterClick',{key:this.checkedItem.key,text:this.checkedItem.text});
  276. }
  277. // else {
  278. // //默认已第一个为选中项
  279. // this.filterKeyList = this.filterKeys.map((item, index) => {
  280. // if (index == 0) {
  281. // this.checkedItem = item;
  282. // return {
  283. // text: item.text,
  284. // checked: true
  285. // }
  286. // }
  287. // })
  288. // }
  289. },
  290. methods: {
  291. startSearch() {
  292. this.isSearchStatus = true;
  293. },
  294. selecterHandle() {
  295. this.$refs.popup.open();
  296. },
  297. listClickHandle(item) {
  298. //筛选条件点击
  299. this.checkedItem = item;
  300. this.filterStr = item.text;
  301. this.$refs.popup.close();
  302. this.$emit('filterClick',{key:item.key,text:item.text});
  303. },
  304. getClass(text) {
  305. if (this.checkedItem && text == this.checkedItem.text) {
  306. return "list on"
  307. }
  308. return "list"
  309. },
  310. clearHandle() {
  311. this.searchKeywords = '';
  312. },
  313. cancelSearchHandle() {
  314. this.searchKeywords = '';
  315. this.isSearchStatus = false;
  316. },
  317. searchInputhandle(e) {
  318. this.searchKeywords = e;
  319. },
  320. checkMessageHandle() {
  321. this.$emit('checkMessage');
  322. },
  323. goBack() {
  324. // 如果自定义了点击返回按钮的函数,则执行,否则执行返回逻辑
  325. if (typeof this.customBack === 'function') {
  326. // 在微信,支付宝等环境(H5正常),会导致父组件定义的customBack()函数体中的this变成子组件的this
  327. // 通过bind()方法,绑定父组件的this,让this.customBack()的this为父组件的上下文
  328. this.customBack.bind(this.$u.$parent.call(this))();
  329. } else {
  330. uni.navigateBack();
  331. }
  332. }
  333. }
  334. };
  335. </script>
  336. <style scoped lang="scss">
  337. @import "./style.scss";
  338. .u-navbar {
  339. width: 100%;
  340. }
  341. .u-navbar-fixed {
  342. position: fixed;
  343. left: 0;
  344. right: 0;
  345. top: 0;
  346. z-index: 991;
  347. }
  348. .u-status-bar {
  349. width: 100%;
  350. }
  351. .u-navbar-inner {
  352. @include vue-flex;
  353. flex-direction: row;
  354. justify-content: space-between;
  355. position: relative;
  356. align-items: center;
  357. padding: 0 30rpx;
  358. .search {
  359. display: flex;
  360. flex-direction: row;
  361. align-items: center;
  362. width: 40rpx;
  363. .searchBtn {
  364. width: 30rpx;
  365. height: 30rpx;
  366. margin-right: 12.5rpx;
  367. }
  368. .clearBtn {
  369. width: 25rpx;
  370. height: 25rpx;
  371. }
  372. .cancelSearchBtn {
  373. font-size: 25rpx;
  374. font-family: SourceHanSansCN-Bold, SourceHanSansCN;
  375. color: #292C33;
  376. margin-left: 25rpx;
  377. }
  378. &.active {
  379. width: 85vw;
  380. }
  381. }
  382. .navTitle {
  383. font-size: 25rpx;
  384. font-family: SourceHanSansCN-Bold, SourceHanSansCN;
  385. font-weight: bold;
  386. color: #292C33;
  387. .filterTitle {
  388. display: flex;
  389. &>image {
  390. position: relative;
  391. top: 12rpx;
  392. left: 5rpx;
  393. width: 12rpx;
  394. height: 12rpx;
  395. }
  396. }
  397. }
  398. .navNoticeIcon {
  399. width: 27.5rpx;
  400. height: 28.75rpx;
  401. }
  402. }
  403. .u-back-wrap {
  404. position: relative;
  405. left: -10rpx;
  406. @include vue-flex;
  407. align-items: center;
  408. flex: 1;
  409. flex-grow: 0;
  410. // padding: 14rpx 14rpx 14rpx 24rpx;
  411. }
  412. .u-back-text {
  413. // padding-left: 4rpx;
  414. font-size: 30rpx;
  415. }
  416. .u-navbar-content-title {
  417. @include vue-flex;
  418. align-items: center;
  419. justify-content: center;
  420. flex: 1;
  421. position: absolute;
  422. left: 0;
  423. right: 0;
  424. height: 60rpx;
  425. text-align: center;
  426. flex-shrink: 0;
  427. }
  428. .u-navbar-centent-slot {
  429. flex: 1;
  430. }
  431. .u-title {
  432. line-height: 60rpx;
  433. font-size: 32rpx;
  434. flex: 1;
  435. }
  436. .u-navbar-right {
  437. flex: 1;
  438. @include vue-flex;
  439. align-items: center;
  440. justify-content: flex-end;
  441. }
  442. .u-slot-content {
  443. flex: 1;
  444. @include vue-flex;
  445. align-items: center;
  446. }
  447. //底部弹窗
  448. .selectableList {
  449. display: flex;
  450. width: 100%;
  451. flex-direction: column;
  452. height: 50vh;
  453. padding-top: 100rpx;
  454. box-sizing: border-box;
  455. border-radius: 25rpx 25rpx 0px 0px;
  456. background-color: #FFFFFF;
  457. .listWrap {
  458. height: calc(50vh - 75rpx);
  459. overflow-y: scroll;
  460. .list {
  461. height: 87.5rpx;
  462. line-height: 87.5rpx;
  463. text-align: center;
  464. font-size: 30rpx;
  465. font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  466. font-weight: 400;
  467. color: #8A8F99;
  468. &.on {
  469. font-weight: 500;
  470. color: #3377FF;
  471. }
  472. }
  473. }
  474. .btnGroup {
  475. display: flex;
  476. width: 100%;
  477. flex-direction: row;
  478. justify-content: center;
  479. align-items: center;
  480. .btn {
  481. width: 50%;
  482. height: 75rpx;
  483. line-height: 75rpx;
  484. text-align: center;
  485. font-size: 22.5rpx;
  486. font-family: SourceHanSansCN-Normal, SourceHanSansCN;
  487. font-weight: 400;
  488. color: #3377FF;
  489. }
  490. .cancelBtn {
  491. border-top: 0.62rpx solid #DADEE6;
  492. }
  493. .confirmBtn {
  494. color: #FFFFFF;
  495. background: #3377FF;
  496. }
  497. }
  498. }
  499. </style>