uni-nav-bar.vue 6.0 KB

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