经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JSJS库框架 » JavaScript » 查看文章
前端滑块验证
来源:cnblogs  作者:Adver  时间:2018/10/16 9:29:29  对本文有异议

1.前端页面应用滑块验证可以防止页面频繁向后台请求数据;

2.主要用到js事件:

   onmousedown()   鼠标按下时响应

   onmousemove()   鼠标移动时响应

   onmouseup()        鼠标弹起时响应

3.获取页面距离:

   e.clientX

   obj.offsetWidth

   obj.offsetLeft

4.代码:

html:

  1. <div class="box">
  2. <div class="txt">滑块验证</div>
  3. <div class="btn">&gt;&gt;</div>
  4. <div class="bg"></div>
  5. </div>

css:

  1. *{
  2. -webkit-user-select: none;
  3. -moz-user-select: none;
  4. -ms-user-select: none;
  5. user-select: none;
  6. }
  7. .box{
  8. position: relative;
  9. width:300px;
  10. height:30px;
  11. background-color: #ccc;
  12. margin:20px auto;
  13. font-size:14px;
  14. line-height:30px;
  15. box-sizing:border-box;
  16. z-index:1;
  17. }
  18. .txt{
  19. position: absolute;
  20. left: 50%;
  21. top:0;
  22. transform: translateX(-50%);
  23. color:blue;
  24. z-index:3;
  25. }
  26. .btn{
  27. position: absolute;
  28. top:0;
  29. left: 0;
  30. width:40px;
  31. height:30px;
  32. border:1px solid #ccc;
  33. background-color: #fff;
  34. text-align: center;
  35. line-height: 30px;
  36. cursor: move;
  37. box-sizing: border-box;
  38. z-index:4;
  39. }
  40. .bg{
  41. position: absolute;
  42. left: 0;
  43. top:0;
  44. width:0;
  45. height:30px;
  46. background-color:green;
  47. box-sizing: border-box;
  48. z-index:2;
  49. }

js:

  1. window.onload = function(){
  2. var box = document.querySelector(".box"),
  3. txt = document.querySelector(".txt"),
  4. btn = document.querySelector(".btn"),
  5. bg = document.querySelector(".bg"),
  6. end = false;
  7. btn.onmousedown = function(e){
  8. var e = e || window.event;
  9. var point = e.clientX - box.offsetLeft;
  10. btn.onmousemove = function(e){
  11. var moveW = e.clientX - box.offsetLeft - point;
  12. btn.style.left = moveW + "px";
  13. bg.style.width = moveW + "px";
  14. if(btn.offsetLeft<=0){
  15. btn.style.left = "0";
  16. }
  17. if(btn.offsetLeft>=(box.clientWidth - btn.clientWidth)){
  18. btn.style.left = box.clientWidth - btn.clientWidth
  19. txt.innerHTML = "验证完成";
  20. btn.onmousemove = null;
  21. btn.onmousedown = null;
  22. end = true;
  23. }
  24. }
  25. btn.onmouseup = function(){
  26. btn.onmousemove = null;
  27. if(!end){
  28. btn.style.left = "0";
  29. bg.style.width = "0";
  30. }
  31. }
  32. }
  33. }

    5.显示

 

  1.  

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号