• javascript案例-----拖动模态框


    模态框,我们也叫弹出框,可以在网易云,京东等之类的网页中看到

    效果如下:

     

    代码思路:

    1.         1.点击弹出层,会弹出模态框,并且显示灰色半透明的遮挡层。
    2.         2.点击关闭按钮,可以关闭模态框,并且同时关闭灰色半透明遮挡层。
    3.         3.鼠标放到模态框最上面一行,可以按住鼠标拖拽模态框在页面中移动。
    4.         4.鼠标松开,可以停止拖动模态框移动。
    5. 在页面中拖拽的原理︰鼠标按下并且移动,之后松开鼠标
              
    6. 触发事件是鼠标按下mousedown,鼠标移动mousemove鼠标松开mouseup
            
    7.  拖拽过程:鼠标移动过程中,获得最新的值赋值给模态框的left和top值,这样模态框可以跟着鼠标走了
              
    8. 鼠标按下触发的事件源是最上面一行.就是id 为title
              
    9. 鼠标的坐标减去鼠标在盒子内的坐标,才是模态框真正的位置。
              
    10. 鼠标按下,我们要得到鼠标在盒子的坐标。
              
    11. 鼠标移动,就让模态框的坐标设置为︰鼠标坐标减去盒子坐标即可,注意移动事件写到按下事件里面。

    代码步骤:

    1.         1.点击弹出层,模态框和遮挡层就会显示出来 display:block
    2.                 1.1点击弹出层这个链接link  让mark 和login 显示出来
    3.        2. 点击关闭按钮,模态框和遮挡层就会隐藏出来 display:none
    4.                 2.1点击弹出层这个关闭按钮closeBtn  让mark 和login 隐藏起来
    5.        3.开始拖拽
    6.                    1)当鼠标按下,就获得鼠标在盒子内的坐标
    7.                       2)鼠标移动的时候,把鼠标在页面中的坐标 减去 鼠标在盒子中的坐标就是模态框的left和top值 注意移动事件写到按下事件里面。
    8.                      (3)鼠标弹起,就让鼠标事件移除

    具体每一步的步骤也会在代码中写出来 

    html部分:

    1. <div class="login-header">
    2. <a href="javascript:;" id="link">
    3. 点击,弹出登录框
    4. </a>
    5. </div>
    6. <div id="login" class="login">
    7. <div id="title" class="login-title">登录会员
    8. <span>
    9. <a href="javascript:void(0);" id="closeBtn">
    10. 关闭
    11. </a>
    12. </span>
    13. </div>
    14. <div class="login-input-content">
    15. <div class="login-input">
    16. <label>用户名:</label>
    17. <input type="text" name="info[name]" id="username" placeholder="请输入用户名" />
    18. </div>
    19. <div class="login-input">
    20. <label>登录密码</label>
    21. <input type="password" name="info[password]" id="password" placeholder="请输入登录密码" />
    22. </div>
    23. </div>
    24. <div id="loginBtn" class="login-button">
    25. <a href="javascript:void(0)" id="login-button-submit"></a>
    26. </div>
    27. </div>
    28. <!-- 遮盖层 -->
    29. <div id="bg" class="login-bg">
    30. </div>

    css部分:

    1. .login-header>a {
    2. position: fixed;
    3. top: 20px;
    4. left: 40%;
    5. font-size: 30px;
    6. }
    7. .login {
    8. display: none;
    9. width: 512px;
    10. height: 280px;
    11. position: fixed;
    12. border: 1px solid #EBEBEB;
    13. left: 50%;
    14. top: 50%;
    15. background-color: #fff;
    16. box-shadow: 0 0 20px #ddd;
    17. z-index: 999;
    18. transform: translate(-50%, -50%);
    19. }
    20. .login-title {
    21. width: 100%;
    22. margin: 10px 0 0 0;
    23. text-align: center;
    24. line-height: 40px;
    25. height: 40px;
    26. position: relative;
    27. cursor: move;
    28. }
    29. .login-input-content {
    30. margin-top: 20px;
    31. }
    32. .login-button {
    33. width: 50%;
    34. margin: 30px auto 0 auto;
    35. line-height: 40px;
    36. font-size: 14px;
    37. border: 1px solid #EBEBEB;
    38. text-align: center;
    39. }
    40. .login-bg {
    41. display: none;
    42. width: 100%;
    43. height: 100%;
    44. position: fixed;
    45. top: 0;
    46. left: 0;
    47. background: rgba(0, 0, 0, 0.3);
    48. }
    49. a {
    50. text-decoration: none;
    51. color: #000000;
    52. }
    53. .login-button a {
    54. display: block;
    55. }
    56. .login-input input.list input {
    57. float: left;
    58. line-height: 35px;
    59. width: 350px;
    60. border: 1px solid #EBEBEB;
    61. text-indent: 5px;
    62. }
    63. .login-input {
    64. overflow: hidden;
    65. margin: 0 0 20px 40px;
    66. }
    67. .login-input label {
    68. float: left;
    69. width: 90px;
    70. padding-right: 10px;
    71. line-height: 35px;
    72. height: 35px;
    73. font-size: 14px
    74. }
    75. .login-title span {
    76. position: absolute;
    77. font-size: 12px;
    78. right: -10px;
    79. top: -30px;
    80. background-color: #fff;
    81. border: 1px solid #ebebeb;
    82. width: 40px;
    83. height: 40px;
    84. border-radius: 20px;
    85. }

    javascript部分:

    1. //1.获取元素
    2. var login = document.querySelector('.login');
    3. //遮盖层
    4. var mark = document.querySelector('.login-bg');
    5. // 显示按钮
    6. var link = document.querySelector('#link');
    7. //关闭按钮
    8. var closeBtn = document.querySelector('#closeBtn');
    9. //在title区域按下鼠标才可以拖动
    10. var title = document.querySelector('#title')
    11. // 2.点击弹出层这个链接link 让mark 和login 显示出来
    12. link.addEventListener('click', function() {
    13. mark.style.display = 'block'
    14. login.style.display = 'block'
    15. })
    16. // 3.点击弹出层这个关闭按钮closeBtn 让mark 和login 隐藏起来
    17. closeBtn.addEventListener('click', function() {
    18. mark.style.display = 'none'
    19. login.style.display = 'none'
    20. })
    21. // 4.开始拖拽
    22. //(1)当鼠标按下,就获得鼠标在盒子内的坐标
    23. title.addEventListener('mousedown', function(e) {
    24. var x = e.pageX - login.offsetLeft;
    25. var y = e.pageY - login.offsetTop;
    26. // (2)鼠标移动的时候,把鼠标在页面中的坐标 减去 鼠标在盒子中的坐标就是模态框的left和top值 注意移动事件写到按下事件里面。
    27. document.addEventListener('mousemove', move)
    28. function move(e) {
    29. login.style.left = e.pageX - x + 'px'
    30. login.style.top = e.pageY - y + 'px'
    31. }
    32. // (3)鼠标弹起,就让鼠标事件移除
    33. document.addEventListener('mouseup', function(e) {
    34. document.removeEventListener('mousemove', move)
    35. })
    36. })

  • 相关阅读:
    如何用python一键去除图片、PDF水印?
    【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
    读《反无效努力工作法》
    Visual Studio Code 中编译和调试 CMake 项目(Milvus Knowhere为例)
    计算机毕业设计ssm宠物商店信息展示与服务订购系统7q5ic系统+程序+源码+lw+远程部署
    django系列之事务操作
    cf1693C. Keshi in Search of AmShZ(div1)【最短路,反向建图】
    自学Python07-学会用Python读取Json 文件
    linux日志文件删除
    MATLAB中findpeaks函数使用
  • 原文地址:https://blog.csdn.net/weixin_45904557/article/details/125399645