如何用JavaScript实现基于HTML的滑动拼图验证码功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计753个文字,预计阅读时间需要4分钟。
原文:本文实例为大家分享了js实现滑动图片拼图验证的具体代码,供大家参考,具体内容如下+:+!DOCTYPE + lang=en+head+meta charset=UTF-8+meta name=viewport content=width=device-width, initial-scale=1.0+/head+body+div id=slider-container+/body+/
改写后:本文以实例展示了如何使用js实现滑动图片拼图验证的功能,代码如下:+:+!DOCTYPE + lang=en+head+meta charset=UTF-8+meta name=viewport content=width=device-width, initial-scale=1.0+/head+body+div id=slider-container+/body+/
本文实例为大家分享了js实现html滑动图片拼图验证的具体代码,供大家参考,具体内容如下
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta picsum.photos/300/150/?image=' + getRandomNumberByRange(0, 100) } //清除tupian function clean(){ x = Math.round(Math.random() * 200) + 10, y = Math.round(Math.random() * 100) + 10, console.log(x,y) canvas_ctx.clearRect(0, 0, 310, 155); block_ctx.clearRect(0, 0, 310, 155) block.width = 310 draw(canvas_ctx, 'fill') draw(block_ctx, 'clip') } //绘制方块 function draw(ctx, operation) { ctx.beginPath() ctx.moveTo(x, y) ctx.arc(x + l / 2, y - r + 2, r, 0.72 * PI, 2.26 * PI) ctx.lineTo(x + l, y) ctx.arc(x + l + r - 2, y + l / 2, r, 1.21 * PI, 2.78 * PI) ctx.lineTo(x + l, y + l) ctx.lineTo(x, y + l) ctx.arc(x + r - 2, y + l / 2, r + 0.4, 2.76 * PI, 1.24 * PI, true) ctx.lineTo(x, y) ctx.lineWidth = 2 ctx.fillStyle = 'rgba(255, 255, 255, 0.7)' ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)' ctx.stroke() ctx[operation]() ctx.globalCompositeOperation = 'overlay' } initImg() draw(canvas_ctx, 'fill') draw(block_ctx, 'clip') //添加移动事件 var block_slider = document.querySelector("#block"); var slider = document.querySelector(".verSliderBlock"); var slider_mark = document.querySelector("#bar-mask"); //用于判断当前是否是在按住滑块的情况下 var yd = false var moveX = 0 var downX = 0 //鼠标按下 slider.onmousedown = function (e) { downX = e.clientX; yd = true } //鼠标移动事件 function hadleMousemove(e) { if (yd) { moveX = e.clientX - downX; document.querySelector('#slide').innerHTML = '' if (moveX >= 310) { moveX = 310 - 40 } if (moveX > -2) { slider.style.backgroundColor = "#1991FA"; slider_mark.style.borderWidth = "1px" slider_mark.style.borderColor = "#1991fa" slider_mark.style.width = moveX + 40 + "px"; block_slider.style.left = (310 - 40 - 20) / (310 - 40) * moveX + "px"; slider.style.left = moveX + "px"; } } } //鼠标抬起事件 function hadleMouseup(e) { if (yd) { slider.onmousemove = null; console.log(moveX) block_slider.style.left = (310 - 40 - 20) / (310 - 40) * moveX + "px"; if (Math.abs((310 - 40 - 20) / (310 - 40) * moveX - x) < 10) { slider.style.background = "url('./success.png')"; slider.style.backgroundSize = '100%' // alert('验证成功') setTimeout(() => { rest(); }, 1000) } else { slider_mark.style.backgroundColor = "#fce1e1" slider_mark.style.borderWidth = "1px" slider_mark.style.borderColor = "#f57a7a" slider.style.backgroundColor = "#f57a7a"; slider.style.background = "url('./fail.png')"; slider.style.backgroundSize = '100%' setTimeout(() => { rest(); }, 1000) } yd = false } } //鼠标在按住滑块下移动 slider.onmousemove = function (e) { hadleMousemove(e) } //鼠标在滑块下抬起 slider.onmouseup = function (e) { hadleMouseup(e) } //设置全局的移动事件,当鼠标按下滑块后,移动过程中鼠标可能会移出滑块,这是滑块也会监听鼠标的移动进行相应的移动 document.addEventListener('mousemove', function (e) { hadleMousemove(e) }) document.addEventListener('mouseup', function (e) { hadleMouseup(e) }) function rest() { clean() document.querySelector('#slide').innerHTML = '向右滑动验证' slider.style.backgroundColor = "#fff"; slider.style.left = "0px" slider.style.background = "url('./right_arrow.png')"; slider.style.backgroundSize = '100%' block_slider.style.left = "0px" slider_mark.style.width = "0px" slider_mark.style.backgroundColor = "#d1e9fe" slider_mark.style.borderWidth = "0px" slider_mark.style.borderColor = "#d1e9fe" initImg() } //刷新 refresh.onclick = function(){ rest() } }(window))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计753个文字,预计阅读时间需要4分钟。
原文:本文实例为大家分享了js实现滑动图片拼图验证的具体代码,供大家参考,具体内容如下+:+!DOCTYPE + lang=en+head+meta charset=UTF-8+meta name=viewport content=width=device-width, initial-scale=1.0+/head+body+div id=slider-container+/body+/
改写后:本文以实例展示了如何使用js实现滑动图片拼图验证的功能,代码如下:+:+!DOCTYPE + lang=en+head+meta charset=UTF-8+meta name=viewport content=width=device-width, initial-scale=1.0+/head+body+div id=slider-container+/body+/
本文实例为大家分享了js实现html滑动图片拼图验证的具体代码,供大家参考,具体内容如下
html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta picsum.photos/300/150/?image=' + getRandomNumberByRange(0, 100) } //清除tupian function clean(){ x = Math.round(Math.random() * 200) + 10, y = Math.round(Math.random() * 100) + 10, console.log(x,y) canvas_ctx.clearRect(0, 0, 310, 155); block_ctx.clearRect(0, 0, 310, 155) block.width = 310 draw(canvas_ctx, 'fill') draw(block_ctx, 'clip') } //绘制方块 function draw(ctx, operation) { ctx.beginPath() ctx.moveTo(x, y) ctx.arc(x + l / 2, y - r + 2, r, 0.72 * PI, 2.26 * PI) ctx.lineTo(x + l, y) ctx.arc(x + l + r - 2, y + l / 2, r, 1.21 * PI, 2.78 * PI) ctx.lineTo(x + l, y + l) ctx.lineTo(x, y + l) ctx.arc(x + r - 2, y + l / 2, r + 0.4, 2.76 * PI, 1.24 * PI, true) ctx.lineTo(x, y) ctx.lineWidth = 2 ctx.fillStyle = 'rgba(255, 255, 255, 0.7)' ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)' ctx.stroke() ctx[operation]() ctx.globalCompositeOperation = 'overlay' } initImg() draw(canvas_ctx, 'fill') draw(block_ctx, 'clip') //添加移动事件 var block_slider = document.querySelector("#block"); var slider = document.querySelector(".verSliderBlock"); var slider_mark = document.querySelector("#bar-mask"); //用于判断当前是否是在按住滑块的情况下 var yd = false var moveX = 0 var downX = 0 //鼠标按下 slider.onmousedown = function (e) { downX = e.clientX; yd = true } //鼠标移动事件 function hadleMousemove(e) { if (yd) { moveX = e.clientX - downX; document.querySelector('#slide').innerHTML = '' if (moveX >= 310) { moveX = 310 - 40 } if (moveX > -2) { slider.style.backgroundColor = "#1991FA"; slider_mark.style.borderWidth = "1px" slider_mark.style.borderColor = "#1991fa" slider_mark.style.width = moveX + 40 + "px"; block_slider.style.left = (310 - 40 - 20) / (310 - 40) * moveX + "px"; slider.style.left = moveX + "px"; } } } //鼠标抬起事件 function hadleMouseup(e) { if (yd) { slider.onmousemove = null; console.log(moveX) block_slider.style.left = (310 - 40 - 20) / (310 - 40) * moveX + "px"; if (Math.abs((310 - 40 - 20) / (310 - 40) * moveX - x) < 10) { slider.style.background = "url('./success.png')"; slider.style.backgroundSize = '100%' // alert('验证成功') setTimeout(() => { rest(); }, 1000) } else { slider_mark.style.backgroundColor = "#fce1e1" slider_mark.style.borderWidth = "1px" slider_mark.style.borderColor = "#f57a7a" slider.style.backgroundColor = "#f57a7a"; slider.style.background = "url('./fail.png')"; slider.style.backgroundSize = '100%' setTimeout(() => { rest(); }, 1000) } yd = false } } //鼠标在按住滑块下移动 slider.onmousemove = function (e) { hadleMousemove(e) } //鼠标在滑块下抬起 slider.onmouseup = function (e) { hadleMouseup(e) } //设置全局的移动事件,当鼠标按下滑块后,移动过程中鼠标可能会移出滑块,这是滑块也会监听鼠标的移动进行相应的移动 document.addEventListener('mousemove', function (e) { hadleMousemove(e) }) document.addEventListener('mouseup', function (e) { hadleMouseup(e) }) function rest() { clean() document.querySelector('#slide').innerHTML = '向右滑动验证' slider.style.backgroundColor = "#fff"; slider.style.left = "0px" slider.style.background = "url('./right_arrow.png')"; slider.style.backgroundSize = '100%' block_slider.style.left = "0px" slider_mark.style.width = "0px" slider_mark.style.backgroundColor = "#d1e9fe" slider_mark.style.borderWidth = "0px" slider_mark.style.borderColor = "#d1e9fe" initImg() } //刷新 refresh.onclick = function(){ rest() } }(window))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

