uni-nav-bar.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="uni-navbar" :class="{'uni-navbar-fixed':isFixed,'uni-navbar-shadow':hasShadow}" :style="{'background-color':backgroundColor}">
  3. <uni-status-bar v-if="insertStatusBar"></uni-status-bar>
  4. <view class="uni-navbar-header" :style="{color:color}">
  5. <view class="uni-navbar-header-btns" @tap="onClickLeft">
  6. <view v-if="leftIcon.length">
  7. <image class="icon icon-left" src="../../../static/image/icon/icon-down.png"></image>
  8. </view>
  9. <view v-if="leftText.length" class="uni-navbar-btn-text" :class="{'uni-navbar-btn-icon-left':!leftIcon.length}">{{leftText}}</view>
  10. <slot name="left"></slot>
  11. </view>
  12. <view class="uni-navbar-container">
  13. <view v-if="title.length" class="uni-navbar-container-title">{{title}}</view>
  14. <!-- 标题插槽 -->
  15. <slot></slot>
  16. </view>
  17. <view class="uni-navbar-header-btns" @tap="onClickRight">
  18. <view v-if="rightIcon.length">
  19. <image class="icon icon-right" src="../../../static/image/icon/icon-down.png"></image>
  20. </view>
  21. <!-- 优先显示图标 -->
  22. <view v-if="rightText.length&&!rightIcon.length" class="uni-navbar-btn-text">{{rightText}}</view>
  23. <slot name="right"></slot>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import uniStatusBar from './uni-status-bar.vue'
  30. export default {
  31. components: {
  32. uniStatusBar,
  33. },
  34. props: {
  35. /**
  36. * 标题文字
  37. */
  38. title: {
  39. type: String,
  40. default: ''
  41. },
  42. /**
  43. * 左侧按钮文本
  44. */
  45. leftText: {
  46. type: String,
  47. default: ''
  48. },
  49. /**
  50. * 右侧按钮文本
  51. */
  52. rightText: {
  53. type: String,
  54. default: ''
  55. },
  56. /**
  57. * 左侧按钮图标
  58. */
  59. leftIcon: {
  60. type: String,
  61. default: ''
  62. },
  63. /**
  64. * 右侧按钮图标
  65. */
  66. rightIcon: {
  67. type: String,
  68. default: ''
  69. },
  70. /**
  71. * 是否固定在顶部
  72. */
  73. fixed: {
  74. type: [Boolean, String],
  75. default: false
  76. },
  77. /**
  78. * 按钮图标和文字颜色
  79. */
  80. color: {
  81. type: String,
  82. default: '#000000'
  83. },
  84. /**
  85. * 背景颜色
  86. */
  87. backgroundColor: {
  88. type: String,
  89. default: '#FFFFFF'
  90. },
  91. /**
  92. * 是否包含状态栏,默认固定在顶部时包含
  93. */
  94. statusBar: {
  95. type: [Boolean, String],
  96. default: ''
  97. },
  98. /**
  99. * 是否使用阴影,默认根据背景色判断
  100. */
  101. shadow: {
  102. type: String,
  103. default: ''
  104. }
  105. },
  106. computed: {
  107. isFixed() {
  108. return String(this.fixed) === 'true'
  109. },
  110. insertStatusBar() {
  111. switch (String(this.statusBar)) {
  112. case 'true':
  113. return true
  114. case 'false':
  115. return false
  116. default:
  117. return this.isFixed
  118. }
  119. },
  120. hasShadow() {
  121. var backgroundColor = this.backgroundColor
  122. switch (String(this.shadow)) {
  123. case 'true':
  124. return true
  125. case 'false':
  126. return false
  127. default:
  128. return backgroundColor !== 'transparent' && backgroundColor.indexOf('rgba') < 0
  129. }
  130. }
  131. },
  132. methods: {
  133. /**
  134. * 左侧按钮点击事件
  135. */
  136. onClickLeft() {
  137. this.$emit('click-left')
  138. },
  139. /**
  140. * 右侧按钮点击事件
  141. */
  142. onClickRight() {
  143. this.$emit('click-right')
  144. }
  145. }
  146. }
  147. </script>
  148. <style>
  149. .uni-navbar {
  150. display: block;
  151. position: relative;
  152. width: 100%;
  153. background-color: #FFFFFF;
  154. overflow: hidden;
  155. }
  156. .icon{
  157. width: 30rpx;
  158. height: 18rpx;
  159. }
  160. .icon-left{
  161. transform: rotate(90deg);
  162. margin-left: 10rpx;
  163. }
  164. .icon-right{
  165. transform: rotate(-90deg);
  166. margin-right: 10rpx;
  167. }
  168. .uni-navbar view{
  169. line-height:44px;
  170. }
  171. .uni-navbar-shadow {
  172. box-shadow: 0 1px 6px #ccc;
  173. }
  174. .uni-navbar.uni-navbar-fixed {
  175. position: fixed;
  176. z-index: 998;
  177. }
  178. .uni-navbar-header {
  179. display: flex;
  180. flex-direction: row;
  181. width: 100%;
  182. height:44px;
  183. line-height:44px;
  184. font-size: 16px;
  185. }
  186. .uni-navbar-header .uni-navbar-header-btns{
  187. display:inline-flex;
  188. flex-wrap:nowrap;
  189. flex-shrink:0;
  190. width: 120upx;
  191. padding:0 12upx;
  192. }
  193. .uni-navbar-header .uni-navbar-header-btns:first-child{
  194. padding-left:0;
  195. }
  196. .uni-navbar-header .uni-navbar-header-btns:last-child{
  197. width: 60upx;
  198. }
  199. .uni-navbar-container{
  200. width:100%;
  201. margin:0 10upx;
  202. }
  203. .uni-navbar-container-title{
  204. font-size:30upx;
  205. text-align:center;
  206. padding-right: 60upx;
  207. }
  208. </style>