vue3拖拽排序,拖拽过程中滚轮可以操作滚动条滚动并且距离底部一定距离就让滚动条滚动

<div class="form-list" ref="scrollContainer">
	<VueDraggable
          handle=".handle"
          chosen-class="ghost"
          :scroll-sensitivity="150"
          :scroll-speed="10"
          :scroll="true"
          :bubble-scroll="false"
          :animation="300"
          ref="el"
          v-model="form.list"
          @end="handleDragEnd"
        >
          <template v-for="(element, index) in form.list" :key="element.seqNo">
            <div class="form-item">
              <div class="sort-icon handle">
                <img
                  v-if="dialogType === 'edit' && form.list.length > 1"
                  src="@/assets/image/sort.svg"
                  style="width: 20px"
                />
              </div>
          </template>
    </VueDraggable>
 </div>
 <script>
 import { VueDraggable } from 'vue-draggable-plus';
 const handleDrag = (event: DragEvent) => {
    lastClientY = event.clientY;
    if (!scrollInterval) {
      scrollInterval = setInterval(() => {
        if (!scrollContainer.value) return;
        const { top, bottom } = scrollContainer.value.getBoundingClientRect();
        const threshold = 100;
        if (lastClientY < top + threshold) {
          scrollContainer.value?.scrollTo(
            0,
            scrollContainer.value.scrollTop - 10,
          );
        } else if (lastClientY > bottom - threshold) {
          scrollContainer.value?.scrollTo(
            0,
            scrollContainer.value.scrollTop + 10,
          );
        }
      }, 16);
    }
  };
  const throttle = (func: Function, wait: number) => {
  let timeout: NodeJS.Timeout | null = null;
  return function(this: any, ...args: any[]) {
    const context = this;
    if (!timeout) {
      timeout = setTimeout(() => {
        func.apply(context, args);
        timeout = null;
      }, wait);
    }
  };
}
  const handleWheel = (event: WheelEvent) => {
    event.preventDefault(); // 阻止默认的滚轮事件
    const delta = Math.sign(event.deltaY);
    scrollContainer.value?.scrollTo(
      0,
      scrollContainer.value.scrollTop + delta,
    );
  };
  const handleDragEnd = () => {
    if (scrollInterval) {
      clearInterval(scrollInterval);
      scrollInterval = null;
    }
  };
  onMounted(() => {
    document.addEventListener('drag', handleDrag);
    document.addEventListener('dragend', handleDragEnd);
    const throttledHandleWheel = throttle(handleWheel, 100);
    document.addEventListener('wheel', throttledHandleWheel, { passive: false });
  });

  onBeforeUnmount(() => {
    document.removeEventListener('drag', handleDrag);
    document.removeEventListener('dragend', handleDragEnd);
  });
  </script>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值