uni-navbar-lite.uvue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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">
  20. <image
  21. src="/static/images/map/3.png"
  22. style="height: 25px;width: 25px;"
  23. @click="handleRightClick"
  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. },
  61. emits: ['back', 'leftClick', 'rightClick'],
  62. data() {
  63. return {
  64. statusBarHeight: 0
  65. };
  66. },
  67. computed: {
  68. navbarStyle() : string {
  69. return `margin-top:${this.statusBarHeight}px`
  70. },
  71. unicode() : string {
  72. return '\ue600'
  73. }
  74. },
  75. created() {
  76. // #ifndef WEB
  77. // 非WEB平台使用原有方式
  78. const iconpath = '/static/css/uniicons.ttf';
  79. uni.loadFontFace({
  80. global: false,
  81. family: 'UniIconsFontFamily',
  82. source: iconpath,
  83. success() { },
  84. fail(err) {
  85. console.log(err);
  86. },
  87. });
  88. // #endif
  89. // #ifdef WEB
  90. // WEB平台使用CSS加载字体
  91. // #endif
  92. const sys = uni.getSystemInfoSync()
  93. const statusBarHeight = sys.statusBarHeight
  94. this.statusBarHeight = statusBarHeight
  95. },
  96. mounted() {
  97. // TODO 暂时加定时器,否则不生效
  98. setTimeout(() => {
  99. uni.setNavigationBarColor({
  100. frontColor: '#000000',
  101. backgroundColor: '#ffffff'
  102. })
  103. }, 100)
  104. },
  105. methods: {
  106. handleLeftClick() {
  107. if (this.enableBack) {
  108. this.$emit('back')
  109. uni.navigateBack({})
  110. } else {
  111. this.$emit('leftClick')
  112. }
  113. },
  114. handleRightClick(){
  115. this.$emit('rightClick')
  116. }
  117. },
  118. }
  119. </script>
  120. <style>
  121. // #ifdef WEB
  122. @font-face {
  123. font-family: 'UniIconsFontFamily';
  124. src: url('/static/css/uniicons.ttf') format('truetype');
  125. }
  126. // #endif
  127. .uni-icons {
  128. font-family: "UniIconsFontFamily" !important;
  129. font-size: 22px;
  130. font-style: normal;
  131. color: #333;
  132. }
  133. .uni-navbar {
  134. border: 1px #eee solid;
  135. background-color: #fff;
  136. }
  137. .uni-navbar-inner {
  138. position: relative;
  139. display: flex;
  140. flex-direction: row;
  141. justify-content: space-between;
  142. height: 45px;
  143. }
  144. .left-content,
  145. .right-content {
  146. display: flex;
  147. justify-content: center;
  148. align-items: center;
  149. width: 45px;
  150. height: 100%;
  151. }
  152. .uni-navbar-content {
  153. position: absolute;
  154. height: 100%;
  155. top: 0;
  156. bottom: 0;
  157. left: 45px;
  158. right: 45px;
  159. display: flex;
  160. flex-direction: row;
  161. justify-content: center;
  162. align-items: center;
  163. }
  164. .show-left{
  165. display: none;
  166. }
  167. .show-right{
  168. display: none;
  169. }
  170. .is-left {
  171. justify-content: flex-start;
  172. }
  173. </style>