uni-nav-bar.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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" :style="{'width':btnWidth}">
  8. <view class="uni-navbar__content_view" v-if="leftIcon.length">
  9. <uni-icons :color="color" :type="leftIcon" size="28" />
  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" :style="{'width':btnWidth}">
  25. <view class="uni-navbar__content_view" v-if="rightIcon.length">
  26. <uni-icons :color="color" :type="rightIcon" size="28" />
  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. btnWidth: {
  89. type: String,
  90. default: ""
  91. },
  92. fixed: {
  93. type: [Boolean, String],
  94. default: false
  95. },
  96. color: {
  97. type: String,
  98. default: "#FFFFFF"
  99. },
  100. backgroundColor: {
  101. type: String,
  102. default: "#ff5d5b"
  103. },
  104. statusBar: {
  105. type: [Boolean, String],
  106. default: false
  107. },
  108. shadow: {
  109. type: [Boolean, String],
  110. default: false
  111. },
  112. border: {
  113. type: [Boolean, String],
  114. default: true
  115. }
  116. },
  117. mounted() {
  118. if(uni.report && this.title !== '') {
  119. uni.report('title', this.title)
  120. }
  121. },
  122. methods: {
  123. onClickLeft() {
  124. this.$emit("clickLeft");
  125. },
  126. onClickRight() {
  127. this.$emit("clickRight");
  128. },
  129. onClickTitle() {
  130. this.$emit("clickTitle");
  131. }
  132. }
  133. };
  134. </script>
  135. <style lang="scss" scoped>
  136. $nav-height: 44px;
  137. .uni-nav-bar-text {
  138. /* #ifdef APP-PLUS */
  139. font-size: 34rpx;
  140. /* #endif */
  141. /* #ifndef APP-PLUS */
  142. font-size: $uni-font-size-lg;
  143. /* #endif */
  144. }
  145. .uni-nav-bar-right-text {
  146. font-size: $uni-font-size-base;
  147. }
  148. .uni-navbar__content {
  149. position: relative;
  150. background-color: $uni-bg-color;
  151. overflow: hidden;
  152. width: 750rpx;
  153. }
  154. .uni-navbar__content_view {
  155. /* #ifndef APP-NVUE */
  156. display: flex;
  157. /* #endif */
  158. align-items: center;
  159. flex-direction: row;
  160. // background-color: #FFFFFF;
  161. }
  162. .uni-navbar__header {
  163. /* #ifndef APP-NVUE */
  164. display: flex;
  165. /* #endif */
  166. flex-direction: row;
  167. height: $nav-height;
  168. line-height: $nav-height;
  169. font-size: 16px;
  170. // background-color: #ffffff;
  171. }
  172. .uni-navbar__header-btns {
  173. /* #ifndef APP-NVUE */
  174. display: flex;
  175. /* #endif */
  176. flex-wrap: nowrap;
  177. width: 120rpx;
  178. padding: 0 6px;
  179. justify-content: center;
  180. align-items: center;
  181. /* #ifdef H5 */
  182. cursor: pointer;
  183. /* #endif */
  184. }
  185. .uni-navbar__header-btns-left {
  186. /* #ifndef APP-NVUE */
  187. display: flex;
  188. /* #endif */
  189. width: 150rpx;
  190. justify-content: flex-start;
  191. }
  192. .uni-navbar__header-btns-right {
  193. /* #ifndef APP-NVUE */
  194. display: flex;
  195. /* #endif */
  196. width: 150rpx;
  197. padding-right: 30rpx;
  198. justify-content: flex-end;
  199. }
  200. .uni-navbar__header-container {
  201. flex: 1;
  202. }
  203. .uni-navbar__header-container-inner {
  204. /* #ifndef APP-NVUE */
  205. display: flex;
  206. /* #endif */
  207. flex: 1;
  208. align-items: center;
  209. justify-content: center;
  210. font-size: $uni-font-size-base;
  211. }
  212. .uni-navbar__placeholder-view {
  213. height: $nav-height;
  214. }
  215. .uni-navbar--fixed {
  216. position: fixed;
  217. z-index: 998;
  218. }
  219. .uni-navbar--shadow {
  220. /* #ifndef APP-NVUE */
  221. box-shadow: 0 1px 6px #ccc;
  222. /* #endif */
  223. }
  224. .uni-navbar--border {
  225. border-bottom-width: 1rpx;
  226. border-bottom-style: solid;
  227. border-bottom-color: $uni-border-color;
  228. }
  229. </style>