app-tabs.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <view v-if="tabs && tabs.length" class="app-tabs" :class="{'tabs-fixed': fixed}">
  3. <view class="tabs-item">
  4. <view class="tab-item" v-for="(tab, i) in tabs" :class="{'active': value===i}" :key="i" @click="tabClick(i)">
  5. {{getTabName(tab)}}
  6. </view>
  7. </view>
  8. <view class="tabs-line" :style="{left:lineLift}"></view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. props:{
  14. tabs: Array,
  15. value: { // 当前显示的下标 (使用v-model语法糖: 1.props需为value; 2.需回调input事件)
  16. type: [String, Number],
  17. default(){
  18. return 0
  19. }
  20. },
  21. fixed: Boolean // 是否悬浮,默认false
  22. },
  23. computed: {
  24. lineLift() {
  25. return 100/this.tabs.length*(this.value + 1) - 100/(this.tabs.length*2) + '%'
  26. }
  27. },
  28. methods: {
  29. getTabName(tab){
  30. return typeof tab === "object" ? tab.name : tab
  31. },
  32. tabClick(i){
  33. if(this.value!=i){
  34. this.$emit("input",i);
  35. this.$emit("change",i);
  36. }
  37. }
  38. }
  39. }
  40. </script>
  41. <style>
  42. .app-tabs{
  43. position: relative;
  44. height: 80rpx;
  45. line-height: 80rpx;
  46. font-size: 24rpx;
  47. background-color: #fff;
  48. border-bottom: 1rpx solid #eee;
  49. }
  50. .app-tabs .tabs-item{
  51. display: flex;
  52. text-align: center;
  53. font-size: 28rpx;
  54. }
  55. .app-tabs .tabs-item .tab-item{
  56. flex: 1;
  57. }
  58. .app-tabs .tabs-item .active{
  59. color: red;
  60. }
  61. .app-tabs .tabs-line{
  62. position: absolute;
  63. bottom: 0;
  64. width: 150rpx;
  65. height: 4rpx;
  66. transform: translateX(-50%);
  67. border-radius: 4rpx;
  68. transition: left .3s;
  69. background: red;
  70. }
  71. /*悬浮*/
  72. .app-tabs.tabs-fixed{
  73. z-index: 9999;
  74. position: fixed;
  75. top: var(--window-top);
  76. left: 0;
  77. width: 100%;
  78. }
  79. </style>