v-for内所有div中的内容(多行多列)全部自适应自动左右滚动(适用于表格)vue2

html部分

 <div class="table_content">
            <div v-for="(item, outerIndex) in table2Data"  :key="outerIndex"           style="display: flex">
              <div
                style="width: 33%"
                v-for="(data,innerIndex) in item.list"
                :key="`${outerIndex}-${innerIndex}`" class="scroll-container"
                :ref="`scrollContainer-${outerIndex}-${innerIndex}`"
                @mouseenter="pauseScroll(outerIndex, innerIndex)"
                @mouseleave="resumeScroll(outerIndex, innerIndex)"
                @click="getOne(item)"
              >
                <div class="scrollable-content">
                  {{ data }}
                </div>
              </div>

            </div>
        </div>

css部分

.table_content{
      width: 100%;
      height: calc(100% - 2.12rem);
      overflow: auto;
      color: #999999;

      tr{
        font-size: 1.25rem;
        height: 1.9rem;
      }
    }

    .table_content::-webkit-scrollbar{
      height: 0;
      width: 0;
    }
.scroll-container{
  //overflow-x: auto;
  //text-overflow: ellipsis;
  white-space: nowrap;
  position: relative;
  margin-right: 0.5rem;
  text-align: center;
}

.scroll-container{
  overflow-x: auto;
}
.scroll-container::-webkit-scrollbar {
  display: none; /* Chrome, Safari, Edge */
}

js部分

data(){
   return:{
         table2Data: [
        {
          name: 'xxxx实业有限公司',
          timeLimit: '2021.1~2023.2',
          region: '1层A区',
          list:[11111111111111111111,1111111111111111111,1111111111111111111111],
        },
        {
          name: 'xxxx实业有限公司',
          timeLimit: '2021.1~2023.2',
          region: '1层A区',
          list:[11111111111111111111,1111111111111111111,1111111111111111111111],
        },
      ],
    }
},
mounted:{
    setTimeout(()=>{
        this.startAutoScroll()
      },1000)
},

methods:{
startAutoScroll() {
      this.table2Data.forEach((item, outerIndex) => {
        item.list.forEach((_, innerIndex) => {
          this.scrollPositions[outerIndex] = this.scrollPositions[outerIndex] || [];
          this.scrollPositions[outerIndex][innerIndex] = 0;
          this.scrollTimers[`${outerIndex}-${innerIndex}`] = setInterval(() => {
            let container = this.$refs[`scrollContainer-${outerIndex}-${innerIndex}`][0]
            if (container.scrollWidth > container.clientWidth) {
              container.scrollLeft += 1; // 水平向右滚动
              if (container.scrollLeft >= container.scrollWidth - container.clientWidth) {
                container.scrollLeft = 0; // 滚动到最右侧后重置到最左侧
              }
            }
          }, 100);
        });
      });
    },
pauseScroll(outerIndex, innerIndex) {
      const timerKey = `${outerIndex}-${innerIndex}`;
      if (this.scrollTimers[timerKey]) {
        clearInterval(this.scrollTimers[timerKey]);
        delete this.scrollTimers[timerKey];
      }
    },
    resumeScroll(outerIndex, innerIndex) {
      const timerKey = `${outerIndex}-${innerIndex}`;
      if (!this.scrollTimers[timerKey]) {
        this.scrollTimers[timerKey] = setInterval(() => {
          let container = this.$refs[`scrollContainer-${outerIndex}-${innerIndex}`][0]
          if (container.scrollWidth > container.clientWidth) {
            container.scrollLeft += 1; // 水平向右滚动
            if (container.scrollLeft >= container.scrollWidth - container.clientWidth) {
              container.scrollLeft = 0; // 滚动到最右侧后重置到最左侧
            }
          }
        }, 100);
      }
    },
},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值