網頁如何實現拼圖滑塊的驗證碼_純js的實現


實現思路:

監聽滑塊的移動事件,獲取它離左側的距離,賦值給分離出來的驗證模塊就可以了,至於陷下去的模塊,得到圖片的大小,根據圖片的長寬的一半隨機賦值吧(因為這個模塊一般都在右側靠中間的位置)

這種滑動解鎖應該是有兩張圖片,一張正常的,一張上面有解鎖區域的(后端給),然后前端只用把用戶釋放鼠標后,滑動模塊在圖片上的xy軸傳給后端,后端做成功與否的判斷。

 

純js的實現:

只是前端驗證,不具備高安全性。

html代碼:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>滑動驗證</title>
    <link rel="stylesheet" href="css/index.css">
</head>

<body>
    <div class="box">
        <div class="imgBox">
            <div class="verify"></div>
            <div class="verified"></div>
        </div>
        <div class="handle">
            <span class="swiper"></span>
            <span class="text">拖動滑塊</span>
        </div>
        <div class="refreshBox">
            <span class="refresh">點擊切換圖片</span>
        </div>
    </div>
    <script src='js/jquery-3.1.1.min.js'></script>
    <script src='js/index.js'></script>
</body>

  

css代碼:

.box {
  width: 300px;
  padding: 20px;
  background-color: #fff;
  box-shadow: 2px 2px 8px 0 rgba(0, 0, 0, 0.4);
}

.imgBox {
  position: relative;
  width: 300px;
  overflow: hidden;
  box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.4);
}

.imgBox img {
  width: 100%;
}

.imgBox div {
  display: none;
}

.handle {
  display: flex;
  align-items: center;
  position: relative;
  height: 30px;
  border-radius: 20px;
  margin: 20px 0;
  padding: 4px 0 4px 70px;
  box-shadow: 0 0 20px 0 rgba(0, 0, 0, 0.2) inset;
  background: #f5f5f5;
  user-select: none;
}

.text {
  opacity: 1;
  transition: opacity 0.5s ease-in-out;
  color: #aaa;
}

.swiper {
  position: absolute;
  top: -10px;
  left: 0px;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background-color: pink;
  box-shadow: 2px 2px 6px 0 rgba(0, 0, 0, 0.2);
}

.verify {
  position: absolute;
  left: 10px;
  width: 38px;
  height: 38px;
  border-radius: 5px;
  background-repeat: no-repeat;
  background-attachment: scroll;
  background-size: 300px;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.4), 0 0 10px 0 rgba(90, 90, 90, 0.4);
  z-index: 10;
}

.verified {
  position: absolute;
  width: 38px;
  height: 38px;
  border-radius: 5px;
  background-color: rgba(0, 0, 0, 0.1);
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.4) inset;
}

.refreshBox {
  border-top: 1px solid #ccc;
  padding: 15px 0 0 5px;
}

.refresh {
  color: #fff;
  cursor: pointer;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  padding: 8px 20px;
  border-radius: 20px;
  background-color: #555;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.4);
  transition: all 0.5s ease-in-out;
}
.refresh.click {
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.4) inset;
}

虎課網https://www.wode007.com/sites/73267.html 設計塢https://www.wode007.com/sites/73738.html

js代碼:

var box = $('.box'),
  imgBox = $('.imgBox'),
  handle = $('.handle'),
  swiper = $('.swiper'),
  text = $('.text'),
  verify = $('.verify'),
  verified = $('.verified'),
  refresh = $('.refresh')

// 圖片集合
var imgs = []
for (let i = 1; i < 17; i++) {
  imgs.push('' + i + i)
}

$(function() {
  // 隨機添加背景圖
  refreshImg()

  refresh.click(function() {
    e = event || window.event
    e.stopPropagation()

    refreshImg()
    start()
  })

  refresh.mousedown(function() {
    $(this).addClass('click')
  })
  refresh.mouseup(function() {
    $(this).removeClass('click')
  })

  window.onload = start()
})

function start() {
  var verImg = document.getElementsByClassName('verImg')[0]

  if (verImg) {
    verImg.onload = function() {
      // 獲取圖片高度
      var imgH = this.clientHeight
      // 隨機生成坐標(圖片框固定寬度為300px,高度不定)
      var verX = 150 * (1 + Math.random()) - 38,
        verY = imgH / 4 + Math.random() * imgH / 2

      // 用戶移動滑塊函數
      fnDown(verX, verY)
    }
  }
}

function fnDown(verX, verY) {
  swiper.mousedown(function() {
    e = event || window.event
    e.stopPropagation()

    // 30為模塊寬度
    verify.css({
      display: 'block',
      top: `${verY}px`,
      'background-position': `-${verX}px -${verY}px`
    })
    verified.css({ display: 'block', left: `${verX}px`, top: `${verY}px` })
    // 獲取鼠標到按鈕的距離
    var disX = e.clientX - $(this).offset().left,
      disY = e.clientY - $(this).offset().top
    text.css('opacity', '0')

    // 防止重復綁定觸發多次
    box.unbind('mousemove')
    box.unbind('mouseup')

    // 移動
    box.bind('mousemove', function() {
      e = event || window.event
      fnMove(e, disX, disY)
    })

    // 釋放
    box.bind('mouseup', function() {
      var stopL = verify.offset().left - 28
      // 誤差在2px以內則算做成功
      if (Math.abs(stopL - verX) > 2) {
        alert('驗證失敗')
      } else {
        alert('驗證成功')
      }
      // 解除綁定,並將滑動模塊歸位
      box.unbind('mousemove')
      swiper.css('left', '0px')
      verify.css('left', '10px')
      text.css('opacity', '1')
      box.unbind('mouseup')
    })
  })
}

function fnMove(e, posX, posY) {
  // 這里的e是以鼠標為參考
  var l = e.clientX - posX - $(handle).offset().left,
    winW = $(handle).width() + 29
  // 限制拖動范圍只能在handle中
  if (l < 0) {
    l = 0
  } else if (l > winW) {
    l = winW
  }

  swiper.css('left', `${l}px`)
  verify.css('left', `${l + 10}px`)
}

function refreshImg() {
  // 隨機生成下標
  var index = Math.round(Math.random() * 15)
  var imgH = 0

  verify.hide()
  verified.hide()

  var verImg = $('.verImg')
  if (verImg.length) {
    verImg.attr('src', `imgs/${imgs[index]}.jpg`)
  } else {
    imgBox.prepend(`<img class='verImg' src="imgs/${imgs[index]}.jpg" />`)
  }
  verify.css('background-image', `url('imgs/${imgs[index]}.jpg')`)
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM