dt_scroll_tab.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <view>
  3. <scroll-view scroll-x :scroll-into-view="aim" :scroll-with-animation="true" class="dt-tab-scroll-wrap">
  4. <view class="tab-wrap">
  5. <block v-for="(item, idx) in dataList" :key="idx">
  6. <view :id="'tab'+idx"
  7. class="tab-item"
  8. :class="{
  9. 'active':current==idx,
  10. 'active-line':conf.isActiveLine && current==idx
  11. }"
  12. :style="{'width':conf.width}"
  13. @tap="tapTabItem(idx)">{{item}}
  14. </view>
  15. </block>
  16. </view>
  17. </scroll-view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. props: {
  23. config: {
  24. type: Object,
  25. default: {}
  26. },
  27. current: {// 支持 .sync
  28. type: Number,
  29. default: 0
  30. },
  31. dataList: {
  32. type: Array,
  33. default: () => {
  34. return [] // ['全部','优惠券','好物券','商品','会员卡']
  35. }
  36. }
  37. },
  38. data() {
  39. return {
  40. conf: {}
  41. }
  42. },
  43. watch: {
  44. config: {
  45. deep: true,
  46. handler: (n, o) => {
  47. if (n) {
  48. this.conf = Object.assign({
  49. width: '20%'
  50. }, n)
  51. }
  52. }
  53. },
  54. },
  55. computed: {
  56. aim() {
  57. let aimIdx = this.current + 2
  58. if (this.current <= 2) {
  59. return 'tab' + 0
  60. }
  61. console.log('aim', 61)
  62. if (aimIdx > this.dataList.length) {
  63. console.log(63, 'tab' + this.dataList.length)
  64. return 'tab' + (this.dataList.length - 1)
  65. }
  66. console.log(66)
  67. return 'tab' + (this.current - 2)
  68. },
  69. },
  70. methods: {
  71. tapTabItem(idx) {
  72. this.$emit('update:current', idx)
  73. this.$emit('change', idx)
  74. },
  75. },
  76. created() {
  77. if (this.config) {
  78. this.conf = Object.assign({
  79. width: '20vw'
  80. }, this.config)
  81. }
  82. },
  83. }
  84. </script>
  85. <style lang="scss">
  86. .dt-tab-scroll-wrap {
  87. width: 100vw;
  88. background-color: #fff;
  89. .tab-wrap {
  90. display: flex;
  91. width: max-content;
  92. white-space: nowrap;
  93. height: 90 upx;
  94. line-height: 90 upx;
  95. font-size: 28 upx;
  96. .tab-item {
  97. position: relative;
  98. width: 20vw;
  99. text-align: center;
  100. // padding:0 44upx;
  101. }
  102. .tab-item.active {
  103. color: $base;
  104. }
  105. .tab-item.active-line:before {
  106. position: absolute;
  107. bottom: 0;
  108. left: 50%;
  109. transform: translateX(-50%);
  110. width: 80 upx;
  111. height: 4 upx;
  112. border-radius: 2 upx;
  113. background-color: $base;
  114. content: '';
  115. }
  116. // .tab-item.active:before{
  117. // position:absolute;
  118. // bottom:0;
  119. // content:'';
  120. // }
  121. }
  122. }
  123. </style>