详情小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
3.8 KiB

  1. <template>
  2. <view class="lb-tabbar">
  3. <view :class="[
  4. 'lb-tabbar-content',
  5. fixed ? 'lb-tabbar--fixed' : ''
  6. ]"
  7. :style="{
  8. backgroundColor: bgColor,
  9. paddingBottom: `${safeAreaHeight}px`
  10. }">
  11. <view v-if="border"
  12. class="lb-tabbar-border"
  13. :style="{
  14. backgroundColor: borderColor,
  15. top: raisedeHeight + 'px'
  16. }">
  17. </view>
  18. <slot></slot>
  19. </view>
  20. <view v-if="placeholder"
  21. class="lb-tabbar-placeholder"
  22. :style="{
  23. height: `${tabbarHeight}px`
  24. }">
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. // git https://github.com/liub1934/uni-lb-tabbar/tree/master/pages/demos
  30. // demo https://github.liubing.me/uni-lb-tabbar/#/
  31. // fileDes https://ext.dcloud.net.cn/plugin?id=4124
  32. const SAFE_AREA_INSET_BOTTOM = 34
  33. import { getPx } from './utils'
  34. export default {
  35. props: {
  36. value: [String, Number],
  37. height: {
  38. type: String,
  39. default: '50px'
  40. },
  41. iconSize: {
  42. type: String,
  43. default: '22px'
  44. },
  45. textSize: {
  46. type: String,
  47. default: '12px'
  48. },
  49. textTop: {
  50. type: String,
  51. default: '5px'
  52. },
  53. dotColor: {
  54. type: String,
  55. default: '#F56C6C'
  56. },
  57. fixed: {
  58. type: Boolean,
  59. default: true
  60. },
  61. placeholder: {
  62. type: Boolean,
  63. default: true
  64. },
  65. animate: String,
  66. closeAnimateOnRaisede: {
  67. type: Boolean,
  68. default: true
  69. },
  70. border: {
  71. type: Boolean,
  72. default: true
  73. },
  74. borderColor: {
  75. type: String,
  76. default: '#DCDFE6'
  77. },
  78. bgColor: {
  79. type: String,
  80. default: '#FFF'
  81. },
  82. inactiveColor: {
  83. type: String,
  84. default: '#909399'
  85. },
  86. activeColor: {
  87. type: String,
  88. default: '#409EFF'
  89. },
  90. inactiveTextColor: String,
  91. activeTextColor: String,
  92. safeAreaInsetBottom: {
  93. type: Boolean,
  94. default: true
  95. },
  96. hideTabbar: {
  97. type: Boolean,
  98. default: true
  99. },
  100. raisedeScale: {
  101. type: Number,
  102. default: 2
  103. }
  104. },
  105. data () {
  106. return {
  107. active: this.value,
  108. activeItem: {},
  109. tabbarItems: [],
  110. hasRaisede: false,
  111. isIphoneX: false
  112. }
  113. },
  114. computed: {
  115. tabbarItemsLength () {
  116. return this.tabbarItems.length
  117. },
  118. safeAreaHeight () {
  119. return this.isIphoneX && this.safeAreaInsetBottom ? SAFE_AREA_INSET_BOTTOM : 0 // 苹果X等机型安全区高度
  120. },
  121. iconHeight () {
  122. return getPx(this.iconSize)
  123. },
  124. raisedeHeight () {
  125. return this.hasRaisede ? this.iconHeight * this.raisedeScale / 2 : 0 // 凸起高度
  126. },
  127. tabbarHeight () {
  128. const height = getPx(this.height) // 默认高度
  129. const raisedeHeight = this.hasRaisede ? this.iconHeight * this.raisedeScale / 2 : 0 // 凸起高度
  130. const tabbarHeight = height + this.safeAreaHeight + raisedeHeight // 整体高度
  131. return tabbarHeight
  132. }
  133. },
  134. provide () {
  135. return {
  136. tabbar: this
  137. }
  138. },
  139. created () {
  140. if (this.hideTabbar) {
  141. uni.hideTabBar()
  142. }
  143. const res = uni.getSystemInfoSync()
  144. const { model, statusBarHeight } = res
  145. if (
  146. (model.indexOf('iPhone') > -1 && statusBarHeight > 20) ||
  147. model.indexOf('iPhone X') > -1 ||
  148. model.indexOf('iPhone 1') > -1
  149. ) {
  150. this.isIphoneX = true
  151. }
  152. this.getTabbarHeight()
  153. },
  154. methods: {
  155. getTabbarHeight () {
  156. return this.tabbarHeight
  157. }
  158. },
  159. watch: {
  160. value (newVal) {
  161. this.active = newVal
  162. },
  163. active (newVal) {
  164. this.activeItem = this.tabbarItems.find(item => item.name === newVal)
  165. this.$emit('input', newVal)
  166. this.$emit('change', this.activeItem)
  167. },
  168. tabbarItemsLength () {
  169. this.hasRaisede = !!this.tabbarItems.find(item => item.raisede)
  170. }
  171. }
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. @import "./style.scss";
  176. </style>