javascript实现滑动解锁功能
程序员文章站
2023-11-02 13:20:52
效果图:
代码如下:
效果图:
代码如下:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <style type="text/css"> * { margin: 0; padding: 0; } #slider-box { width: 300px; height: 50px; border-radius: 4px; background: #ccc; margin: 250px auto; position: relative; } #slider { width: 48px; height: 48px; border: 1px solid #eee; text-align: center; line-height: 48px; display: inline-block; background: #fff; border-radius: 4px; cursor: move; position: absolute; left: 0; z-index: 5; } #slider-text { text-align: center; line-height: 50px; display: inline-block; width: 100%; height: 50px; font-family: "微软雅黑"; position: absolute; left: 0; z-index: 4; } #slider-bg { width: 0; height: 48px; background: green; position: absolute; z-index: 3; border-radius: 4px; } #slider-emerge { width: 100px; background:; height: 50px; position: absolute; } #stop-go { width: 48px; height: 48px; border: 1px solid #eee; background:#36f; position: absolute; right: -1px; display: none; text-align: center; line-height: 48px; color: #fff; font-family: "微软雅黑"; border-radius: 4px; z-index: 5; } div{ -moz-user-select:none; -webkit-user-select:none; user-select:none; } </style> </head> <body> <div id="slider-box"> <span id="slider">></span> <span id="slider-text">滑动解锁</span> <span id="slider-bg"></span> <span id="slider-emerge"></span> <span id="stop-go">∨</span> </div> </body> <script type="text/javascript"> var sliderel={ $: function(selector){ return document.getelementbyid(selector) }, getevent:function(e){ var e=e || window.event return e; }, stopbubble:function(e){ var e =this.getevent(e) if(typeof e.preventdefault != "undefined"){ e.preventdefault(); }else{ e.returnvalue = false; } } }, elemt={ flag:false, nowmosex: 0, mx:sliderel.$("slider-box"), sd:sliderel.$("slider"), st:sliderel.$("slider-text"), sb:sliderel.$("slider-bg"), se:sliderel.$("slider-emerge"), sg:sliderel.$("stop-go"), } elemt.sd.onmousedown=function(e){ var e =sliderel.getevent(e) sliderel.stopbubble(e); elemt.flag=true nowmosex=e.clientx-elemt.sd.offsetleft; } //滑块最大移动的距离 maxmove=elemt.mx.offsetwidth -elemt.sd.offsetwidth; //鼠标移动的时候是否成功 elemt.mx.onmousemove=function(e){ var e =sliderel.getevent(e) if(elemt.flag){ var movex=e.clientx-nowmosex; var oelemleft=elemt.sd.offsetleft;//判断滑块移动的范围 if(oelemleft<0){ //判断滑块是否超出限制位置 movex=0; elemt.flag=false }else if(oelemleft>maxmove){ movex=maxmove; elemt.sg.style.display="block"; elemt.sd.style.display="none" elemt.sb.style.width=300+"px" elemt.st.innerhtml="滑动成功" elemt.st.style.color="#fff" } } elemt.sd.style.left=movex+"px" elemt.sb.style.width=oelemleft+20+"px"; } //当鼠抬起判断是否滑动成功 elemt.mx.onmouseup=function(e){ var e =sliderel.getevent(e) elemt.flag=false if(elemt.sd.offsetleft<maxmove){ speed=math.ceil(elemt.sd.offsetleft/40); time=setinterval(function(){ if(elemt.sd.offsetleft>=0){ elemt.sd.style.left=elemt.sd.offsetleft-speed+"px"; elemt.sb.style.width=elemt.sb.offsetwidth-speed+"px"; }else{ clearinterval(time); return false; } },10) } } //当鼠离开是否滑动成功 elemt.sd.onmouseout=function(e){ sliderel.stopbubble(e); elemt.flag=false; if( elemt.sd.offsetleft<maxmove){ speed=math.ceil(elemt.sd.offsetleft/40); time=setinterval(function(){ if(elemt.sd.offsetleft>=0){ elemt.sd.style.left=elemt.sd.offsetleft-speed+"px"; elemt.sb.style.width=elemt.sb.offsetwidth-speed+"px"; }else{ clearinterval(time); return false; } },10); } } </script> </html>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
下一篇: 企业网站建设要分五步走