uni-nav-bar.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <view class="uni-navbar">
  3. <view :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }" :style="{ 'background-color': backgroundColor }"
  4. class="uni-navbar__content">
  5. <uni-status-bar v-if="statusBar" />
  6. <view :style="{ color: color,backgroundColor: backgroundColor }" class="uni-navbar__header uni-navbar__content_view">
  7. <view @tap="onClickLeft" class="uni-navbar__header-btns uni-navbar__header-btns-left uni-navbar__content_view">
  8. <view class="uni-navbar__content_view" v-if="leftIcon.length">
  9. <uni-icons :color="color" :type="leftIcon" size="24" />
  10. </view>
  11. <view :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length }" class="uni-navbar-btn-text uni-navbar__content_view"
  12. v-if="leftText.length">
  13. <text :style="{ color: color, fontSize: '14px' }">{{ leftText }}</text>
  14. </view>
  15. <slot name="left" />
  16. </view>
  17. <view class="uni-navbar__header-container uni-navbar__content_view" @tap="onClickTitle">
  18. <view class="uni-navbar__header-container-inner uni-navbar__content_view" v-if="title.length">
  19. <text class="uni-nav-bar-text" :style="{color: color }">{{ title }}</text>
  20. </view>
  21. <!-- 标题插槽 -->
  22. <slot />
  23. </view>
  24. <view :class="title.length ? 'uni-navbar__header-btns-right' : ''" @tap="onClickRight" class="uni-navbar__header-btns uni-navbar__content_view">
  25. <view class="uni-navbar__content_view" v-if="rightIcon.length">
  26. <uni-icons :color="color" :type="rightIcon" size="24" />
  27. </view>
  28. <!-- 优先显示图标 -->
  29. <view class="uni-navbar-btn-text uni-navbar__content_view" v-if="rightText.length && !rightIcon.length">
  30. <text class="uni-nav-bar-right-text">{{ rightText }}</text>
  31. </view>
  32. <slot name="right" />
  33. </view>
  34. </view>
  35. </view>
  36. <view class="uni-navbar__placeholder" v-if="fixed">
  37. <uni-status-bar v-if="statusBar" />
  38. <view class="uni-navbar__placeholder-view" />
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import uniStatusBar from "../uni-status-bar/uni-status-bar.vue";
  44. import uniIcons from "../uni-icons/uni-icons.vue";
  45. /**
  46. * NavBar 自定义导航栏
  47. * @description 导航栏组件,主要用于头部导航
  48. * @tutorial https://ext.dcloud.net.cn/plugin?id=52
  49. * @property {String} title 标题文字
  50. * @property {String} leftText 左侧按钮文本
  51. * @property {String} rightText 右侧按钮文本
  52. * @property {String} leftIcon 左侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
  53. * @property {String} rightIcon 右侧按钮图标(图标类型参考 [Icon 图标](http://ext.dcloud.net.cn/plugin?id=28) type 属性)
  54. * @property {String} color 图标和文字颜色
  55. * @property {String} backgroundColor 导航栏背景颜色
  56. * @property {Boolean} fixed = [true|false] 是否固定顶部
  57. * @property {Boolean} statusBar = [true|false] 是否包含状态栏
  58. * @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
  59. * @event {Function} clickLeft 左侧按钮点击时触发
  60. * @event {Function} clickRight 右侧按钮点击时触发
  61. * @event {Function} clickTitle 中间标题点击时触发
  62. */
  63. export default {
  64. name: "UniNavBar",
  65. components: {
  66. uniStatusBar,
  67. uniIcons
  68. },
  69. props: {
  70. title: {
  71. type: String,
  72. default: ""
  73. },
  74. leftText: {
  75. type: String,
  76. default: ""
  77. },
  78. rightText: {
  79. type: String,
  80. default: ""
  81. },
  82. leftIcon: {
  83. type: String,
  84. default: ""
  85. },
  86. rightIcon: {
  87. type: String,
  88. default: ""
  89. },
  90. fixed: {
  91. type: [Boolean, String],
  92. default: false
  93. },
  94. color: {
  95. type: String,
  96. default: "#000000"
  97. },
  98. backgroundColor: {
  99. type: String,
  100. default: "#FFFFFF"
  101. },
  102. statusBar: {
  103. type: [Boolean, String],
  104. default: false
  105. },
  106. shadow: {
  107. type: [Boolean, String],
  108. default: false
  109. },
  110. border: {
  111. type: [Boolean, String],
  112. default: true
  113. }
  114. },
  115. mounted() {
  116. if(uni.report && this.title !== '') {
  117. uni.report('title', this.title)
  118. }
  119. },
  120. methods: {
  121. onClickLeft() {
  122. this.$emit("clickLeft");
  123. },
  124. onClickRight() {
  125. this.$emit("clickRight");
  126. },
  127. onClickTitle() {
  128. this.$emit("clickTitle");
  129. }
  130. }
  131. };
  132. </script>
  133. <style lang="scss" scoped>
  134. $nav-height: 44px;
  135. .uni-nav-bar-text {
  136. /* #ifdef APP-PLUS */
  137. font-size: 34rpx;
  138. /* #endif */
  139. /* #ifndef APP-PLUS */
  140. font-size: $uni-font-size-lg;
  141. /* #endif */
  142. }
  143. .uni-nav-bar-right-text {
  144. font-size: $uni-font-size-base;
  145. }
  146. .uni-navbar__content {
  147. position: relative;
  148. background-color: $uni-bg-color;
  149. overflow: hidden;
  150. width: 750rpx;
  151. }
  152. .uni-navbar__content_view {
  153. /* #ifndef APP-NVUE */
  154. display: flex;
  155. /* #endif */
  156. align-items: center;
  157. flex-direction: row;
  158. // background-color: #FFFFFF;
  159. }
  160. .uni-navbar__header {
  161. /* #ifndef APP-NVUE */
  162. display: flex;
  163. /* #endif */
  164. flex-direction: row;
  165. height: $nav-height;
  166. line-height: $nav-height;
  167. font-size: 16px;
  168. // background-color: #ffffff;
  169. }
  170. .uni-navbar__header-btns {
  171. /* #ifndef APP-NVUE */
  172. display: flex;
  173. /* #endif */
  174. flex-wrap: nowrap;
  175. width: 120rpx;
  176. padding: 0 6px;
  177. justify-content: center;
  178. align-items: center;
  179. }
  180. .uni-navbar__header-btns-left {
  181. /* #ifndef APP-NVUE */
  182. display: flex;
  183. /* #endif */
  184. width: 150rpx;
  185. justify-content: flex-start;
  186. }
  187. .uni-navbar__header-btns-right {
  188. /* #ifndef APP-NVUE */
  189. display: flex;
  190. /* #endif */
  191. width: 150rpx;
  192. padding-right: 30rpx;
  193. justify-content: flex-end;
  194. }
  195. .uni-navbar__header-container {
  196. flex: 1;
  197. }
  198. .uni-navbar__header-container-inner {
  199. /* #ifndef APP-NVUE */
  200. display: flex;
  201. /* #endif */
  202. flex: 1;
  203. align-items: center;
  204. justify-content: center;
  205. font-size: $uni-font-size-base;
  206. }
  207. .uni-navbar__placeholder-view {
  208. height: $nav-height;
  209. }
  210. .uni-navbar--fixed {
  211. position: fixed;
  212. z-index: 998;
  213. }
  214. .uni-navbar--shadow {
  215. /* #ifndef APP-NVUE */
  216. box-shadow: 0 1px 6px #ccc;
  217. /* #endif */
  218. }
  219. .uni-navbar--border {
  220. border-bottom-width: 1rpx;
  221. border-bottom-style: solid;
  222. border-bottom-color: $uni-border-color;
  223. }
  224. </style>