如何使用js实现滑动拼图解锁(JS,开发技术)

时间:2024-05-06 17:29:05 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

具体内容如下

简单实现滑动解锁,效果图是这样的

如何使用js实现滑动拼图解锁

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>滑动解锁</title><style>.div1{width:200px;height:30px;border:1pxsolid#cccccc;user-select:none;position:relative;margin:0auto;margin-top:300px;}.div2{width:100%;height:30px;}.div3{position:absolute;top:0;left:0;width:36px;height:30px;line-height:30px;text-align:center;background:#fff;cursor:pointer;font-family:"宋体";z-index:10;font-weight:bold;color:#929292;/*cursor:move;*/}.div4{position:absolute;left:0;top:0;bottom:0;right:0;line-height:30px;text-align:center;z-index:-1;background:#ccc;}</style></head><body><!--大盒子--><divclass="div1"id='div1'><divclass="div2"id='div2'></div><spanclass="div3"id='div3'>>></span><divclass="div4"id='div4'>滑动解锁</div></div><script>//vara=1,b='1'//console.log(a+b,a-b,a++,b++)//conststr='console.log(1)';////replaceAll("\\(.*\\)","");//constnewStr=str.replace[/console\.log/,'return']//console.log(newStr)vardiv1=document.getElementById('div1');vardiv3=document.getElementById('div3');vardiv4=document.getElementById('div4');varleft;varpx=div1.offsetWidth-div3.offsetWidthdiv3.onmousedown=function(event){varevent=window.event||ev;left=event.clientX-div3.offsetLeft;//鼠标按下时的位置console.log(event)console.log(left)document.onmousemove=function(event){//鼠标移动varevent=window.event||ev;lefta=event.clientX-left;//鼠标移动的距离console.log(px,lefta);if(lefta<0){lefta=0;}elseif(px<lefta){lefta=px-2;}div3.style.left=lefta+'px';}document.onmouseup=function(event){//鼠标抬起varevent=window.event||ev;document.onmousemove=null;document.onmouseup=null;lefta=event.clientX-left;if(lefta<0){lefta=0;span.innerHTML='滑动解锁';}elseif(px<lefta){lefta=px-2;div4.innerHTML='解锁成功';div3.innerHTML='√';div3.onmousedown=null;alert('成功')}else{lefta=0;}div3.style.left=lefta+'px';}}</script></body></html>

由于拼图解锁我没做过,做了个大概的样子,效果图

如何使用js实现滑动拼图解锁

代码

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>滑动解锁</title><style>*{margin:0;padding:0;}.conter{width:202px;border:1pxsolid#ccc;margin:0auto;margin-top:300px;}.box{width:202px;height:130px;background:#ff00ff;position:relative;}.box::before{content:'';position:absolute;bottom:50px;right:30px;width:30px;height:30px;background:#fff;z-index:99;}.div1{position:absolute;bottom:0;width:200px;height:30px;border:1pxsolid#cccccc;user-select:none;position:relative;}.div2{width:100%;height:30px;}.div3{position:absolute;top:0;left:0;width:36px;height:30px;line-height:30px;text-align:center;background:#fff;cursor:pointer;font-family:"宋体";z-index:10;font-weight:bold;color:#929292;z-index:1000;}.div3::before{content:'';position:absolute;top:-81px;left:3px;width:30px;height:30px;background:#000;}.div4{position:absolute;left:0;top:0;bottom:0;right:0;line-height:30px;text-align:center;z-index:-1;background:#ccc;}</style></head><body><!--大盒子--><divclass="conter"><!--可以放图片计算位置--><divclass="box"></div><divclass="div1"id='div1'><divclass="div2"id='div2'></div><!--对应缺口位置拖动到相应位置既符合--><spanclass="div3"id='div3'>>></span><divclass="div4"id='div4'>滑动解锁</div></div></div><script>vardiv1=document.getElementById('div1');vardiv3=document.getElementById('div3');vardiv4=document.getElementById('div4');varleft;varpx=div1.offsetWidth-div3.offsetWidthdiv3.onmousedown=function(event){varevent=window.event||ev;left=event.clientX-div3.offsetLeft;//鼠标按下时的位置document.onmousemove=function(event){//鼠标移动varevent=window.event||ev;lefta=event.clientX-left;//鼠标移动的距离//console.log(lefta);if(lefta<0){lefta=0;}elseif(px<lefta){lefta=px;}div3.style.left=lefta+'px';}document.onmouseup=function(event){//鼠标抬起varevent=window.event||ev;document.onmousemove=null;document.onmouseup=null;lefta=event.clientX-left;console.log(lefta)if(lefta<0){lefta=0;div4.innerHTML='滑动解锁';//是否符合缺口位置}elseif(lefta>'134'&&lefta<'142'){div4.innerHTML='解锁成功';div3.innerHTML='√';div3.onmousedown=null;alert('成功')}else{alert('错误,请重试')lefta=0;}div3.style.left=lefta+'px';}}</script></body></html>
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:如何使用js实现滑动拼图解锁的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:MySQL聚合查询与联合查询操作的示例分析下一篇:

12 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18