vue 自定义滚动条同步拖动(移动端)

实现效果,拖动左右箭头实现图片区域同步滚动,到边缘停止拖动。

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

HTML代码

<template>
  <div @touchstart="onClick">
	<!--使用draggable组件 图片列表区域-->
    <draggable
      v-model="select_list"
      @end="onEnd"
      class="display_flex_ac list_all"
      id="imageAssignment"
    >
    </draggable>
	
	<!--自定义拖动条-->
	<div
	  ref="container"
	  class="scroll_bar"
	  @touchend="endDrag"
	  @touchcancel="endDrag"
	  @touchstart="startDrag"
	  @touchmove.prevent="drag"
	>
	  <div
	    ref="draggable"
	    class="display_flex_ac_jc slide"
	    :style="{ transform: `translateX(${left}px)` }"
	  >
	    <a-icon type="double-left" /><a-icon type="double-right" />
	  </div>
	</div>
  </div>
</template>

JS代码

<script>
export default {
  data() {
	return {
	  dragging: false,
      left: 0,
      startX: 0,
      currentX: 0,
      scrollWidth: 0,
      rollingValue: 0,
      containerWidth: 0,
      draggableWidth: 0,
	}
  },
  methods: {
  	// 点击
    onClick(){
      this.containerWidth = this.$refs.container?.offsetWidth;
      this.draggableWidth = this.$refs.draggable?.offsetWidth;
      this.scrollWidth = document.getElementById("imageAssignment")?.scrollWidth;
    },
    // 拖动开始
    startDrag(e) {
      this.dragging = true;
      this.startX = e.touches[0].clientX - this.left;
    },
    // 拖动中
    drag(e) {
      if (this.dragging) {
        const minLeft = 0;
        const newX = e.touches[0].clientX - this.startX;
        const maxLeft = this.containerWidth - this.draggableWidth; // 去掉拖动元素宽度
        this.left = Math.max(minLeft, Math.min(maxLeft, newX)); // 当前拖动值

        const maxRight = this.scrollWidth - this.containerWidth; // 去掉当前屏幕
        this.rollingValue = this.onRolling(maxRight, maxLeft, this.left); // 计算滚动值
        document.getElementById("imageAssignment").scrollLeft = this.rollingValue;
      }
    },
    // 拖动结束
    endDrag() {
      this.dragging = false;
    },
    // 计算滚动条拖动值
    onRolling(maxRight, maxLeft, value) {

      // 计算比例关系
      const ratio = value / maxLeft;

      // 计算最终的值
      const finalValue = maxRight * ratio;

      // 返回最终值
      return finalValue;
    },
    // 拖动结束
    async onEnd(e) {
      // 同步滚动条位置
      let left = e.target.scrollLeft;
      const maxLeft = this.containerWidth - this.draggableWidth; // 去掉拖动元素宽度
      const maxRight = this.scrollWidth - this.containerWidth; // 去掉当前屏幕
      this.left = this.onRolling(maxLeft, maxRight, left); // 计算滚动条

      // 替换变化数据
      this.setImage_list(this.select_list);
    }
  }
}
</script>

CSS代码

.scroll_bar{
  width: 100%;
  overflow: auto;
  .slide{
    z-index: 1;
    height: 16px;
    bottom: -7px;
    opacity: 0.8;
    padding: 0 5px;
    position: absolute;
    border-radius: 5px;
    background: #fff;
    box-shadow: 0 0px 3px #999;
    .anticon{
      font-size: 12px;
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值