uni-nav-bar.vue 7.7 KB

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