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;
    }
  }
}

相关推荐

  1. Vue2聊天框滚动定位

    2024-07-12 16:22:02       51 阅读
  2. qt,滚动,放大缩小拖动图片

    2024-07-12 16:22:02       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 16:22:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 16:22:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 16:22:02       57 阅读
  4. Python语言-面向对象

    2024-07-12 16:22:02       68 阅读

热门阅读

  1. Python进行数据分析:从基础到实践

    2024-07-12 16:22:02       20 阅读
  2. Spring Boot 实现统一异常处理:构建健壮的应用

    2024-07-12 16:22:02       21 阅读
  3. SQL Server触发器的魔法:数据库自动化的瑞士军刀

    2024-07-12 16:22:02       19 阅读
  4. spark 中hint使用总结

    2024-07-12 16:22:02       22 阅读
  5. 安卓文件上传照片单张及多张照片上传实现

    2024-07-12 16:22:02       18 阅读
  6. 编译Linux内核, 制作迷你系统并在虚拟机里运行

    2024-07-12 16:22:02       21 阅读
  7. 力扣1209.删除字符串中的所有相邻重复项 II

    2024-07-12 16:22:02       20 阅读
  8. Python使用总结之jieba形容词提取详解

    2024-07-12 16:22:02       22 阅读
  9. postman接口测试工具详解

    2024-07-12 16:22:02       22 阅读