vue 中横向滚动设置scrollLeft,并且加上过渡动画

本文介绍如何在Vue中实现横向滚动,并结合过渡动画,当点击第四个选项时,内容会平滑移动到目标位置,展示下一个选项。由于`scrollLeft`属性不支持CSS控制,因此采用定时器分步移动的策略来达成动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图一
图一
在这里插入图片描述
图二

如图一所示,需求是比如在点击第四个选项时,会自动移至前方,并显示出下一个选项,到图二效果

由于scrollLeft 不是css中的属性,所以用css无法解决

思路:

将需要移动的距离均分,设置定时器每隔一段时间移动一点,直到 移动至目标距离为止,下面直接上代码

// html
<div class="scrollView" ref="idSwiperImg">
	<div class="chooseImgItem" @click="tabClick(index)" v-for="(item, index) in normalData" :key="index" ref="imgList">
    	<van-image :src="item.pic[0].url" fit="cover" />
    </div>
</div>
// scss
.scrollView {
  width: 240px;
  height: 78px;
  overflow-x: scroll;
  // 隐藏滚动条
  &::-webkit-scrollbar {
    display: none;
  }
  .chooseImgItem {
    margin-left: 8px;
    height: 56px;
    width: 56px;
    border-radius: 4px;
    overflow: hidden;
    .van-image {
      width: 56px;
      height: 56px;
    }
    &:first-child {
      margin-left: 15px;
    }
    &:last-child {
      margin-right: 15px;
    }
  }
}
// js
tabClick(index) {
   // 容器宽度和内部点击的 item 宽度 
   let boxWidth = this.$refs.idSwiperImg.offsetWidth
   let elWidth = this.$refs.imgList[index].offsetWidth
   // 上一个选中的 item 下标 和 当前选中 item 下标
   this.lastItemIndex = this.itemIndex
   this.itemIndex = index

   if (elWidth <= boxWidth / 3) {
   	 // 获取当前容器的 scrollLeft 并计算接下去要移动的目标值
     let distance = this.$refs.idSwiperImg.scrollLeft
     let total = index * 70 - Math.floor( boxWidth / 3 )
     // 上述两者的差值即为要移动的距离,将其细分为小块,确保为正值
     let step = (distance - total) / 50
     if (step < 0) step = -step
     this.moveSlow(distance, total, step)
   }
 },
 moveSlow(distance, total, step) {
   // 正向滚动 和 反向滚动
   if (this.lastItemIndex < this.itemIndex) {
   	 // 每隔1毫秒移动一小段距离,直到移动至目标至为止,反之亦然
     if (distance < total) {
       distance += step
       this.$refs.idSwiperImg.scrollLeft = distance
       setTimeout(() => {
         this.moveSlow(distance, total, step)
       }, 1)
     } else {
       this.$refs.idSwiperImg.scrollLeft = total
     }
   } else if (this.lastItemIndex > this.itemIndex) {
     if (distance > total) {
       distance -= step
       this.$refs.idSwiperImg.scrollLeft = distance
       setTimeout(() => {
         this.moveSlow(distance, total, step)
       }, 1)
     } else {
       this.$refs.idSwiperImg.scrollLeft = total
     }
   }
 }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值