dt_scroll_tab.vue 2.4 KB

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