su-navbar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <!-- 自定义导航栏 -->
  2. <template>
  3. <view class="uni-navbar" :class="{ 'uni-dark': dark }">
  4. <view
  5. :class="{
  6. 'uni-navbar--fixed': fixed,
  7. 'uni-navbar--shadow': shadow,
  8. 'uni-navbar--border': border,
  9. }"
  10. class="uni-navbar__content"
  11. >
  12. <view class="fixed-bg" :class="[opacity ? '' : opacityBgUi]"></view>
  13. <su-status-bar v-if="statusBar" />
  14. <view
  15. :style="{
  16. color: themeColor,
  17. height: navbarHeight,
  18. background: backgroundColor,
  19. }"
  20. class="uni-navbar__header"
  21. >
  22. <view v-if="showBackButton" class="back_btn" @tap="onClickBack"></view>
  23. <view
  24. v-else
  25. class="uni-navbar__header-btns uni-navbar__header-btns-left"
  26. :style="{ width: leftIconWidth }"
  27. >
  28. <slot name="left">
  29. <view class="uni-navbar__content_view" v-if="leftIcon.length > 0">
  30. <view class="icon-box ss-flex">
  31. <view class="icon-button icon-button-left ss-flex ss-row-center" @tap="onClickLeft">
  32. <text class="sicon-back" v-if="hasHistory" />
  33. <text class="sicon-home" v-else />
  34. </view>
  35. <view class="line"></view>
  36. <view
  37. class="icon-button icon-button-right ss-flex ss-row-center"
  38. @tap="showMenuTools"
  39. >
  40. <text class="sicon-more" />
  41. </view>
  42. </view>
  43. </view>
  44. <view
  45. :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length > 0 }"
  46. class="uni-navbar-btn-text"
  47. v-if="
  48. titleAlign === 'left' &&
  49. title.length &&
  50. sheep.$platform.name !== 'WechatOfficialAccount'
  51. "
  52. >
  53. <text :style="{ color: themeColor, fontSize: '18px' }">{{ title }}</text>
  54. </view>
  55. </slot>
  56. </view>
  57. <view v-if="tools === 'search'" class="ss-flex-1">
  58. <slot name="center">
  59. <uni-search-bar
  60. class="ss-flex-1 search-box"
  61. :radius="20"
  62. placeholder="请输入关键词"
  63. cancelButton="none"
  64. v-model="searchModel"
  65. @confirm="onSearch"
  66. />
  67. </slot>
  68. </view>
  69. <view v-else class="uni-navbar__header-container" @tap="onClickTitle">
  70. <slot name="center">
  71. <view
  72. v-if="tools === 'title' && titleAlign === 'center' && title.length"
  73. class="uni-navbar__header-container-inner"
  74. >
  75. <text :style="{ color: themeColor, fontSize: '36rpx' }" class="ss-line-1">{{
  76. title
  77. }}</text>
  78. </view>
  79. </slot>
  80. </view>
  81. </view>
  82. </view>
  83. <view class="uni-navbar__placeholder" v-if="placeholder">
  84. <su-status-bar v-if="statusBar" />
  85. <view class="uni-navbar__placeholder-view" :style="{ height: navbarHeight }" />
  86. </view>
  87. <!-- 头部问题 -->
  88. <!-- #ifdef MP -->
  89. <!-- <view :style="[capsuleStyle]"></view> -->
  90. <!-- #endif -->
  91. </view>
  92. </template>
  93. <script setup>
  94. import sheep from '@/common';
  95. import { onLoad } from '@dcloudio/uni-app';
  96. import { showMenuTools, closeMenuTools } from '@/common/hooks/useModal';
  97. import { computed, ref } from 'vue';
  98. /**
  99. * NavBar 自定义导航栏
  100. * @description 导航栏组件,主要用于头部导航
  101. * @property {Boolean} dark 开启黑暗模式
  102. * @property {String} title 标题文字
  103. * @property {String} rightText 右侧按钮文本
  104. * @property {String} leftIcon 左侧按钮图标
  105. * @property {String} rightIcon 右侧按钮图标
  106. * @property {String} color 图标和文字颜色
  107. * @property {String} backgroundColor 导航栏背景颜色
  108. * @property {Boolean} fixed = [true|false] 是否固定顶部
  109. * @property {Boolean} statusBar = [true|false] 是否包含状态栏
  110. * @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
  111. * @event {Function} clickLeft 左侧按钮点击时触发
  112. * @event {Function} clickRight 右侧按钮点击时触发
  113. * @event {Function} clickTitle 中间标题点击时触发
  114. */
  115. const getVal = (val) => (typeof val === 'number' ? val + 'px' : val);
  116. const emits = defineEmits(['clickLeft', 'clickRight', 'clickTitle', 'search']);
  117. const props = defineProps({
  118. dark: {
  119. type: Boolean,
  120. default: false,
  121. },
  122. modelValue: {
  123. type: String,
  124. default: '',
  125. },
  126. title: {
  127. type: String,
  128. default: '',
  129. },
  130. titleAlign: {
  131. type: String,
  132. default: 'center', // left | center
  133. },
  134. rightText: {
  135. type: String,
  136. default: '',
  137. },
  138. leftIcon: {
  139. type: String,
  140. default: 'left',
  141. },
  142. rightIcon: {
  143. type: String,
  144. default: '',
  145. },
  146. fixed: {
  147. type: [Boolean, String],
  148. default: true,
  149. },
  150. placeholder: {
  151. type: [Boolean, String],
  152. default: true,
  153. },
  154. color: {
  155. type: String,
  156. default: '',
  157. },
  158. backgroundColor: {
  159. type: String,
  160. default: '',
  161. },
  162. opacity: {
  163. type: [Boolean, String],
  164. default: false,
  165. },
  166. opacityBgUi: {
  167. type: String,
  168. default: 'bg-white',
  169. },
  170. statusBar: {
  171. type: [Boolean, String],
  172. default: false,
  173. },
  174. shadow: {
  175. type: [Boolean, String],
  176. default: false,
  177. },
  178. border: {
  179. type: [Boolean, String],
  180. default: false,
  181. },
  182. height: {
  183. type: [Number, String],
  184. default: '88rpx',
  185. },
  186. leftWidth: {
  187. type: [Number, String],
  188. default: 80,
  189. },
  190. rightWidth: {
  191. type: [Number, String],
  192. default: 0,
  193. },
  194. tools: {
  195. type: String,
  196. default: 'title',
  197. },
  198. defaultSearch: {
  199. type: String,
  200. default: '',
  201. },
  202. showBackButton: {
  203. type: Boolean,
  204. default: false,
  205. },
  206. });
  207. const capsuleStyle = computed(() => {
  208. return {
  209. width: sheep.$platform.capsule.width + 'px',
  210. height: sheep.$platform.capsule.height + 'px',
  211. margin: '0 ' + (sheep.$platform.device.windowWidth - sheep.$platform.capsule.right) + 'px',
  212. };
  213. });
  214. const searchModel = computed(() => {
  215. return props.defaultSearch
  216. })
  217. const themeBgColor = computed(() => {
  218. if (props.dark) {
  219. // 默认值
  220. if (props.backgroundColor) {
  221. return props.backgroundColor;
  222. } else {
  223. return props.dark ? '#333' : '#FFF';
  224. }
  225. }
  226. return props.backgroundColor || '#FFF';
  227. });
  228. const themeColor = computed(() => {
  229. if (props.dark) {
  230. // 默认值
  231. if (props.color) {
  232. return props.color;
  233. } else {
  234. return props.dark ? '#fff' : '#333';
  235. }
  236. }
  237. return props.color || '#333';
  238. });
  239. const navbarHeight = computed(() => {
  240. return getVal(props.height);
  241. });
  242. const leftIconWidth = computed(() => {
  243. return getVal(props.leftWidth);
  244. });
  245. const rightIconWidth = computed(() => {
  246. return getVal(props.rightWidth);
  247. });
  248. function onSearch(e) {
  249. emits('search', e.value);
  250. }
  251. onLoad(() => {
  252. if (uni.report && props.title !== '') {
  253. uni.report('title', props.title);
  254. }
  255. });
  256. const hasHistory = sheep.$router.hasHistory();
  257. function onClickBack(){
  258. if (hasHistory) {
  259. sheep.$router.back();
  260. } else {
  261. sheep.$router.go('/pages/index/index');
  262. }
  263. }
  264. function onClickLeft() {
  265. if (hasHistory) {
  266. sheep.$router.back();
  267. } else {
  268. sheep.$router.go('/pages/index/index');
  269. }
  270. emits('clickLeft');
  271. }
  272. function onClickRight() {
  273. showMenuTools();
  274. }
  275. function onClickTitle() {
  276. emits('clickTitle');
  277. }
  278. </script>
  279. <style lang="scss" scoped>
  280. .bg-main {
  281. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient)) !important;
  282. color: #fff !important;
  283. }
  284. .back_btn{
  285. width: 48rpx;
  286. height: 48rpx;
  287. background: url('/static/back_icon.png') no-repeat center/48rpx 48rpx;
  288. }
  289. .icon-box {
  290. background: #ffffff;
  291. box-shadow: 0px 0px 4rpx rgba(51, 51, 51, 0.08), 0px 4rpx 6rpx 2rpx rgba(102, 102, 102, 0.12);
  292. border-radius: 30rpx;
  293. width: 134rpx;
  294. height: 56rpx;
  295. margin-left: 8rpx;
  296. .line {
  297. width: 2rpx;
  298. height: 24rpx;
  299. background: #e5e5e7;
  300. }
  301. .sicon-back {
  302. font-size: 32rpx;
  303. color: #000;
  304. }
  305. .sicon-home {
  306. font-size: 32rpx;
  307. color: #000;
  308. }
  309. .sicon-more {
  310. font-size: 32rpx;
  311. color: #000;
  312. }
  313. .icon-button {
  314. width: 67rpx;
  315. height: 56rpx;
  316. &-left:hover {
  317. background: rgba(0, 0, 0, 0.16);
  318. border-radius: 30rpx 0px 0px 30rpx;
  319. }
  320. &-right:hover {
  321. background: rgba(0, 0, 0, 0.16);
  322. border-radius: 0px 30rpx 30rpx 0px;
  323. }
  324. }
  325. }
  326. $nav-height: 88rpx;
  327. .fixed-bg {
  328. position: absolute;
  329. width: 100%;
  330. height: 100%;
  331. top: 0;
  332. z-index: 1;
  333. pointer-events: none;
  334. }
  335. .uni-nav-bar-text {
  336. /* #ifdef APP-PLUS */
  337. font-size: 34rpx;
  338. /* #endif */
  339. /* #ifndef APP-PLUS */
  340. font-size: 14px;
  341. /* #endif */
  342. }
  343. .uni-nav-bar-right-text {
  344. font-size: 12px;
  345. }
  346. .uni-navbar__content {
  347. position: relative;
  348. // background-color: #fff;
  349. // box-sizing: border-box;
  350. background-color: transparent;
  351. width: 750rpx;
  352. margin: 0 auto;
  353. }
  354. .uni-navbar__content_view {
  355. // box-sizing: border-box;
  356. }
  357. .uni-navbar-btn-text {
  358. /* #ifndef APP-NVUE */
  359. display: flex;
  360. /* #endif */
  361. flex-direction: column;
  362. justify-content: flex-start;
  363. align-items: center;
  364. line-height: 18px;
  365. }
  366. .uni-navbar__header {
  367. /* #ifndef APP-NVUE */
  368. display: flex;
  369. /* #endif */
  370. padding: 0 10px;
  371. flex-direction: row;
  372. justify-content: space-between;
  373. height: $nav-height;
  374. font-size: 12px;
  375. position: relative;
  376. z-index: 2;
  377. align-items: center;
  378. }
  379. .uni-navbar__header-btns {
  380. /* #ifndef APP-NVUE */
  381. overflow: hidden;
  382. display: flex;
  383. /* #endif */
  384. flex-wrap: nowrap;
  385. flex-direction: row;
  386. // min-width: 120rpx;
  387. min-width: 40rpx;
  388. // padding: 0 6px;
  389. justify-content: center;
  390. align-items: center;
  391. /* #ifdef H5 */
  392. cursor: pointer;
  393. /* #endif */
  394. }
  395. .uni-navbar__header-btns-left {
  396. /* #ifndef APP-NVUE */
  397. display: flex;
  398. /* #endif */
  399. width: 120rpx;
  400. justify-content: flex-start;
  401. align-items: center;
  402. }
  403. .uni-navbar__header-btns-right {
  404. /* #ifndef APP-NVUE */
  405. display: flex;
  406. /* #endif */
  407. flex-direction: row;
  408. // width: 150rpx;
  409. // padding-right: 30rpx;
  410. justify-content: flex-end;
  411. align-items: center;
  412. }
  413. .uni-navbar__header-container {
  414. /* #ifndef APP-NVUE */
  415. // display: flex;
  416. /* #endif */
  417. // flex: 1;
  418. // padding: 0 10px;
  419. // overflow: hidden;
  420. position: absolute;
  421. left: 50%;
  422. transform: translateX(-50%) translateY(-50%);
  423. top: 50%;
  424. }
  425. .uni-navbar__header-container-inner {
  426. /* #ifndef APP-NVUE */
  427. display: flex;
  428. /* #endif */
  429. flex: 1;
  430. flex-direction: row;
  431. align-items: center;
  432. justify-content: center;
  433. font-size: 12px;
  434. overflow: hidden;
  435. // box-sizing: border-box;
  436. }
  437. .uni-navbar__placeholder-view {
  438. height: $nav-height;
  439. }
  440. .uni-navbar--fixed {
  441. position: fixed;
  442. z-index: 998;
  443. /* #ifdef H5 */
  444. left: var(--window-left);
  445. right: var(--window-right);
  446. /* #endif */
  447. /* #ifndef H5 */
  448. left: 0;
  449. right: 0;
  450. /* #endif */
  451. }
  452. .uni-navbar--shadow {
  453. box-shadow: 0 1px 6px #ccc;
  454. }
  455. .uni-navbar--border {
  456. border-bottom-width: 1rpx;
  457. border-bottom-style: solid;
  458. border-bottom-color: #eee;
  459. }
  460. .uni-ellipsis-1 {
  461. overflow: hidden;
  462. /* #ifndef APP-NVUE */
  463. white-space: nowrap;
  464. text-overflow: ellipsis;
  465. /* #endif */
  466. /* #ifdef APP-NVUE */
  467. lines: 1;
  468. text-overflow: ellipsis;
  469. /* #endif */
  470. }
  471. // 暗主题配置
  472. .uni-dark {
  473. }
  474. </style>