uni-nav-bar.vue 7.8 KB

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