custom-navbar.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <template>
  2. <view class="uni-navbar" :class="{'uni-dark':dark}">
  3. <view :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }"
  4. :style="{ 'background-color': themeBgColor }" class="uni-navbar__content">
  5. <customStatusBar v-if="statusBar" />
  6. <view :style="{ color: themeColor,backgroundColor: themeBgColor ,height:navbarHeight}"
  7. class="uni-navbar__header">
  8. <view @tap="onClickLeft" class="uni-navbar__header-btns uni-navbar__header-btns-left"
  9. :style="{width:leftIconWidth}">
  10. <slot name="left">
  11. <view class="uni-navbar__content_view" v-if="leftIcon.length > 0">
  12. <uni-icons :color="themeColor" :type="leftIcon" size="20" />
  13. </view>
  14. <view :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length > 0 }" class="uni-navbar-btn-text"
  15. v-if="leftText.length">
  16. <text :style="{ color: themeColor, fontSize: leftTitleFontSize + 'px' }">{{ leftText }}</text>
  17. </view>
  18. </slot>
  19. </view>
  20. <view class="uni-navbar__header-container " @tap="onClickTitle">
  21. <slot>
  22. <view class="uni-navbar__header-container-inner" v-if="title.length>0">
  23. <text class="uni-nav-bar-text uni-ellipsis-1"
  24. :style="{color: themeColor, fontSize: titleFontSize + 'px', fontWeight:700}">{{ title }}</text>
  25. </view>
  26. </slot>
  27. </view>
  28. <view @click="onClickRight" class="uni-navbar__header-btns uni-navbar__header-btns-right"
  29. :style="{width:rightIconWidth}">
  30. <slot name="right">
  31. <view v-if="rightIcon.length">
  32. <uni-icons :color="themeColor" :type="rightIcon" size="22" />
  33. </view>
  34. <view class="uni-navbar-btn-text" v-if="rightText.length && !rightIcon.length">
  35. <text class="uni-nav-bar-right-text" :style="{ color: themeColor}">{{ rightText }}</text>
  36. </view>
  37. </slot>
  38. </view>
  39. </view>
  40. </view>
  41. <view class="uni-navbar__placeholder" v-if="fixed">
  42. <customStatusBar v-if="statusBar" />
  43. <view class="uni-navbar__placeholder-view" :style="{ height:navbarHeight}" />
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import customStatusBar from "../../components/custom-navbar/custom-statusbar.vue"
  49. const getVal = (val) => typeof val === 'number' ? val + 'px' : val;
  50. /**
  51. * NavBar 自定义导航栏
  52. * @description 导航栏组件,主要用于头部导航
  53. * @tutorial https://ext.dcloud.net.cn/plugin?id=52
  54. * @property {Boolean} dark 开启黑暗模式
  55. * @property {String} title 标题文字
  56. * @property {Number} titleFontSize 标题文字大小(新增)
  57. * @property {String} leftText 左侧按钮文本
  58. * @property {String} rightText 右侧按钮文本
  59. * @property {String} leftIcon 左侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
  60. * @property {String} rightIcon 右侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
  61. * @property {String} color 图标和文字颜色
  62. * @property {String} backgroundColor 导航栏背景颜色
  63. * @property {Boolean} fixed = [true|false] 是否固定顶部
  64. * @property {Boolean} statusBar = [true|false] 是否包含状态栏
  65. * @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
  66. * @property {Boolean} border = [true|false] 导航栏下是横线
  67. * @property {Boolean} customClickLeft 自定义左侧按钮点击时触发
  68. * @event {Function} clickLeft 左侧按钮点击时触发
  69. * @event {Function} clickRight 右侧按钮点击时触发
  70. * @event {Function} clickTitle 中间标题点击时触发
  71. */
  72. export default {
  73. name: "CustomNavBar",
  74. components: {
  75. customStatusBar
  76. },
  77. emits: ['clickLeft', 'clickRight', 'clickTitle'],
  78. props: {
  79. dark: {
  80. type: Boolean,
  81. default: false
  82. },
  83. title: {
  84. type: String,
  85. default: ""
  86. },
  87. leftText: {
  88. type: String,
  89. default: ""
  90. },
  91. rightText: {
  92. type: String,
  93. default: ""
  94. },
  95. leftIcon: {
  96. type: String,
  97. default: ""
  98. },
  99. rightIcon: {
  100. type: String,
  101. default: ""
  102. },
  103. fixed: {
  104. type: [Boolean, String],
  105. default: false
  106. },
  107. color: {
  108. type: String,
  109. default: ""
  110. },
  111. backgroundColor: {
  112. type: String,
  113. default: ""
  114. },
  115. statusBar: {
  116. type: [Boolean, String],
  117. default: false
  118. },
  119. shadow: {
  120. type: [Boolean, String],
  121. default: false
  122. },
  123. border: {
  124. type: [Boolean, String],
  125. default: true
  126. },
  127. height: {
  128. type: [Number, String],
  129. default: 44
  130. },
  131. leftWidth: {
  132. type: [Number, String],
  133. default: 60
  134. },
  135. rightWidth: {
  136. type: [Number, String],
  137. default: 60
  138. },
  139. titleFontSize: {
  140. type: [Number, String],
  141. default: 17
  142. },
  143. leftTitleFontSize: {
  144. type: [Number, String],
  145. default: 12
  146. },
  147. customClickLeft: {
  148. type: [Boolean, String],
  149. default: false
  150. }
  151. },
  152. computed: {
  153. themeBgColor() {
  154. if (this.dark) {
  155. // 默认值
  156. if (this.backgroundColor) {
  157. return this.backgroundColor
  158. } else {
  159. return this.dark ? '#333' : '#FFF'
  160. }
  161. }
  162. return this.backgroundColor || '#FFF'
  163. },
  164. themeColor() {
  165. if (this.dark) {
  166. // 默认值
  167. if (this.color) {
  168. return this.color
  169. } else {
  170. return this.dark ? '#fff' : '#333'
  171. }
  172. }
  173. return this.color || '#333'
  174. },
  175. navbarHeight() {
  176. return getVal(this.height)
  177. },
  178. leftIconWidth() {
  179. return getVal(this.leftWidth)
  180. },
  181. rightIconWidth() {
  182. return getVal(this.rightWidth)
  183. },
  184. isCustomClickLeft() {
  185. return getVal(this.customClickLeft)
  186. }
  187. },
  188. mounted() {
  189. if (uni.report && this.title !== '') {
  190. uni.report('title', this.title)
  191. }
  192. },
  193. methods: {
  194. onClickLeft() {
  195. if(this.isCustomClickLeft==false) {
  196. uni.navigateBack()
  197. }
  198. this.$emit("clickLeft");
  199. },
  200. onClickRight() {
  201. this.$emit("clickRight");
  202. },
  203. onClickTitle() {
  204. this.$emit("clickTitle");
  205. }
  206. }
  207. };
  208. </script>
  209. <style lang="scss" scoped>
  210. $nav-height: 44px;
  211. .uni-navbar {
  212. // box-sizing: border-box;
  213. }
  214. .uni-nav-bar-text {
  215. /* #ifdef APP-PLUS */
  216. font-size: 34rpx;
  217. /* #endif */
  218. /* #ifndef APP-PLUS */
  219. font-size: 14px;
  220. /* #endif */
  221. }
  222. .uni-nav-bar-right-text {
  223. font-size: 12px;
  224. }
  225. .uni-navbar__content {
  226. position: relative;
  227. // background-color: #fff;
  228. // box-sizing: border-box;
  229. background-color: transparent;
  230. }
  231. .uni-navbar__content_view {
  232. // box-sizing: border-box;
  233. }
  234. .uni-navbar-btn-text {
  235. /* #ifndef APP-NVUE */
  236. display: flex;
  237. /* #endif */
  238. flex-direction: column;
  239. justify-content: flex-start;
  240. align-items: center;
  241. line-height: 12px;
  242. }
  243. .uni-navbar__header {
  244. /* #ifndef APP-NVUE */
  245. display: flex;
  246. /* #endif */
  247. padding: 0 10px;
  248. flex-direction: row;
  249. height: $nav-height;
  250. font-size: 12px;
  251. }
  252. .uni-navbar__header-btns {
  253. /* #ifndef APP-NVUE */
  254. overflow: hidden;
  255. display: flex;
  256. /* #endif */
  257. flex-wrap: nowrap;
  258. flex-direction: row;
  259. width: 120rpx;
  260. // padding: 0 6px;
  261. justify-content: center;
  262. align-items: center;
  263. /* #ifdef H5 */
  264. cursor: pointer;
  265. /* #endif */
  266. }
  267. .uni-navbar__header-btns-left {
  268. /* #ifndef APP-NVUE */
  269. display: flex;
  270. /* #endif */
  271. width: 120rpx;
  272. justify-content: flex-start;
  273. align-items: center;
  274. }
  275. .uni-navbar__header-btns-right {
  276. /* #ifndef APP-NVUE */
  277. display: flex;
  278. /* #endif */
  279. flex-direction: row;
  280. // width: 150rpx;
  281. // padding-right: 30rpx;
  282. justify-content: flex-end;
  283. align-items: center;
  284. }
  285. .uni-navbar__header-container {
  286. /* #ifndef APP-NVUE */
  287. display: flex;
  288. /* #endif */
  289. flex: 1;
  290. padding: 0 10px;
  291. overflow: hidden;
  292. }
  293. .uni-navbar__header-container-inner {
  294. /* #ifndef APP-NVUE */
  295. display: flex;
  296. /* #endif */
  297. flex: 1;
  298. flex-direction: row;
  299. align-items: center;
  300. justify-content: center;
  301. font-size: 12px;
  302. overflow: hidden;
  303. // box-sizing: border-box;
  304. }
  305. .uni-navbar__placeholder-view {
  306. height: $nav-height;
  307. }
  308. .uni-navbar--fixed {
  309. position: fixed;
  310. z-index: 998;
  311. /* #ifdef H5 */
  312. left: var(--window-left);
  313. right: var(--window-right);
  314. /* #endif */
  315. /* #ifndef H5 */
  316. left: 0;
  317. right: 0;
  318. /* #endif */
  319. }
  320. .uni-navbar--shadow {
  321. box-shadow: 0 1px 6px #ccc;
  322. }
  323. .uni-navbar--border {
  324. border-bottom-width: 1rpx;
  325. border-bottom-style: solid;
  326. border-bottom-color: #eee;
  327. }
  328. .uni-ellipsis-1 {
  329. overflow: hidden;
  330. /* #ifndef APP-NVUE */
  331. white-space: nowrap;
  332. text-overflow: ellipsis;
  333. /* #endif */
  334. /* #ifdef APP-NVUE */
  335. lines: 1;
  336. text-overflow: ellipsis;
  337. /* #endif */
  338. }
  339. // 暗主题配置
  340. .uni-dark {}
  341. </style>