vue @mousemove实现拖动,鼠标移动过快拖动卡顿

需求

使用vue实现滑动拼图验证码

踩到的坑

使用@mousemove绑定事件拖拽速度过快有严重的卡顿
未优化之前效果图

源代码

<template>
  <div class="slider-verify">
    <div class="img">
      <img src="./101.jpg" class="big-img" />
      <img
        ref="block"
        src="./101.jpg"
        class="small-img"
        @mousemove="handleDragMove"
        @mousedown="handleDragStart"
        @mouseup="handleDragEnd"
      />
    </div>
    <div ref="sliderContainer" class="sliderContainer">
      <div ref="sliderMask" class="sliderMask">
        <div ref="slider" class="slider">
          <i
            class="el-icon-right"
            @mousemove="handleDragMove"
            @mousedown="handleDragStart"
            @mouseup="handleDragEnd"
          ></i>
        </div>
      </div>
      <span class="sliderText">向右滑动填充拼图</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "SliderVerify",
  props: {
    width: {
      type: Number,
      default: 310
    }
  },
  data() {
    return {
      isMouseDown: false,
      originX: 0,
      originY: 0,
      slider: null,
      sliderMask: null,
      sliderContainer: null,
      block: null
    };
  },
  created() {
    this.$nextTick(() => {
      this.slider = this.$refs.slider;
      this.sliderMask = this.$refs.sliderMask;
      this.sliderContainer = this.$refs.sliderContainer;
      this.block = this.$refs.block;
    });
  },
  methods: {
    handleDragMove(e) {
      if (!this.isMouseDown) return false;
      const w = this.width;
      // 获取拖拽移动的距离
      const eventX = e.clientX || e.touches[0].clientX;
      const moveX = eventX - this.originX;
      if (moveX < 0 || moveX + 40 >= w) return false;
      this.slider.style.left = moveX + "px";
      this.block.style.left = moveX + "px";
      this.sliderMask.style.width = moveX + "px";
    },
    handleDragStart(e) {
      // 获取拖拽起始位置坐标
      this.originX = e.clientX || e.touches[0].clientX;
      this.originY = e.clientY || e.touches[0].clientY;
      this.isMouseDown = true;
    },
    handleDragEnd(e) {
      if (!this.isMouseDown) return false;
      this.isMouseDown = false;
      const eventX = e.clientX || e.changedTouches[0].clientX;
      if (eventX === this.originX) return false;
    }
  }
};
</script>
...

解决方案

使用JS原生事件替代Vue v-on事件

优化后代码

<template>
  <div class="slider-verify">
    <div class="img">
      <img src="./101.jpg" class="big-img" />
      <img
        ref="block"
        src="./101.jpg"
        class="small-img"
        @mousedown="handleDragStart"
      />
    </div>
    <div ref="sliderContainer" class="sliderContainer">
      <div ref="sliderMask" class="sliderMask">
        <div ref="slider" class="slider">
          <i
            class="el-icon-right"
            @mousedown="handleDragStart"
          ></i>
        </div>
      </div>
      <span class="sliderText">向右滑动填充拼图</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "SliderVerify",
  props: {
    width: {
      type: Number,
      default: 310
    }
  },
  data() {
    return {
      isMouseDown: false,
      originX: 0,
      originY: 0,
      slider: null,
      sliderMask: null,
      sliderContainer: null,
      block: null
    };
  },
  created() {
    this.$nextTick(() => {
      this.slider = this.$refs.slider;
      this.sliderMask = this.$refs.sliderMask;
      this.sliderContainer = this.$refs.sliderContainer;
      this.block = this.$refs.block;
    });
  },
  methods: {
    handleDragStart(e) {
      // 获取拖拽起始位置坐标
      this.originX = e.clientX || e.touches[0].clientX;
      this.originY = e.clientY || e.touches[0].clientY;
      this.isMouseDown = true;
      document.onmousemove = (ev) => {
        if (!this.isMouseDown) return false;
        const w = this.width;
        // 获取拖拽移动的距离
        const eventX = ev.clientX || ev.touches[0].clientX;
        const moveX = eventX - this.originX;
        if (moveX < 0 || moveX + 40 >= w) return false;
        this.slider.style.left = moveX + "px";
        this.block.style.left = moveX + "px";
        this.sliderMask.style.width = moveX + "px";
      };
      document.onmouseup = (ev) => {
        if (!this.isMouseDown) return false;
        this.isMouseDown = false;
        const eventX = ev.clientX || ev.changedTouches[0].clientX;
        if (eventX === this.originX) return false;
      };
    }
  }
};
</script>
...

优化后效果

优化后效果

Ending…

实现 div 横向拖动并互换位置,可以使用 Vue.js 和一些 JavaScript 库来实现。 首先,你需要在 Vue.js 中创建一个拖动事件。你可以使用 `v-on:mousemove` 来监听鼠标移动事件,并使用 `v-on:mousedown` 来监听鼠标按下事件。在鼠标按下事件中,你需要记录鼠标的当前位置。 接下来,在鼠标移动事件中,你需要计算鼠标的偏移量,并将偏移量添加到 div 的 left 样式中。这将使 div 沿着 x 轴移动。 当鼠标释放时,你需要计算 div 的位置,并使用 JavaScript 库来实现 div 位置的互换。你可以使用 Sortable.js 或者 Draggable.js 这样的库来实现拖拽和位置交换。 最后,你需要将这些逻辑封装在一个 Vue.js 组件中,以便在应用程序中使用。以下是一个简单的示例: ```html <template> <div class="container"> <div v-for="(item, index) in items" :key="index" :class="{ active: activeIndex === index }" v-bind:style="{ left: item.left + 'px' }" v-on:mousedown="startDrag(index, $event)"> {{ item.text }} </div> </div> </template> <script> import Sortable from 'sortablejs'; export default { data() { return { items: [ { text: 'Item 1', left: 0 }, { text: 'Item 2', left: 100 }, { text: 'Item 3', left: 200 }, { text: 'Item 4', left: 300 }, ], activeIndex: null, startX: 0, }; }, mounted() { const container = this.$el.querySelector('.container'); Sortable.create(container, { animation: 150, swapThreshold: 0.5, onSwap: (evt) => { const { oldIndex, newIndex } = evt; const item = this.items.splice(oldIndex, 1)[0]; this.items.splice(newIndex, 0, item); }, }); }, methods: { startDrag(index, event) { this.activeIndex = index; this.startX = event.clientX; window.addEventListener('mousemove', this.drag); window.addEventListener('mouseup', this.stopDrag); }, drag(event) { const deltaX = event.clientX - this.startX; this.items[this.activeIndex].left += deltaX; this.startX = event.clientX; }, stopDrag() { window.removeEventListener('mousemove', this.drag); window.removeEventListener('mouseup', this.stopDrag); this.activeIndex = null; }, }, }; </script> <style> .container { display: flex; position: relative; width: 100%; height: 100px; background-color: #f5f5f5; } .container > div { position: absolute; top: 0; height: 100%; width: 100px; background-color: #fff; border: 1px solid #ccc; display: flex; justify-content: center; align-items: center; cursor: move; } .container > div.active { z-index: 1; } </style> ``` 在这个示例中,我们创建了一个包含多个 div 的容器,并使用 Vue.js 的数据绑定将每个 div 的位置绑定到数据模型中。当用户按下鼠标并开始拖动 div 时,我们记录 div 的当前位置,并在每次移动时计算偏移量。当用户释放鼠标时,我们使用 Sortable.js 库来实现 div 的位置交换。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值