uni-nav-bar.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view class="uni-navbar" id="uni-navbar">
  3. <view :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }" :style="{ 'background': backgroundColor }"
  4. class="uni-navbar__content">
  5. <uni-status-bar v-if="statusBar" />
  6. <view :style="{ color: color,background: 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">
  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. <uni-status-bar v-if="statusBar" />
  38. <view class="uni-navbar__placeholder-view" />
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import uniStatusBar from "../uni-status-bar/uni-status-bar.vue";
  44. import uniIcons from "../uni-icons/uni-icons.vue";
  45. export default {
  46. name: "UniNavBar",
  47. components: {
  48. uniStatusBar,
  49. uniIcons
  50. },
  51. props: {
  52. title: {
  53. type: String,
  54. default: ""
  55. },
  56. leftText: {
  57. type: String,
  58. default: ""
  59. },
  60. rightText: {
  61. type: String,
  62. default: ""
  63. },
  64. leftIcon: {
  65. type: String,
  66. default: ""
  67. },
  68. rightIcon: {
  69. type: String,
  70. default: ""
  71. },
  72. fixed: {
  73. type: [Boolean, String],
  74. default: false
  75. },
  76. color: {
  77. type: String,
  78. default: "#000000"
  79. },
  80. backgroundColor: {
  81. type: String,
  82. default: "#FFFFFF"
  83. },
  84. statusBar: {
  85. type: [Boolean, String],
  86. default: false
  87. },
  88. shadow: {
  89. type: [String, Boolean],
  90. default: false
  91. },
  92. border: {
  93. type: [String, Boolean],
  94. default: true
  95. }
  96. },
  97. created() {
  98. this.$nextTick(()=>{
  99. uni.createSelectorQuery().in(this).select('#uni-navbar').boundingClientRect((res)=>{
  100. this.$emit('onHeight',res);
  101. }).exec();
  102. })
  103. },
  104. mounted() {
  105. if(uni.report && this.title !== '') {
  106. uni.report('title', this.title)
  107. }
  108. },
  109. methods: {
  110. onClickLeft() {
  111. this.$emit("clickLeft");
  112. },
  113. onClickRight() {
  114. this.$emit("clickRight");
  115. }
  116. }
  117. };
  118. </script>
  119. <style lang="scss" scoped>
  120. $nav-height: 44px;
  121. .uni-nav-bar-text {
  122. /* #ifdef APP-PLUS */
  123. font-size: 34rpx;
  124. /* #endif */
  125. /* #ifndef APP-PLUS */
  126. font-size: $uni-font-size-lg;
  127. /* #endif */
  128. font-weight: bold;
  129. }
  130. .uni-nav-bar-right-text {
  131. font-size: $uni-font-size-base;
  132. }
  133. .uni-navbar {
  134. width: 750rpx;
  135. }
  136. .uni-navbar__content {
  137. position: relative;
  138. width: 750rpx;
  139. background-color: $uni-bg-color;
  140. overflow: hidden;
  141. }
  142. .uni-navbar__content_view {
  143. /* #ifndef APP-NVUE */
  144. display: flex;
  145. /* #endif */
  146. align-items: center;
  147. flex-direction: row;
  148. // background-color: #FFFFFF;
  149. }
  150. .uni-navbar__header {
  151. /* #ifndef APP-NVUE */
  152. display: flex;
  153. /* #endif */
  154. flex-direction: row;
  155. width: 750rpx;
  156. height: $nav-height;
  157. line-height: $nav-height;
  158. font-size: 16px;
  159. // background-color: #ffffff;
  160. }
  161. .uni-navbar__header-btns {
  162. /* #ifndef APP-NVUE */
  163. display: flex;
  164. /* #endif */
  165. flex-wrap: nowrap;
  166. width: 120rpx;
  167. padding: 0 6px;
  168. justify-content: center;
  169. align-items: center;
  170. }
  171. .uni-navbar__header-btns-left {
  172. /* #ifndef APP-NVUE */
  173. display: flex;
  174. /* #endif */
  175. width: 150rpx;
  176. justify-content: flex-start;
  177. }
  178. .uni-navbar__header-btns-right {
  179. /* #ifndef APP-NVUE */
  180. display: flex;
  181. /* #endif */
  182. width: 150rpx;
  183. padding-right: 30rpx;
  184. justify-content: flex-end;
  185. }
  186. .uni-navbar__header-container {
  187. flex: 1;
  188. }
  189. .uni-navbar__header-container-inner {
  190. /* #ifndef APP-NVUE */
  191. display: flex;
  192. /* #endif */
  193. flex: 1;
  194. align-items: center;
  195. justify-content: center;
  196. font-size: $uni-font-size-base;
  197. }
  198. .uni-navbar__placeholder-view {
  199. height: $nav-height;
  200. }
  201. .uni-navbar--fixed {
  202. position: fixed;
  203. z-index: 998;
  204. }
  205. .uni-navbar--shadow {
  206. /* #ifndef APP-NVUE */
  207. box-shadow: 0 1px 6px #ccc;
  208. /* #endif */
  209. }
  210. .uni-navbar--border {
  211. border-bottom-width: 1rpx;
  212. border-bottom-style: solid;
  213. border-bottom-color: $uni-border-color;
  214. }
  215. </style>