返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue+elementui实现拖住滑块拼图验证
  • 423
分享到

vue+elementui实现拖住滑块拼图验证

2024-04-02 19:04:59 423人浏览 独家记忆
摘要

Vue拖住滑块拼图验证,以下是cavas直接写的滑块拼图验证码,直接复制引用即可 ​<template>   <div id="puzzle" ref="puzz

Vue拖住滑块拼图验证,以下是cavas直接写的滑块拼图验证码,直接复制引用即可

​<template>
  <div id="puzzle" ref="puzzle" style="display:inline-block;">
  <!-- :style="'padding:' + 16*scale + 'px ' + 16*scale + 'px ' + 28*scale + 'px;border-radius:'+16*scale+'px;'" -->
    <div class="puzzle-container"
     >
      <div :style="'position:relative;overflow:hidden;width:'+ dataWidth +'px;'">
        <div :style="'position:relative;width:' + dataWidth + 'px;height:' + dataHeight + 'px;'">
          <img
            id="scream"
            ref="scream"
            :src="imgRandom"
            :style="'width:' + dataWidth + 'px;height:' + dataHeight + 'px;'"
          />
          <canvas id="puzzle-box" ref="puzzleBox" :width="dataWidth" :height="dataHeight"></canvas>
        </div>
        <div
          class="puzzle-lost-box"
          :style="'left:' + left_Num + 'px;width:' + dataWidth + 'px;height:' + dataHeight + 'px;'"
        >
          <canvas id="puzzle-shadow" ref="puzzleShadow" :width="dataWidth" :height="dataHeight"></canvas>
          <canvas id="puzzle-lost" ref="puzzleLost" :width="dataWidth" :height="dataHeight"></canvas>
        </div>
        <p
          class="ver-tips"
          ref="verTips"
          :style="'height: '+22*scale+'px;line-height:'+22*scale+'px;bottom: ' + (displayTips==true ? 0 : -22*scale ) +'px;font-size: '+12*scale+'px;'"
        >
          <template v-if="verification">
            <span :style="'color:#42ca6b;line-height:'+ 22*scale+'px;'">验证通过</span>
            <span :style="'margin-left:'+4*scale+'px;line-height:'+ 22*scale+'px;'">哇喔,怪物吃了拼图,快去登录吧~</span>
          </template>
          <template v-if="!verification">
            <span :style="'color:red;line-height:'+22*scale+'px;'">验证失败:</span>
            <span :style="'margin-left:'+4*scale+'px;line-height:'+ 22*scale+'px;'">拖动滑块将悬浮图像正确拼合</span>
          </template>
        </p>
      </div>
      <div
        class="re-btn"
        @click="refreshImg"
        :style="'height: '+28*scale+'px;padding: '+1*scale+'px '+16*scale+'px;font-size: '+18*scale+'px;'"
      >
        <a-icon type="redo" />
      </div>
    </div>
    <br />
    <div class="slider-container" :style="'width:' + dataWidth + 'px;'">
      <div class="slider-bar" :style="'border-radius:'+ 24*scale+'px;'">
        <p
          class="slider-bar-text select-none"
          onselectstart="return false"
          :style="'line-height:' + 28*scale + 'px;font-size:'+12*scale+'px;'"
        >按住滑块, 拖动完成上方拼图</p>
      </div>
      <div
        class="slider-btn"
        ref="sliderBtn"
        @mousedown="startMove"
        @touchstart="startMove"
        :style="'top: '+ -7*scale + 'px;'"
      >
        <a-icon
          type="pause-circle"
          theme="twoTone"
          twoToneColor="#52c41a"
          :style="'font-size: '+44*scale+'px;'"
        />
        <span class="slider-center-zzz">▍▍▍</span>
      </div>
    </div>
  </div>
</template>

<script lang="js">

export default {
  name: 'puzzle',
  data () {
    return {
      moveStart: '',
      displayTips: false,
      verification: false,
      randomX: null,
      randomY: null,
      imgRandom: '',
      left_Num: 0,
      dataWidth: 404,
      dataHeight: 90,
      puzzleSize: 100,
      deviation: 11,
      scale: 1,
      reTimer: null,
      isleavePage: false
    }
  },
  props: {
    width: {
      type: [String, Number],
      default: 340
    },
    height: {
      type: [String, Number],
      default: 120
    },
    cropImage: {
      type: [Boolean],
      default: false
    },
    puzzleImgList: {
      type: Array,
      default: () => [
        '../../../static/img/a.png',
      ]
    },
    PlSize: {
      type: [String, Number],
      default: 48
    },
    Deviation:{
      type: [String, Number],
      default: 4
    },
    padding: {
      type: [Number],
      default: 20
    },
    onSuccess: {
      type: Function,
      default: () => {
        alert('验证成功')
      }
    },
    onError: {
      type: Function,
      default: () => {
        alert('验证失败')
      }
    }
  },
  methods: {
    
    RandomNum(Min, Max) {
      let Range = Max - Min + 1
      let Rand = Math.random()
      return Min + Math.floor(Rand * Range)
    },
    refreshImg () {
      let imgRandomNum = this.RandomNum(0, this.puzzleImgList.length-1)
      this.imgRandom = this.puzzleImgList[imgRandomNum]
      this.initCanvas()
    },
    
    initCanvas () {
      this.clearCanvas()

      // let self = this
      let w = this.dataWidth
      let h = this.dataHeight
      // let PL_Size = 48
      // 2019-02-12开始PL_Size使用可传递参数
      let PL_Size = this.puzzleSize
      let padding = 20
      let MinN_X = padding + PL_Size
      let MaxN_X = w - padding - PL_Size - PL_Size / 6
      let MaxN_Y = padding
      let MinN_Y = h - padding - PL_Size - PL_Size / 6
      this.randomX = this.RandomNum(MinN_X, MaxN_X)
      this.randomY = this.RandomNum(MinN_Y, MaxN_Y)
      let X = this.randomX
      let Y = this.randomY
      this.left_Num = -X + 10
      let d = PL_Size / 3

      let c = this.$refs.puzzleBox
      let c_l = this.$refs.puzzleLost
      let c_s = this.$refs.puzzleShadow
      let ctx = c.getContext('2d')
      let ctx_l = c_l.getContext('2d')
      let ctx_s = c_s.getContext('2d')
      ctx.globalCompositeOperation = 'xor'
      ctx.shadowBlur = 10
      ctx.shadowColor = '#fff'
      ctx.shadowOffsetX = 3
      ctx.shadowOffsetY = 3
      ctx.fillStyle = 'rgba(0,0,0,0.7)'
      ctx.beginPath()
      ctx.lineWidth = '1'
      ctx.strokeStyle = 'rgba(0,0,0,0)'
      ctx.moveTo(X, Y)
      ctx.lineTo(X + d, Y)
      ctx.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
      ctx.lineTo(X + 3 * d, Y)
      ctx.lineTo(X + 3 * d, Y + d)
      ctx.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
      ctx.lineTo(X + 3 * d, Y + 3 * d)
      ctx.lineTo(X, Y + 3 * d)
      ctx.closePath()
      ctx.stroke()
      ctx.fill()

      let img = new Image()
      img.src = this.imgRandom
      img.onload = function () {
        ctx_l.beginPath()
        ctx_l.strokeStyle = 'rgba(0,0,0,0)'
        ctx_l.moveTo(X, Y)
        ctx_l.lineTo(X + d, Y)
        ctx_l.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
        ctx_l.lineTo(X + 3 * d, Y)
        ctx_l.lineTo(X + 3 * d, Y + d)
        ctx_l.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
        ctx_l.lineTo(X + 3 * d, Y + 3 * d)
        ctx_l.lineTo(X, Y + 3 * d)
        ctx_l.closePath()
        ctx_l.stroke()
        ctx_l.shadowBlur = 10
        ctx_l.shadowColor = 'black'
        ctx_l.clip()
        ctx_l.drawImage(img, 0, 0, w, h)

      }
      ctx_s.beginPath()
      ctx_s.lineWidth = '1'
      ctx_s.strokeStyle = 'rgba(0,0,0,0)'
      ctx_s.moveTo(X, Y)
      ctx_s.lineTo(X + d, Y)
      ctx_s.bezierCurveTo(X + d, Y - d, X + 2 * d, Y - d, X + 2 * d, Y)
      ctx_s.lineTo(X + 3 * d, Y)
      ctx_s.lineTo(X + 3 * d, Y + d)
      ctx_s.bezierCurveTo(X + 2 * d, Y + d, X + 2 * d, Y + 2 * d, X + 3 * d, Y + 2 * d)
      ctx_s.lineTo(X + 3 * d, Y + 3 * d)
      ctx_s.lineTo(X, Y + 3 * d)
      ctx_s.closePath()
      ctx_s.stroke()
      ctx_s.shadowBlur = 20
      ctx_s.shadowColor = 'black'
      ctx_s.fill()
    },
    
    clearCanvas () {
      let c = this.$refs.puzzleBox
      let c_l = this.$refs.puzzleLost
      let c_s = this.$refs.puzzleShadow
      c.setAttribute('height', c.getAttribute('height'))
      c_l.setAttribute('height', c.getAttribute('height'))
      c_s.setAttribute('height', c.getAttribute('height'))
      // c.height = c.height
      // c_l.height = c_l.height
      // c_s.height = c_s.height
    },
    
    startMove (e) {
      console.log(e)
      e = e || window.event
      this.$refs.sliderBtn.style.backgroundPosition = '0 -216px'
      this.moveStart = e.pageX || e.targetTouches[0].pageX
      this.addMouseMoveListener()
    },
    
    moving (e) {
      let self = this
      e = e || window.event
      let moveX = e.pageX || e.targetTouches[0].pageX
      let d = moveX - self.moveStart
      let w = self.dataWidth
      let PL_Size = this.puzzleSize
      let padding = 20
      if (self.moveStart === '') {
        return ''
      }
      if (d < 0 || d > (w - padding - PL_Size)) {
        return ''
      }
      self.$refs.sliderBtn.style.left = d + 'px'
      self.$refs.sliderBtn.style.transition = 'inherit'
      self.$refs.puzzleLost.style.left = d + 'px'
      self.$refs.puzzleLost.style.transition = 'inherit'
      self.$refs.puzzleShadow.style.left = d + 'px'
      self.$refs.puzzleShadow.style.transition = 'inherit'
    },
    
    moveEnd (e) {
      let self = this
      e = e || window.event
      let moveEnd_X = (e.pageX || e.changedTouches[0].pageX) - self.moveStart
      let ver_Num = self.randomX - 10
      let deviation = this.deviation
      let Min_left = ver_Num - deviation
      let Max_left = ver_Num + deviation
      if (self.moveStart !== '') {
        if (Max_left > moveEnd_X && moveEnd_X > Min_left) {
            
          self.displayTips = true
          self.verification = true
          setTimeout(function () {
            if(self.isleavePage){
              return
            }
            self.displayTips = false
            self.initCanvas()
          }, 2000)
          
          self.onSuccess()
        } else {
          self.displayTips = true
          self.verification = false
          setTimeout(function () {
            if(self.isleavePage){
              return
            }
            self.displayTips = false
          }, 2000)
          
          self.onError()
        }
      }
      if (typeof(self.$refs.sliderBtn) !== 'undefined' && typeof (self.$refs.puzzleLost) !== 'undefined' && typeof (self.$refs.puzzleShadow) !== 'undefined') {
        self.reTimer = setTimeout(function () {
          if(self.isleavePage){
            return
          }
          self.$refs.sliderBtn.style.left = 0
          self.$refs.sliderBtn.style.transition = 'left 0.5s'
          self.$refs.puzzleLost.style.left = 0
          self.$refs.puzzleLost.style.transition = 'left 0.5s'
          self.$refs.puzzleShadow.style.left = 0
          self.$refs.puzzleShadow.style.transition = 'left 0.5s'
        }, 1000)
        self.$refs.sliderBtn.style.backgroundPosition = '0 -84px'
      }
      self.moveStart = ''
    },
    
    addMouseMoveListener () {
      let self = this
      document.addEventListener('mousemove', self.moving)
      document.addEventListener('touchmove', self.moving)
      document.addEventListener('mouseup', self.moveEnd)
      document.addEventListener('touchend', self.moveEnd)
    }

  },
  mounted () {
    let self = this
    this.dataWidth = this.$refs.puzzle.clientWidth*0.884
    this.dataHeight = this.dataWidth*(this.height/this.width)
    this.scale = this.$refs.puzzle.clientWidth/260
    this.puzzleSize = this.dataWidth*(this.PlSize/this.width)
    setTimeout(self.refreshImg,0)
  },
  created () {
    let imgRandomNum = this.RandomNum(0, this.puzzleImgList.length - 1)
    this.imgRandom = this.puzzleImgList[imgRandomNum]
    this.puzzleSize = Number(this.PlSize)
    this.deviation = Number(this.Deviation)
    
    // this.dataWidth = this.width
    // this.dataHeight = this.height
    // this.scale = this.width/260
  },
  watch: {
  },
  beforeDestroy() {
    this.isleavePage = true
  }
}
</script>

<style scoped>
.slider-btn {
  position: absolute;
  width: 90px;
  height: 67px;
  left: 0;
  top: -7px;
  z-index: 12;
  cursor: pointer;
  background-position: 0 -84px;
  transition: inherit;
  background-color: #f4f7fe;
  border-radius: 45%;
  display: flex;
}
.slider-center-zzz {
  text-align: center;
  line-height: 67px;
  position: absolute;
  color: #53C300;
  height: 40px;
  width: 100%;
}
.ver-tips {
  position: absolute;
  left: 0;
  bottom: -22px;
  background: rgba(255, 255, 255, 0.9);
  height: 22px;
  line-height: 22px;
  font-size: 12px;
  width: 100%;
  margin: 0;
  text-align: left;
  padding: 0 8px;
  transition: all 0.4s;
}
.slider-tips {
  bottom: 0;
}
.ver-tips i {
  display: inline-block;
  width: 22px;
  height: 22px;
  vertical-align: top;
  background-position: -4px -1229px;
}
.ver-tips span {
  display: inline-block;
  vertical-align: top;
  line-height: 22px;
  color: #455;
}
.active-tips {
  display: block;
}
.hidden {
  display: none;
}
.re-btn {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 28px;
  padding: 0 16px;
}
.re-btn a {
  display: inline-block;
  width: 14px;
  height: 14px;
  margin: 0px 0;
  background-position: 0 -1179px;
  cursor: pointer;
}
.re-btn a:hover {
  background-position: 0 -1193px;
}

.puzzle-container {
  position: relative;
  
  
  background: #e0e1e3;
  

  
  
}
.slider-container {
  position: relative;
  margin: auto;
}
.slider-bar {
  border: 1px solid #c3c3c3;
  border-radius: 24px;
  background: #e0e1e3;
  box-shadow: 0 1px 1px rgba(12, 10, 10, 0.2) inset;
}
.slider-bar-text {
  font-size: 12px;
  color: #486c80;
  line-height: 28px;
  margin: 0;
  text-align: right;
  padding-right: 22px;
}

#puzzle-box {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 22;
}
#puzzle-shadow {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 22;
}
#puzzle-lost {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 33;
}
.puzzle-lost-box {
  position: absolute;
  width: 260px;
  height: 116px;
  left: 0;
  top: 0;
  z-index: 111;
}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: vue+elementui实现拖住滑块拼图验证

本文链接: https://lsjlt.com/news/144154.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • vue+elementui实现拖住滑块拼图验证
    vue拖住滑块拼图验证,以下是cavas直接写的滑块拼图验证码,直接复制引用即可 ​<template>   <div id="puzzle" ref="puzz...
    99+
    2024-04-02
  • JavaScript实现拖动滑块拼图验证功能(html5、canvas)
    引言: 滑块拖动验证现在很多地方都用到,周末就琢磨着写了一个,放上来,看看有没有人用得上! 效果: 实现思路:  用一张画布绘制源图,再绘制一个填充的方形,这样就可以达到...
    99+
    2024-04-02
  • Java实现滑块拼图验证码
    本文实例为大家分享了Java实现滑块拼图验证码的具体代码,供大家参考,具体内容如下 1、后端随机生成抠图和带有抠图阴影的背景图片,后台保存随机抠图位置坐标 2、前端实现滑动交互,将抠...
    99+
    2024-04-02
  • vue实现登录滑动拼图验证
    本文实例为大家分享了vue实现登录滑动拼图验证的具体代码,供大家参考,具体内容如下 一、安装插件 npm install --save vue-monoplasty-slide-ve...
    99+
    2024-04-02
  • JS实现拖动滑块验证
    使用这种验证方法的目的:证明当前的用户不是机器人~防止恶意操作。 实现思路: 1、获取silde滑块(获取元素) 2、为元素注册事件———鼠标点击...
    99+
    2024-04-02
  • Python实现滑块拼图验证码详解
    目录初级版滑块拼图验证码补充知识点高级版滑动拼图验证码滑动拼图验证码可以算是滑块验证码的进阶版本,其验证机制相对复杂。本节将介绍两种滑动拼图验证码:初级版和高级版本。 初级版滑块拼...
    99+
    2024-04-02
  • JavaScript实现拼图式滑块验证功能
    目录演示前戏源码介绍主页样式设计滑块验证部分img_ver内部演示 前戏 滑块验证码是在网站、APP等应用中常见的一种验证方式,通过按照一定规则滑动滑块到指定位置完成验证,才可以进...
    99+
    2024-04-02
  • Android 如何实现滑块拼图验证码功能
    本篇内容主要讲解“Android 如何实现滑块拼图验证码功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android 如何实现滑块拼图验证码功能”吧!本篇主要从两方面进行介绍:使用依赖库实现...
    99+
    2023-06-14
  • JavaScript如何实现拼图式滑块验证功能
    这篇文章主要介绍“JavaScript如何实现拼图式滑块验证功能”,在日常操作中,相信很多人在JavaScript如何实现拼图式滑块验证功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”JavaScript如...
    99+
    2023-07-02
  • vue实现简单滑块验证
    本文实例为大家分享了Vue滑块验证的实现,代码如下 <template> <div class="drag" ref="dragDiv">...
    99+
    2024-04-02
  • vue实现移动滑块验证
    本文实例为大家分享了vue实现移动滑块验证的具体代码,供大家参考,具体内容如下 记录一下今天的学习内容 <div class="solidBox" :class="{'so...
    99+
    2024-04-02
  • Android 简单的实现滑块拼图验证码功能
    目录实现过程:接下来我们对这个库进行介绍:实现滑块拼图验证码功能之前已经写过一篇了,上一篇使用的是自定义控件的方式实现这个功能,主要还是想让童鞋们知其然更知其所以然,还没看的童鞋可以...
    99+
    2024-04-02
  • C#滑动验证码拼图验证功能实现(SlideCaptcha)
    目录使用背景:实现分析:后端代码:准备:使用:前端代码:结语:使用背景: 关于滑动验证码的使用场所还是非常多的,如:调取短信接口之前,和 注册请求之前 或者 频繁会调用的接口都需要加...
    99+
    2024-04-02
  • vue实现登录时滑块验证
    本文实例为大家分享了vue实现登录时滑块验证的具体代码,供大家参考,具体内容如下 1.效果图 2. 新建 SliderCheck.vue组件 <template> &...
    99+
    2024-04-02
  • vue实现图片滑动验证
    本文实例为大家分享了vue实现图片滑动验证的具体代码,供大家参考,具体内容如下 效果图: 1、引用自定义组件 import img0 from '../assets/img.jpg...
    99+
    2024-04-02
  • PHP如何实现滑块验证图片
    这篇文章主要讲解了“PHP如何实现滑块验证图片”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PHP如何实现滑块验证图片”吧!安装composer require kkok...
    99+
    2023-07-04
  • 如何在Android中实现一个滑块拼图验证码功能
    本篇文章给大家分享的是有关如何在Android中实现一个滑块拼图验证码功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一、实现步骤:定义自定义属性; 2、确认目标位置,这里使...
    99+
    2023-06-06
  • 微信小程序实现滑动验证拼图
    本文实例为大家分享了微信小程序实现滑动验证拼图的具体代码,供大家参考,具体内容如下 效果图 .wxml <button bindtap="visidlisd">滑动验证...
    99+
    2024-04-02
  • 如何使用php+js实现极验,拖动滑块验证码验证表单
    这篇文章给大家分享的是有关如何使用php+js实现极验,拖动滑块验证码验证表单的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。   代码实现   html文件   <!...
    99+
    2024-04-02
  • js canvas实现滑块验证
    本文实例为大家分享了js canvas实现滑块验证的具体代码,供大家参考,具体内容如下 滑块验证 话不多说先上代码想用的小伙伴可以直接使用,想了解的我后面会说下我的思路 <...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作