uni-nav-bar.vue 5.8 KB

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