uni-navbar-lite.uvue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. uni.loadFontFace({
  57. global: false,
  58. family: 'UniIconsFontFamily',
  59. source: iconpath,
  60. success() { },
  61. fail(err) {
  62. console.log(err);
  63. },
  64. })
  65. const sys = uni.getSystemInfoSync()
  66. const statusBarHeight = sys.statusBarHeight
  67. this.statusBarHeight = statusBarHeight
  68. },
  69. mounted() {
  70. // TODO 暂时加定时器,否则不生效
  71. setTimeout(() => {
  72. uni.setNavigationBarColor({
  73. frontColor: '#000000',
  74. backgroundColor: '#ffffff'
  75. })
  76. }, 100)
  77. },
  78. methods: {
  79. back() {
  80. uni.navigateBack({})
  81. },
  82. onPerson(){
  83. uni.navigateTo({
  84. url: '/pages/profile/index',
  85. fail: (err: any) => {
  86. console.log('跳转失败', err)
  87. }
  88. })
  89. }
  90. },
  91. }
  92. </script>
  93. <style>
  94. .uni-icons {
  95. font-family: "UniIconsFontFamily" !important;
  96. font-size: 22px;
  97. font-style: normal;
  98. color: #333;
  99. }
  100. .uni-navbar {
  101. border: 1px #eee solid;
  102. background-color: #fff;
  103. }
  104. .uni-navbar-inner {
  105. position: relative;
  106. display: flex;
  107. flex-direction: row;
  108. justify-content: space-between;
  109. height: 45px;
  110. }
  111. .left-content,
  112. .right-content {
  113. display: flex;
  114. justify-content: center;
  115. align-items: center;
  116. width: 45px;
  117. height: 100%;
  118. }
  119. .uni-navbar-content {
  120. position: absolute;
  121. height: 100%;
  122. top: 0;
  123. bottom: 0;
  124. left: 45px;
  125. right: 45px;
  126. display: flex;
  127. flex-direction: row;
  128. justify-content: center;
  129. align-items: center;
  130. }
  131. .show-left{
  132. display: none;
  133. }
  134. .is-left {
  135. justify-content: flex-start;
  136. }
  137. </style>