• JS——利用js实现模态框的拖拽


    文章目录

    • 预备知识
      • 1.offset 偏移量
      • 2.offset 与 style 区别
    • 二、实现过程
      • 1.示例图
      • 2.实现思路
      • 3.具体代码实现
    • 总结

    一、预备知识

    1.offset 偏移量

    offset 翻译过来就是偏移量, 我们使用 offset系列相关属性可以动态的得到该元素的位置(偏移)、大小等。

    • - offset 可以得到任意样式表中的样式值
    • - offset 系列获得的数值是没有单位的
    • - offsetWidth 包含padding+border+width
    • - offsetWidth 等属性是只读属性,只能获取不能赋值
    • - offsetLeft  返回相对于父元素左边框的距离,动态的。
    • - offsetTop  返回相对于父元素上方的举例

    2.offset 与 style 区别

    • - offsetWidth 数值是没有单位的
    • - offsetWidth 包含padding+border+width
    • - offsetWidth 只读属性,只能获取不能赋值
    • - style.width 获得的是带有单位的字符串,只获取行内样式。
    • - style.width 获得不包含padding和border 的值
    • - style.width 是可读写属性,可以获取也可以赋值

    总结:获取元素位置用offset ,修改元素属性值用style

    二、实现过程

    1.示例图

     2.实现思路

    模态款的拖拽在许多页面中都很常见,那么如何实现呢?在许多应用中常采取点击某模态框标题栏的方式,该区域即会随着鼠标的移动而移动。

    1. 本例以鼠标单击为例,获取在鼠标在该模态框标题栏的位置。
    2. 随即获取当 鼠标移动时(mousemove)该模态框最新的(页面左边的距离)(pageX)减去鼠标在标题栏的横向距离,就是模态框的left与top值。
    3. 鼠标松开时(mouseup),移除mousemove 事件

    3.具体代码实现

    3.1 HTML结构

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <link rel="stylesheet" href="../css/reset.css">
    8. <link rel="stylesheet" href="../css/login-one.css">
    9. <title>网易云音乐title>
    10. head>
    11. <body>
    12. <div class="container">
    13. <div class="wrap">
    14. <div class="title">
    15. <span class="login-one">登录span>
    16. <span><a href="javascript:;" class="quite">×a>span>
    17. div>
    18. <div class="user-login">
    19. <div class="user-login-inner">
    20. <div class="left-login">
    21. <img src="../img/登录页面图库/登录页面图标.png" alt="">
    22. <a href="#" class="byphone-login">手机号登录a>
    23. <a href="#" class="register">注册a>
    24. div>
    25. <ul class="right-login">
    26. <li><a href="#"><i>i>微信登录a>li>
    27. <li><a href="#"><i>i>QQ登录a>li>
    28. <li><a href="#"><i>i>微博登录a>li>
    29. <li><a href="#"><i>i>网易邮箱账号登录a>li>
    30. ul>
    31. div>
    32. div>
    33. <div class="user-agree">
    34. <input type="checkbox" name="user-agree">
    35. <span class="agree">同意span>
    36. <a href="#">《服务条款》a>
    37. <a href="#">《隐私政策》a>
    38. <a href="#">《儿童隐私政策》a>
    39. div>
    40. <div class="code">
    41. <img src="../img/登录页面图库/二维码.png" alt="">
    42. div>
    43. div>
    44. div>
    45. <script src="../INDEXJS/login-one.js">script>
    46. body>
    47. html>

    3.2 CSS样式

    1. /* 外部容器样式设置 */
    2. .wrap {
    3. margin: 200px auto;
    4. width: 530px;
    5. height: 366px;
    6. background-color: #ffffff;
    7. position: relative;
    8. }
    9. /* 顶部标题样式设置 */
    10. .title {
    11. height: 39px;
    12. line-height: 39px;
    13. background-color: #2d2d2d;
    14. padding: 0px 25px 0px 18px;
    15. border-bottom: 1px solid #191919;
    16. border-radius: 4px 4px 0 0;
    17. }
    18. .login-one {
    19. font-weight: bold;
    20. font-size: 14px;
    21. color: #fff;
    22. }
    23. .quite {
    24. margin-left: 20px;
    25. float: right;
    26. color: #888888;
    27. font-size: 23px;
    28. }
    29. /* 用户登录区域样式设置*/
    30. .user-login {
    31. width: 528px;
    32. height: 326px;
    33. border: 1px solid #d8d8d8;
    34. border-top: none;
    35. border-radius: 0 0 4px 4px;
    36. overflow: hidden;
    37. }
    38. .user-login-inner {
    39. width: 488px;
    40. height: 266.26px;
    41. margin: 30px 20px;
    42. position: relative;
    43. }
    44. /* 左侧登录区域 */
    45. .left-login {
    46. width: 225px;
    47. height: 205.3px;
    48. padding: 0px 35px 3px 40px;
    49. border-right: 1px dotted #ccc;
    50. }
    51. .left-login img {
    52. width: 224px;
    53. height: 120px;
    54. }
    55. .left-login a {
    56. display: inline-block;
    57. width: 224px;
    58. height: 31px;
    59. line-height: 31px;
    60. text-align: center;
    61. border-radius: 5px;
    62. font-size: 12px;
    63. margin-top: 10px;
    64. }
    65. .byphone-login {
    66. background-color: #2d7dca;
    67. border: 1px solid #cdcccc;
    68. color: #fff;
    69. }
    70. .byphone-login:hover {
    71. background-color: #4a94d9;
    72. }
    73. .register {
    74. background-color: #f3f3f3;
    75. border: 1px solid #cbcbcb;
    76. color: #333333;
    77. }
    78. .register:hover {
    79. background-color: #ffffff;
    80. }
    81. /* 右侧用户多选登录方案区域样式设置 */
    82. .right-login {
    83. position: absolute;
    84. top: 0;
    85. right: 0;
    86. width: 148px;
    87. height: 214.21px;
    88. margin-top: -15px;
    89. padding: 3px 0 3px 39px;
    90. }
    91. .right-login a {
    92. display: inline-block;
    93. width: 148px;
    94. height: 38.55px;
    95. line-height: 38.55px;
    96. margin-top: 15px;
    97. font-size: 12px;
    98. color: #333333;
    99. }
    100. .right-login li a:hover {
    101. text-decoration: underline;
    102. }
    103. .right-login i {
    104. display: inline-block;
    105. vertical-align: middle;
    106. margin-right: 14px;
    107. width: 38px;
    108. height: 38px;
    109. }
    110. .right-login li:first-child i {
    111. background-image: url(../img/登录页面图库/通讯软件logo.png);
    112. background-position: -150px -670px;
    113. }
    114. .right-login li:nth-child(2) i {
    115. background-image: url(../img/登录页面图库/通讯软件logo.png);
    116. background-position: -190px -670px;
    117. }
    118. .right-login li:nth-child(3) i {
    119. background-image: url(../img/登录页面图库/通讯软件logo.png);
    120. background-position: -231px -670px;
    121. }
    122. .right-login li:nth-child(4) i {
    123. background-image: url(../img/登录页面图库/通讯软件logo.png);
    124. background-position: -271px -670px;
    125. }
    126. /* 底部同意服务条款区域样式设置 */
    127. .user-agree {
    128. position: relative;
    129. top: -50px;
    130. margin-left: 33px;
    131. }
    132. .agree {
    133. font-family: NotoSansHans-Regular;
    134. font-size: 10px;
    135. color: rgba(0, 0, 0, 0.4);
    136. line-height: 15px;
    137. margin-left: -4px;
    138. margin-right: -5px;
    139. }
    140. .user-agree a {
    141. color: #507DAF;
    142. cursor: pointer;
    143. font-size: 12px;
    144. margin-right: -4px;
    145. }
    146. input[type=checkbox] {
    147. vertical-align: middle;
    148. border-color: #767676;
    149. }
    150. input[type=checkbox] {
    151. border-color: #4f4f4f;
    152. }
    153. /* 底部右侧二维码样式设置 */
    154. .code img {
    155. width: 52px;
    156. height: 52px;
    157. position: absolute;
    158. right: 0;
    159. bottom: 0;
    160. }

    3.3 JS 行为

    1. // 实现当鼠标点击标题栏时登录框随着鼠标的移动而移动效果
    2. /*
    3. 思路 ——
    4. 1.获取鼠标位于当前标题栏的位置
    5. 2.mousemove 鼠标移动的时获取最新的pageX减去
    6. 鼠标在登录框的X距离,就是模态框的left与top值。
    7. */
    8. // 获取外部容器
    9. var wrap = document.querySelector(".wrap");
    10. // 获取标题栏
    11. var title = document.querySelector(".title");
    12. // 为标题栏添加鼠标点击事件
    13. title.addEventListener("click", function (e) {
    14. var x = e.pageX - wrap.offsetLeft;
    15. var y = e.pageY - wrap.offsetTop;
    16. // (2) 鼠标移动的时候,把鼠标在页面中的坐标,减去 鼠标在盒子内的坐标就是模态框的left和top值
    17. document.addEventListener("mousemove", move);
    18. function move(e) {
    19. wrap.style.left = e.pageX - x - 580 + "px";
    20. wrap.style.top = e.pageY - y - 200 + "px";
    21. }
    22. // (3) 鼠标弹起,就让鼠标移动事件移除
    23. document.addEventListener("dblclick", function () {
    24. document.removeEventListener("mousemove", move);
    25. });
    26. });

    总结

    以上就是今日所要分享的内容,最后,依旧诚挚祝福屏幕前的你健康幸福、平安喜乐。

  • 相关阅读:
    不同的方式检查Null
    【JavaSE】初识泛型
    配置vscode免密登录本地Ubuntu
    论文复现(一)
    【云原生】Kubernetes PDB(Pod Disruption Budget)介绍与简单使用
    SINetv2:Concealed Object Detection——论文简述
    模型部署 — PaddleNLP 基于 Paddle Serving 快速使用(服务化部署 - Docker)— 图像识别 + 信息抽取(UIE-X)
    Docker部署Logstash 7.2.0
    深圳库卡机器人KR460控制柜维修快速解决
    社交网络用户行为分析,各类社交软件用户分析
  • 原文地址:https://blog.csdn.net/Bonsoir777/article/details/126592058