vue2滑块校验手动实现,定义滑块和滑块背景图,拖动校验

  1. 在项目中,创建一个组件,例如 SliderVerification
    <template>
      <div class="slider-verification">
        <div class="background" :style="backgroundStyle"></div>
        <div class="slider" :style="sliderStyle" :mousedown="onMouseDown"></div>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          isDragging: false,
          startX: 0,
          offsetX: 0,
        };
      },
      computed: {
        backgroundStyle() {
          return `background-image: url('path/to/background-image.jpg');`;
        },
        sliderStyle() {
          return `background-image: url('path/to/slider-image.png'); left: ${this.offsetX}px;`;
        },
      },
      methods: {
        onMouseDown(event) {
          event.preventDefault();
          this.isDragging = true;
          this.startX = event.clientX;
        },
        onMouseMove(event) {
          if (this.isDragging) {
            const moveX = event.clientX - this.startX;
            this.offsetX = Math.max(0, Math.min(moveX, 200)); // 设置滑块最大可拖动距离为 200px
          }
        },
        onMouseUp(event) {
          if (this.isDragging) {
            this.isDragging = false;
    
            if (this.offsetX >= 200) {
              // 拖动校验成功,触发相应操作
              // 在这里可以添加你的逻辑代码
              console.log("Verification passed!");
            } else {
              // 校验失败时,重置滑块位置
              this.offsetX = 0;
            }
          }
        },
      },
      mounted() {
        document.addEventListener("mousemove", this.onMouseMove);
        document.addEventListener("mouseup", this.onMouseUp);
      },
      beforeDestroy() {
        document.removeEventListener("mousemove", this.onMouseMove);
        document.removeEventListener("mouseup", this.onMouseUp);
      },
    };
    </script>
    
    <style scoped>
    .slider-verification {
      position: relative;
      width: 300px; /* 设置组件容器宽度 */
      height: 80px; /* 设置组件容器高度 */
      overflow: hidden;
    }
    
    .background {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-repeat: no-repeat;
      background-size: contain;
    }
    
    .slider {
      position: absolute;
      top: 0;
      width: 80px; /* 设置滑块宽度 */
      height: 80px; /* 设置滑块高度 */
      background-repeat: no-repeat;
      background-size: contain;
      cursor: pointer;
      user-select: none;
      pointer-events: none;
    }
    </style>

  2. 组件 SliderVerification 引入到你的页面中,并使用它:
    <template>
      <div>
        <h1>滑块校验示例</h1>
        <SliderVerification></SliderVerification>
      </div>
    </template>
    
    <script>
    import SliderVerification from "./components/SliderVerification.vue";
    
    export default {
      components: {
        SliderVerification,
      },
    };
    </script>

    在上述示例中,通过监听鼠标事件实现了滑块的拖动验证。当滑块的拖动距离达到一定的阈值(在示例中设置为 200px)时,即可认为验证成功,你可以在 onMouseUp 方法中对验证通过时的逻辑进行处理。

    请确保将 path/to/background-image.jpg 和 path/to/slider-image.png 更换为你自己的图片路径。另外,你可以根据需要调整组件的宽度、高度和样式

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值