uni-navbar-lite.uvue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="uni-navbar">
  3. <view class="uni-navbar-inner" :style="navbarStyle">
  4. <view class="left-content" @click="back">
  5. <text class="uni-icons" :class="{'show-left':!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">
  11. <slot name="right">
  12. <view class="custom-right">
  13. <image
  14. src="/static/images/index/person.png"
  15. style="height: 25px;width: 25px;"
  16. @click="onPerson"
  17. />
  18. </view>
  19. </slot>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. // import iconpath from './uniicons.ttf'
  26. export default {
  27. name: "uni-navbar",
  28. props: {
  29. title: {
  30. type: String,
  31. default: ''
  32. },
  33. isLeft: {
  34. type: Boolean,
  35. default: false
  36. },
  37. showLeft:{
  38. type: Boolean,
  39. default: true
  40. },
  41. },
  42. data() {
  43. return {
  44. statusBarHeight: 0
  45. };
  46. },
  47. computed: {
  48. navbarStyle() : string {
  49. return `margin-top:${this.statusBarHeight}px`
  50. },
  51. unicode() : string {
  52. return '\ue600'
  53. }
  54. },
  55. created() {
  56. // #ifndef WEB
  57. // 非WEB平台使用原有方式
  58. const iconpath = '/static/css/uniicons.ttf';
  59. uni.loadFontFace({
  60. global: false,
  61. family: 'UniIconsFontFamily',
  62. source: iconpath,
  63. success() { },
  64. fail(err) {
  65. console.log(err);
  66. },
  67. });
  68. // #endif
  69. // #ifdef WEB
  70. // WEB平台使用CSS加载字体
  71. // #endif
  72. const sys = uni.getSystemInfoSync()
  73. const statusBarHeight = sys.statusBarHeight
  74. this.statusBarHeight = statusBarHeight
  75. },
  76. mounted() {
  77. // TODO 暂时加定时器,否则不生效
  78. setTimeout(() => {
  79. uni.setNavigationBarColor({
  80. frontColor: '#000000',
  81. backgroundColor: '#ffffff'
  82. })
  83. }, 100)
  84. },
  85. methods: {
  86. back() {
  87. uni.navigateBack({})
  88. },
  89. onPerson(){
  90. uni.navigateTo({
  91. url: '/pages/profile/index',
  92. fail: (err: any) => {
  93. console.log('跳转失败', err)
  94. }
  95. })
  96. }
  97. },
  98. }
  99. </script>
  100. <style>
  101. // #ifdef WEB
  102. @font-face {
  103. font-family: 'UniIconsFontFamily';
  104. src: url('/static/css/uniicons.ttf') format('truetype');
  105. }
  106. // #endif
  107. .uni-icons {
  108. font-family: "UniIconsFontFamily" !important;
  109. font-size: 22px;
  110. font-style: normal;
  111. color: #333;
  112. }
  113. .uni-navbar {
  114. border: 1px #eee solid;
  115. background-color: #fff;
  116. }
  117. .uni-navbar-inner {
  118. position: relative;
  119. display: flex;
  120. flex-direction: row;
  121. justify-content: space-between;
  122. height: 45px;
  123. }
  124. .left-content,
  125. .right-content {
  126. display: flex;
  127. justify-content: center;
  128. align-items: center;
  129. width: 45px;
  130. height: 100%;
  131. }
  132. .uni-navbar-content {
  133. position: absolute;
  134. height: 100%;
  135. top: 0;
  136. bottom: 0;
  137. left: 45px;
  138. right: 45px;
  139. display: flex;
  140. flex-direction: row;
  141. justify-content: center;
  142. align-items: center;
  143. }
  144. .show-left{
  145. display: none;
  146. }
  147. .is-left {
  148. justify-content: flex-start;
  149. }
  150. </style>