最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何使用HTML+JavaScript實現(xiàn)滑動驗證碼

 更新時間:2026年01月13日 11:29:22   作者:bjzhang75  
這篇文章主要介紹了如何使用HTML+JavaScript實現(xiàn)滑動驗證碼的相關資料,通過示例代碼詳細描述了系統(tǒng)的功能實現(xiàn)、代碼結(jié)構(gòu)及擴展建議,需要的朋友可以參考下

一、滑動驗證碼

在現(xiàn)代網(wǎng)絡安全體系中,人機驗證機制扮演著至關重要的角色。傳統(tǒng)的文本驗證碼由于識別困難、用戶體驗差等問題逐漸被更先進的驗證方式取代?;瑒域炞C碼作為一種新型的人機驗證手段,憑借其直觀的操作體驗和良好的安全性,廣泛應用于各類網(wǎng)站和應用程序中。本文將詳細介紹如何使用 HTML、CSS 和 JavaScript 構(gòu)建一個完整的滑動驗證碼系統(tǒng)。

二、效果演示

滑動驗證碼的核心交互流程包括圖像加載、拼圖生成、用戶拖拽和驗證判斷四個階段,用戶通過拖拽右側(cè)滑塊向右移動,使拼圖塊與背景圖像中的缺口對齊,驗證成功時顯示綠色成功提示,失敗則顯示紅色錯誤信息并自動重置。

三、系統(tǒng)分析

1、頁面結(jié)構(gòu)

整個滑動驗證碼系統(tǒng)采用簡潔清晰的 HTML 結(jié)構(gòu)設計,主要包括圖像顯示區(qū)(verify-img)、滑動控制區(qū)(erify-bar-box)和結(jié)果顯示區(qū)(verify-result)三個核心部分。

<div class="verify-container">
  <div class="verify-box">
    <div class="verify-img">
      <img class="back-img" src="" style="width:100%;height:100%;"/>
      <div class="loading-indicator" id="backImgLoading" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);color:#666;font-size:14px;display:none;">加載中...</div>
    </div>
    <div class="verify-bar-box">
      <span class="verify-msg">向右滑動完成驗證</span>
      <div class="verify-left-bar"></div>
      <div class="verify-move-block">
        <span>></span>
        <div class="verify-sub-block">
          <img class="block-img" src="" style="width:100%;height:100%;"/>
        </div>
      </div>
    </div>
  </div>
  <div class="verify-result" id="verifyResult"></div>
</div>

2、核心功能實現(xiàn)

2.1 初始化流程

主要實現(xiàn)了隨機位置生成、圖像加載和拼圖繪制三個步驟。

async function init() {
  // 顯示加載指示器
  showLoading()
  targetX = Math.floor(Math.random() * (imgWidth - bolckSize - 60)) + 30;
  targetY = Math.floor(Math.random() * (imgHeight - bolckSize - 20)) + 10;
  var img = await loadImg(imgUrl+'?'+Math.random());
  // 創(chuàng)建背景畫布并繪制帶缺口的圖像
  var backCanvas = document.createElement('canvas')
  backCanvas.width = imgWidth;
  backCanvas.height = imgHeight;
  var backCtx = backCanvas.getContext('2d');
  backCtx.drawImage(img, 0, 0, 380, 190, 0, 0, imgWidth, imgHeight);
  backCtx.fillStyle = '#FFFFFF';
  backCtx.fillRect(targetX, targetY, 50, 50);
  backImg.src = backCanvas.toDataURL('image/png');
  // 創(chuàng)建拼圖塊
  var canvas = document.createElement('canvas')
  canvas.width = 50;
  canvas.height = 50;
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, targetX, targetY, bolckSize, bolckSize, 0, 0, bolckSize, bolckSize)
  blockImg.src = canvas.toDataURL('image/png');
  subBlock.style.top = (-201 + targetY) + 'px';
  // 隱藏加載指示器
  hideLoading()
}

2.2 拖拽交互處理

通過鼠標事件監(jiān)聽實現(xiàn)流暢的拖拽體驗,當用戶在滑塊上按下鼠標并移動時,滑塊滑塊會隨鼠標移動;當用戶釋放鼠標時,進行位置校驗,如果失敗滑塊會變?yōu)榧t色并平滑的回到起點位置。

// 鼠標按下事件 - 開始拖拽
moveBlock.addEventListener('mousedown', function(e) {
  isDragging = true;
  startX = e.clientX;
  moveBlock.style.backgroundColor = '#337AB7';
  moveBlock.style.color = '#FFFFFF';
  verifyLeftBar.style.border = '1px solid #337AB7';
});
// 鼠標移動事件 - 拖拽過程
document.addEventListener('mousemove', function(e) {
  if (!isDragging) return;
  var newLeft = e.clientX - startX - 2;
  // 限制滑塊移動范圍
  if (newLeft < 0) newLeft = 0;
  if (newLeft > maxWidth) newLeft = maxWidth;

  moveBlock.style.left = newLeft + 'px';
  verifyLeftBar.style.width = newLeft + 'px';
  verifyLeftBar.style.border = '1px solid #337AB7';
});
// 鼠標釋放事件 - 結(jié)束拖拽
document.addEventListener('mouseup', function() {
  if (!isDragging) return;
  isDragging = false;
  var currentPosition = moveBlock.offsetLeft;
  if (Math.abs(currentPosition - targetX) <= tolerance) {
    moveBlock.style.backgroundColor = '#5CB85C';
    moveBlock.style.color = '#FFFFFF';
    verifyLeftBar.style.border = '1px solid #5CB85C';

    // 顯示成功提示
    verifyResult.textContent = '驗證成功!';
    verifyResult.className = 'verify-result success';
    return;
  }
  moveBlock.style.backgroundColor = '#D9534F';
  moveBlock.style.color = '#FFFFFF';
  verifyLeftBar.style.border = '1px solid #D9534F';
  verifyLeftBar.style.backgroundColor = '#fff0f0';
  // 顯示失敗提示
  verifyResult.textContent = '驗證失敗,請重試';
  verifyResult.className = 'verify-result fail';
  // 滑塊回彈動畫
  moveBlock.style.transition = 'left 0.8s';
  moveBlock.style.left = '0px';

  verifyLeftBar.style.transition = 'width 0.8s';
  verifyLeftBar.style.width = '0px';

  // 動畫結(jié)束后清除過渡效果
  setTimeout(() => {
    init()
    moveBlock.style.transition = '';
    verifyLeftBar.style.transition = '';
    moveBlock.style.backgroundColor = '#FFFFFF';
    moveBlock.style.color = '#999';
    verifyLeftBar.style.backgroundColor = '#F0FFF0';
    // 清除驗證結(jié)果提示
    verifyResult.className = 'verify-result';
  }, 800);
});

四、擴展建議

  • 添加服務器端驗證,防止客戶端偽造結(jié)果
  • 增加行為特征檢測,識別自動化工具攻擊
  • 添加個性化拼圖形狀設計,豐富視覺表現(xiàn)
  • 增加重試次數(shù)限制機制,防止無限重試

五、完整代碼

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>滑動驗證碼</title>
  <style>
      * {
          margin: 0;
          padding: 0;
          box-sizing: border-box;
      }
      body {
          background: #f5f5f5;
      }
      .container {
          display: flex;
          padding: 20px;
          justify-content: center;
      }
      .verify-container {
          background: #fff;
          width:400px;
          padding: 10px;
          border: 1px solid #ddd;
          user-select: none;
      }
      .verify-img {
          width: 380px;
          height: 190px;
          margin-bottom: 10px;
          position: relative;
      }
      .verify-bar-box {
          width: 380px;
          height: 50px;
          line-height: 50px;
          position: relative;
          background: #FFFFFF;
          text-align: center;
          box-sizing: content-box;
          border: 1px solid #ddd;
          border-radius: 4px;
          color: #999;
      }
      .verify-left-bar {
          background: #f0fff0;
          position: absolute;
          top: 0;
          left: 0;
          height: 50px;
      }
      .verify-move-block {
          position: absolute;
          top: 0;
          left: 0;
          background: #fff;
          cursor: pointer;
          box-sizing: content-box;
          box-shadow: 0 0 2px #888888;
          border-radius: 1px;
          width: 50px;
          height: 50px;
      }
      .verify-sub-block {
          position: absolute;
          border: 1px solid #ddd;
          height: 50px;
          left: -2px;
          top: -201px;
      }

      .verify-result {
          margin-top: 10px;
          padding: 8px 12px;
          text-align: center;
          border-radius: 4px;
          font-weight: bold;
          display: none;
      }

      .verify-result.success {
          background-color: #dff0d8;
          color: #3c763d;
          border: 1px solid #d6e9c6;
          display: block;
      }

      .verify-result.fail {
          background-color: #f2dede;
          color: #a94442;
          border: 1px solid #ebccd1;
          display: block;
      }
      .loading-indicator {
          padding: 5px 10px;
          border-radius: 4px;
      }
  </style>
</head>
<body>
<div class="container">
  <div class="verify-container">
    <div class="verify-box">
      <div class="verify-img">
        <img class="back-img" src="" style="width:100%;height:100%;"/>
        <div class="loading-indicator" id="backImgLoading" style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);color:#666;font-size:14px;display:none;">加載中...</div>
      </div>
      <div class="verify-bar-box">
        <span class="verify-msg">向右滑動完成驗證</span>
        <div class="verify-left-bar"></div>
        <div class="verify-move-block">
          <span>></span>
          <div class="verify-sub-block">
            <img class="block-img" src="" style="width:100%;height:100%;"/>
          </div>
        </div>
      </div>
    </div>
    <div class="verify-result" id="verifyResult"></div>
  </div>
</div>
<script>
  var verifyBarBox = document.querySelector('.verify-bar-box');
  var moveBlock = document.querySelector('.verify-move-block');
  var verifyLeftBar = document.querySelector('.verify-left-bar');
  var backImg = document.querySelector('.back-img');
  var subBlock = document.querySelector('.verify-sub-block');
  var blockImg = document.querySelector('.block-img');
  var verifyResult = document.getElementById('verifyResult');
  var backImgLoading = document.getElementById('backImgLoading');

  var startX = 0;
  var isDragging = false;
  var maxWidth = verifyBarBox.offsetWidth - moveBlock.offsetWidth;

  var imgUrl = 'https://picsum.photos/380/190';
  var imgWidth = 380;
  var imgHeight = 190;
  var bolckSize = 50;
  var targetX = 0;
  var targetY = 0;
  var tolerance = 5;
  init();
  async function init() {
    // 顯示加載指示器
    showLoading()
    targetX = Math.floor(Math.random() * (imgWidth - bolckSize - 60)) + 30;
    targetY = Math.floor(Math.random() * (imgHeight - bolckSize - 20)) + 10;
    var img = await loadImg(imgUrl+'?'+Math.random());
    // 創(chuàng)建背景畫布并繪制帶缺口的圖像
    var backCanvas = document.createElement('canvas')
    backCanvas.width = imgWidth;
    backCanvas.height = imgHeight;
    var backCtx = backCanvas.getContext('2d');
    backCtx.drawImage(img, 0, 0, 380, 190, 0, 0, imgWidth, imgHeight);
    backCtx.fillStyle = '#FFFFFF';
    backCtx.fillRect(targetX, targetY, 50, 50);
    backImg.src = backCanvas.toDataURL('image/png');
    // 創(chuàng)建拼圖塊
    var canvas = document.createElement('canvas')
    canvas.width = 50;
    canvas.height = 50;
    var ctx = canvas.getContext('2d');
    ctx.drawImage(img, targetX, targetY, bolckSize, bolckSize, 0, 0, bolckSize, bolckSize)

    blockImg.src = canvas.toDataURL('image/png');
    subBlock.style.top = (-201 + targetY) + 'px';
    // 隱藏加載指示器
    hideLoading()
  }
  function showLoading() {
    backImgLoading.style.display = 'block';
    subBlock.style.display = 'none'
    backImg.style.display = 'none'
  }
  function hideLoading() {
    backImgLoading.style.display = 'none';
    subBlock.style.display = 'block'
    backImg.style.display = 'block'
  }

  // 繪制拼圖塊
  function loadImg(url){
    return new Promise((res,rej)=>{
      const im = new Image();
      im.crossOrigin='anonymous';
      im.onload = ()=>res(im);
      im.onerror= rej;
      im.src = url;
    });
  }

  // 鼠標按下事件 - 開始拖拽
  moveBlock.addEventListener('mousedown', function(e) {
    isDragging = true;
    startX = e.clientX;
    moveBlock.style.backgroundColor = '#337AB7';
    moveBlock.style.color = '#FFFFFF';
    verifyLeftBar.style.border = '1px solid #337AB7';
  });
  // 鼠標移動事件 - 拖拽過程
  document.addEventListener('mousemove', function(e) {
    if (!isDragging) return;
    var newLeft = e.clientX - startX - 2;
    // 限制滑塊移動范圍
    if (newLeft < 0) newLeft = 0;
    if (newLeft > maxWidth) newLeft = maxWidth;

    moveBlock.style.left = newLeft + 'px';
    verifyLeftBar.style.width = newLeft + 'px';
    verifyLeftBar.style.border = '1px solid #337AB7';
  });
  // 鼠標釋放事件 - 結(jié)束拖拽
  document.addEventListener('mouseup', function() {
    if (!isDragging) return;
    isDragging = false;
    var currentPosition = moveBlock.offsetLeft;
    if (Math.abs(currentPosition - targetX) <= tolerance) {
      moveBlock.style.backgroundColor = '#5CB85C';
      moveBlock.style.color = '#FFFFFF';
      verifyLeftBar.style.border = '1px solid #5CB85C';

      // 顯示成功提示
      verifyResult.textContent = '驗證成功!';
      verifyResult.className = 'verify-result success';
      return;
    }
    moveBlock.style.backgroundColor = '#D9534F';
    moveBlock.style.color = '#FFFFFF';
    verifyLeftBar.style.border = '1px solid #D9534F';
    verifyLeftBar.style.backgroundColor = '#fff0f0';
    // 顯示失敗提示
    verifyResult.textContent = '驗證失敗,請重試';
    verifyResult.className = 'verify-result fail';
    // 滑塊回彈動畫
    moveBlock.style.transition = 'left 0.8s';
    moveBlock.style.left = '0px';

    verifyLeftBar.style.transition = 'width 0.8s';
    verifyLeftBar.style.width = '0px';

    // 動畫結(jié)束后清除過渡效果
    setTimeout(() => {
      init()
      moveBlock.style.transition = '';
      verifyLeftBar.style.transition = '';
      moveBlock.style.backgroundColor = '#FFFFFF';
      moveBlock.style.color = '#999';
      verifyLeftBar.style.backgroundColor = '#F0FFF0';
      // 清除驗證結(jié)果提示
      verifyResult.className = 'verify-result';
    }, 800);
  });
</script>
</body>
</html>

總結(jié) 

到此這篇關于如何使用HTML+JavaScript實現(xiàn)滑動驗證碼的文章就介紹到這了,更多相關HTML+JS滑動驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

和田市| 锡林浩特市| 陵水| 滁州市| 晋城| 商水县| 当阳市| 禄丰县| 建瓯市| 海安县| 盐亭县| 开化县| 那曲县| 云安县| 辛集市| 屏东县| 民县| 灯塔市| 万山特区| 惠来县| 湄潭县| 镇江市| 余江县| 泸定县| 伊吾县| 逊克县| 阿城市| 东至县| 阳江市| 美姑县| 益阳市| 阜康市| 紫云| 商洛市| 潞城市| 杭锦旗| 黑龙江省| 闵行区| 神农架林区| 永宁县| 贡嘎县|