uni-nav-bar.vue 6.2 KB

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