vue实现点击按钮左右滚动

<template>
  <div class="app-wrapper">
    <!-- 文本超出,左右按钮滚动 -->
    <div id="click-scroll-X">
      <span class="left_btn" @click="leftSlide">《</span>
      <div class="scroll_wrapper" ref="wrapperCon">
        <div class="scroll_list">
          <div class="item" v-for="(item, index) in classify" :key="index">
            {{ item.encoded }}
          </div>
        </div>
      </div>
      <div class="right_btn" @click="rightSlide">》</div>
    </div>
  </div>
</template>

<script>
export default ({
  name: '',
  components: {},
  props: {},
  data() {
    return {
      timer: null,  // 定时器(方便清除)
      classify: [
        { encoded: 'TJ202209A10' },
        { encoded: 'TJ202209A11' },
        { encoded: 'TJ202209A12' },
        { encoded: 'TJ202209A13' },
        { encoded: 'TJ202209A14' },
        { encoded: 'TJ202209A15' },
        { encoded: 'TJ202209A16' },
        { encoded: 'TJ202209A17' },
        { encoded: 'TJ202209A18' },
        { encoded: 'TJ202209A19' },
        { encoded: 'TJ202209A20' }
      ]
    }
  },
  created() { },
  methods: {
    // 左箭头
    leftSlide() {
      // 保存滚动盒子左侧已滚动的距离
      let left = this.$refs.wrapperCon.scrollLeft
      let num = 0
      clearInterval(this.timer)
      this.timer = null
      this.timer = setInterval(() => {
        //   !left:已经滚动到最左侧
        //   一次性滚动距离(可调节)
        if (!left || num >= 300) {
          // 停止滚动
          clearInterval(this.timer)
          this.timer = null
          return
        }
        // 给滚动盒子元素赋值向左滚动距离
        this.$refs.wrapperCon.scrollLeft = left -= 30
        // 保存向左滚动距离(方便判断一次性滚动多少距离)
        num += 30
      }, 20)
      // 20:速度(可调节)
    },
    // 右箭头
    rightSlide() {
      // 保存滚动盒子左侧已滚动的距离
      let left = this.$refs.wrapperCon.scrollLeft
      // 保存元素的整体宽度
      let scrollWidth = this.$refs.wrapperCon.scrollWidth
      // 保存元素的可见宽度
      let clientWidth = this.$refs.wrapperCon.clientWidth
      let num = 0
      clearInterval(this.timer)
      this.timer = setInterval(() => {
        // 已经滚动距离+可见宽度
        // left+clientWidth>=scrollWidth:滚动到最右侧
        // num>=300一次性滚动距离
        if (left + clientWidth >= scrollWidth || num >= 300) {
          clearInterval(this.timer)
          return
        }
        // 给滚动盒子元素赋值向左滚动距离
        this.$refs.wrapperCon.scrollLeft = left += 30
        // 保存向左滚动距离(方便判断一次性滚动多少距离)
        num += 30
      }, 20)
      // 20:速度(可调节)
    }
  },
})
</script>
<style lang='scss' scoped>
#click-scroll-X {
  display: flex;
  align-items: center;

  .left_btn,
  .right_btn {
    font-size: 30px;
    cursor: pointer;
    margin: -10px -5px 0;
  }

  .scroll_wrapper {
    width: 600px;
    overflow-x: scroll;
    padding-bottom: 10px;

    .scroll_list {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .item {
        cursor: pointer;
        width: 146px;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        border-radius: 3px;
        box-sizing: border-box;
        flex-shrink: 0;
        margin: 0 2px;
      }
    }
  }
}

/*隐藏滚动条*/
.scroll_wrapper::-webkit-scrollbar {
  display: none;
}
</style>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值