uni-nav-bar.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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="22"/>
  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: '12px' }">{{ 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 uni-navbar__header-btns-right" v-if="rightIcon.length">
  26. <uni-icons :color="color" :type="rightIcon" size="22" />
  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" >
  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: 14px;
  140. /* #endif */
  141. }
  142. .uni-nav-bar-right-text {
  143. font-size: 12px;
  144. }
  145. .uni-navbar__content {
  146. position: relative;
  147. background-color: #fff;
  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. padding: 0 10px;
  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: 12px;
  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. /* #ifdef H5 */
  180. cursor: pointer;
  181. /* #endif */
  182. }
  183. .uni-navbar__header-btns-left {
  184. /* #ifndef APP-NVUE */
  185. display: flex;
  186. /* #endif */
  187. width: 120rpx;
  188. justify-content: flex-start;
  189. }
  190. .uni-navbar__header-btns-right {
  191. /* #ifndef APP-NVUE */
  192. display: flex;
  193. /* #endif */
  194. width: 120rpx;
  195. padding-right: 5rpx;
  196. justify-content: flex-end;
  197. }
  198. .uni-navbar__header-container {
  199. flex: 1;
  200. }
  201. .uni-navbar__header-container-inner {
  202. /* #ifndef APP-NVUE */
  203. display: flex;
  204. /* #endif */
  205. flex: 1;
  206. align-items: center;
  207. justify-content: center;
  208. font-size: 12px;
  209. }
  210. .uni-navbar__placeholder-view {
  211. height: $nav-height;
  212. }
  213. .uni-navbar--fixed {
  214. position: fixed;
  215. z-index: 998;
  216. /* #ifdef H5 */
  217. left: var(--window-left);
  218. right: var(--window-right);
  219. /* #endif */
  220. /* #ifndef H5 */
  221. left:0;
  222. right: 0;
  223. /* #endif */
  224. }
  225. .uni-navbar--shadow {
  226. /* #ifndef APP-NVUE */
  227. box-shadow: 0 1px 6px #ccc;
  228. /* #endif */
  229. }
  230. .uni-navbar--border {
  231. border-bottom-width: 1rpx;
  232. border-bottom-style: solid;
  233. border-bottom-color: #eee;
  234. }
  235. </style>