uni-nav-bar.vue 9.0 KB

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