su-navbar.vue 11 KB

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