uni-navbar-lite.uvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="uni-navbar">
  3. <view class="uni-navbar-inner" :style="navbarStyle">
  4. <view class="left-content" :class="{'show-left':!showLeft}" @click="handleLeftClick">
  5. <text class="uni-icons" v-if="showLeft">{{unicode}}</text>
  6. </view>
  7. <view class="uni-navbar-content" :class="{'is-left':isLeft}">
  8. <slot>{{title}}</slot>
  9. </view>
  10. <view class="right-content" :class="{'show-right':!showRight}">
  11. <slot name="right">
  12. <!-- <view class="custom-right" v-if="showRight && enablePerson">
  13. <image
  14. src="/static/images/index/person.png"
  15. style="height: 25px;width: 25px;"
  16. @click="handleRightClick"
  17. />
  18. </view> -->
  19. <view class="custom-right" v-if="showRight && enablePerson" @click="handleRightClick">
  20. <text v-if="rightBtnText" class="custom-right-text" :style="rightBtnStyle">{{rightBtnText}}</text>
  21. <image v-else
  22. src="/static/images/map/3.png"
  23. style="height: 25px;width: 25px;"
  24. />
  25. </view>
  26. </slot>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. // import iconpath from './uniicons.ttf'
  33. export default {
  34. name: "uni-navbar",
  35. props: {
  36. title: {
  37. type: String,
  38. default: ''
  39. },
  40. isLeft: {
  41. type: Boolean,
  42. default: false
  43. },
  44. showLeft:{
  45. type: Boolean,
  46. default: true
  47. },
  48. showRight:{
  49. type: Boolean,
  50. default: true
  51. },
  52. enableBack:{
  53. type: Boolean,
  54. default: true
  55. },
  56. enablePerson:{
  57. type: Boolean,
  58. default: true
  59. },
  60. rightBtnText: {
  61. type: String,
  62. default: ''
  63. },
  64. rightBtnStyle: {
  65. type: Object,
  66. default: () => ({})
  67. }
  68. },
  69. emits: ['back', 'leftClick', 'rightClick'],
  70. data() {
  71. return {
  72. statusBarHeight: 0
  73. };
  74. },
  75. computed: {
  76. navbarStyle() : string {
  77. return `margin-top:${this.statusBarHeight}px`
  78. },
  79. unicode() : string {
  80. return '\ue600'
  81. }
  82. },
  83. created() {
  84. // #ifndef WEB
  85. // 非WEB平台使用原有方式
  86. const iconpath = '/static/css/uniicons.ttf';
  87. uni.loadFontFace({
  88. global: false,
  89. family: 'UniIconsFontFamily',
  90. source: iconpath,
  91. success() { },
  92. fail(err) {
  93. console.log(err);
  94. },
  95. });
  96. // #endif
  97. // #ifdef WEB
  98. // WEB平台使用CSS加载字体
  99. // #endif
  100. const sys = uni.getSystemInfoSync()
  101. const statusBarHeight = sys.statusBarHeight
  102. this.statusBarHeight = statusBarHeight
  103. },
  104. mounted() {
  105. // TODO 暂时加定时器,否则不生效
  106. setTimeout(() => {
  107. uni.setNavigationBarColor({
  108. frontColor: '#000000',
  109. backgroundColor: '#ffffff'
  110. })
  111. }, 100)
  112. },
  113. methods: {
  114. handleLeftClick() {
  115. if (this.enableBack) {
  116. this.$emit('back')
  117. uni.navigateBack({})
  118. } else {
  119. this.$emit('leftClick')
  120. }
  121. },
  122. handleRightClick(){
  123. this.$emit('rightClick')
  124. }
  125. },
  126. }
  127. </script>
  128. <style>
  129. // #ifdef WEB
  130. @font-face {
  131. font-family: 'UniIconsFontFamily';
  132. src: url('/static/css/uniicons.ttf') format('truetype');
  133. }
  134. // #endif
  135. .uni-icons {
  136. font-family: "UniIconsFontFamily" !important;
  137. font-size: 22px;
  138. font-style: normal;
  139. color: #333;
  140. }
  141. .uni-navbar {
  142. border: 1px #eee solid;
  143. background-color: #fff;
  144. }
  145. .uni-navbar-inner {
  146. position: relative;
  147. display: flex;
  148. flex-direction: row;
  149. justify-content: space-between;
  150. height: 45px;
  151. }
  152. .left-content {
  153. display: flex;
  154. justify-content: center;
  155. align-items: center;
  156. width: 55px;
  157. height: 100%;
  158. }
  159. .right-content {
  160. display: flex;
  161. justify-content: center;
  162. align-items: center;
  163. width: 55px;
  164. height: 100%;
  165. }
  166. .uni-navbar-content {
  167. position: absolute;
  168. height: 100%;
  169. top: 0;
  170. bottom: 0;
  171. left: 55px;
  172. right: 55px;
  173. display: flex;
  174. flex-direction: row;
  175. justify-content: center;
  176. align-items: center;
  177. }
  178. .show-left{
  179. display: none;
  180. }
  181. .show-right{
  182. display: none;
  183. }
  184. .is-left {
  185. justify-content: flex-start;
  186. }
  187. .custom-right-text {
  188. font-size: 28rpx;
  189. color: #333333;
  190. }
  191. </style>