uni-nav-bar.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view class="uni-navbar" :class="{'uni-dark':dark, 'uni-nvue-fixed': fixed}">
  3. <view class="uni-navbar__content" :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }"
  4. :style="{ 'background-color': themeBgColor, 'border-bottom-color':themeColor }" >
  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. <!-- #ifndef APP-NVUE -->
  42. <view class="uni-navbar__placeholder" v-if="fixed">
  43. <status-bar v-if="statusBar" />
  44. <view class="uni-navbar__placeholder-view" :style="{ height:navbarHeight}" />
  45. </view>
  46. <!-- #endif -->
  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. stat: {
  142. type: [Boolean, String],
  143. default: ''
  144. }
  145. },
  146. computed: {
  147. themeBgColor() {
  148. if (this.dark) {
  149. // 默认值
  150. if (this.backgroundColor) {
  151. return this.backgroundColor
  152. } else {
  153. return this.dark ? '#333' : '#FFF'
  154. }
  155. }
  156. return this.backgroundColor || '#FFF'
  157. },
  158. themeColor() {
  159. if (this.dark) {
  160. // 默认值
  161. if (this.color) {
  162. return this.color
  163. } else {
  164. return this.dark ? '#fff' : '#333'
  165. }
  166. }
  167. return this.color || '#333'
  168. },
  169. navbarHeight() {
  170. return getVal(this.height)
  171. },
  172. leftIconWidth() {
  173. return getVal(this.leftWidth)
  174. },
  175. rightIconWidth() {
  176. return getVal(this.rightWidth)
  177. }
  178. },
  179. mounted() {
  180. if (uni.report && this.stat && this.title !== '') {
  181. uni.report('title', this.title)
  182. }
  183. },
  184. methods: {
  185. onClickLeft() {
  186. this.$emit("clickLeft");
  187. },
  188. onClickRight() {
  189. this.$emit("clickRight");
  190. },
  191. onClickTitle() {
  192. this.$emit("clickTitle");
  193. }
  194. }
  195. };
  196. </script>
  197. <style lang="scss" scoped>
  198. $nav-height: 44px;
  199. .uni-nvue-fixed {
  200. /* #ifdef APP-NVUE */
  201. position: sticky;
  202. /* #endif */
  203. }
  204. .uni-navbar {
  205. // box-sizing: border-box;
  206. }
  207. .uni-nav-bar-text {
  208. /* #ifdef APP-PLUS */
  209. font-size: 34rpx;
  210. /* #endif */
  211. /* #ifndef APP-PLUS */
  212. font-size: 14px;
  213. /* #endif */
  214. }
  215. .uni-nav-bar-right-text {
  216. font-size: 12px;
  217. }
  218. .uni-navbar__content {
  219. position: relative;
  220. // background-color: #fff;
  221. // box-sizing: border-box;
  222. background-color: transparent;
  223. }
  224. .uni-navbar__content_view {
  225. // box-sizing: border-box;
  226. }
  227. .uni-navbar-btn-text {
  228. /* #ifndef APP-NVUE */
  229. display: flex;
  230. /* #endif */
  231. flex-direction: column;
  232. justify-content: flex-start;
  233. align-items: center;
  234. line-height: 12px;
  235. }
  236. .uni-navbar__header {
  237. /* #ifndef APP-NVUE */
  238. display: flex;
  239. /* #endif */
  240. padding: 0 10px;
  241. flex-direction: row;
  242. height: $nav-height;
  243. font-size: 12px;
  244. }
  245. .uni-navbar__header-btns {
  246. /* #ifndef APP-NVUE */
  247. overflow: hidden;
  248. display: flex;
  249. /* #endif */
  250. flex-wrap: nowrap;
  251. flex-direction: row;
  252. width: 120rpx;
  253. // padding: 0 6px;
  254. justify-content: center;
  255. align-items: center;
  256. /* #ifdef H5 */
  257. cursor: pointer;
  258. /* #endif */
  259. }
  260. .uni-navbar__header-btns-left {
  261. /* #ifndef APP-NVUE */
  262. display: flex;
  263. /* #endif */
  264. width: 120rpx;
  265. justify-content: flex-start;
  266. align-items: center;
  267. }
  268. .uni-navbar__header-btns-right {
  269. /* #ifndef APP-NVUE */
  270. display: flex;
  271. /* #endif */
  272. flex-direction: row;
  273. // width: 150rpx;
  274. // padding-right: 30rpx;
  275. justify-content: flex-end;
  276. align-items: center;
  277. }
  278. .uni-navbar__header-container {
  279. /* #ifndef APP-NVUE */
  280. display: flex;
  281. /* #endif */
  282. flex: 1;
  283. padding: 0 10px;
  284. overflow: hidden;
  285. }
  286. .uni-navbar__header-container-inner {
  287. /* #ifndef APP-NVUE */
  288. display: flex;
  289. /* #endif */
  290. flex: 1;
  291. flex-direction: row;
  292. align-items: center;
  293. justify-content: center;
  294. font-size: 12px;
  295. overflow: hidden;
  296. // box-sizing: border-box;
  297. }
  298. .uni-navbar__placeholder-view {
  299. height: $nav-height;
  300. }
  301. .uni-navbar--fixed {
  302. position: fixed;
  303. z-index: 99;
  304. /* #ifdef H5 */
  305. left: var(--window-left);
  306. right: var(--window-right);
  307. /* #endif */
  308. /* #ifndef H5 */
  309. left: 0;
  310. right: 0;
  311. /* #endif */
  312. }
  313. .uni-navbar--shadow {
  314. box-shadow: 0 1px 6px #ccc;
  315. }
  316. .uni-navbar--border {
  317. border-bottom-width: 1rpx;
  318. border-bottom-style: solid;
  319. border-bottom-color: #eee;
  320. }
  321. .uni-ellipsis-1 {
  322. overflow: hidden;
  323. /* #ifndef APP-NVUE */
  324. white-space: nowrap;
  325. text-overflow: ellipsis;
  326. /* #endif */
  327. /* #ifdef APP-NVUE */
  328. lines: 1;
  329. text-overflow: ellipsis;
  330. /* #endif */
  331. }
  332. // 暗主题配置
  333. .uni-dark {}
  334. </style>