经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
js实现模态框的拖拽效果
来源:jb51  时间:2022/7/4 8:51:13  对本文有异议

本文实例为大家分享了js实现模态框拖拽效果的具体代码,供大家参考,具体内容如下

之前学习js遇到了这样的需求:
鼠标按下后,移动鼠标,模态框随鼠标移动,鼠标松开,模态框也不会随鼠标移动。<完整的代码在最后哦>

分析思路:

1.点击弹出层,模态框和遮挡层就会显示出来。display:block
2.点击关闭按钮,模态框和遮挡层就会隐藏。display:none

3.在页面中拖拽的步骤:鼠标按下并移动,之后松开鼠标
4.触发事件是鼠标按下mousedown,鼠标移动是mousemove,鼠标松开:mouseup
5.拖拽过程:鼠标移动过程中,获得最新的值赋值给模态框的left和top值,这样模态框就可以跟着鼠标走了
6.鼠标按下触发的时间源是最上面一行,也就是说,鼠标只有放在最上面一行,才能触发该事件。放在其他区域不会触发该事件。
7.鼠标的坐标减去鼠标在盒子内的坐标,才是模态框真正的位置。(因为模态框是可移动的,只有第一次才能拿到模态框的left和top,其他时候并不能直接拿到。所以采用‘鼠标的坐标 - 鼠标在模态框内的坐标’来计算模态框的位置)

8.鼠标按下,我们要得到鼠标在盒子内的坐标
9.鼠标移动,就让模态框的坐标设置为:鼠标坐标 - 盒子坐标即可。注意移送事件要写到按下事件里面
10.鼠标松开,就停止拖拽,可以让鼠标移动事件解除

代码实现:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. ? ? <meta charset="UTF-8">
  6. ? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. ? ? <title>模态框拖拽</title>
  9. ? ? <style>
  10. ? ? ? ? * {
  11. ? ? ? ? ? ? margin: 0;
  12. ? ? ? ? ? ? padding: 0;
  13. ? ? ? ? }
  14.  
  15. ? ? ? ? #link {
  16. ? ? ? ? ? ? color: #000;
  17. ? ? ? ? ? ? text-decoration: none;
  18. ? ? ? ? ? ? border: 1px solid #000;
  19. ? ? ? ? }
  20.  
  21. ? ? ? ? .login {
  22. ? ? ? ? ? ? width: 300px;
  23. ? ? ? ? ? ? height: 200px;
  24. ? ? ? ? ? ? background-color: antiquewhite;
  25. ? ? ? ? ? ? position: absolute;
  26. ? ? ? ? ? ? top: 50%;
  27. ? ? ? ? ? ? left: 50%;
  28. ? ? ? ? ? ? transform: translate(-50%, -50%);
  29. ? ? ? ? ? ? z-index: 2;
  30. ? ? ? ? ? ? display: none;
  31. ? ? ? ? }
  32.  
  33. ? ? ? ? .login-title {
  34. ? ? ? ? ? ? text-align: center;
  35. ? ? ? ? ? ? width: 100%;
  36. ? ? ? ? ? ? height: 40px;
  37. ? ? ? ? ? ? line-height: 40px;
  38. ? ? ? ? ? ? background-color: aqua;
  39. ? ? ? ? ? ? cursor: move;
  40.  
  41. ? ? ? ? }
  42.  
  43. ? ? ? ? .login-title span {
  44. ? ? ? ? ? ? display: block;
  45. ? ? ? ? ? ? height: 30px;
  46. ? ? ? ? ? ? width: 30px;
  47. ? ? ? ? ? ? background-color: antiquewhite;
  48. ? ? ? ? ? ? line-height: 30px;
  49. ? ? ? ? ? ? border-radius: 50%;
  50. ? ? ? ? ? ? position: absolute;
  51. ? ? ? ? ? ? top: -10px;
  52. ? ? ? ? ? ? right: -10px;
  53. ? ? ? ? ? ? font-size: 12px;
  54.  
  55. ? ? ? ? }
  56.  
  57. ? ? ? ? .login-title span a {
  58. ? ? ? ? ? ? text-decoration: none;
  59. ? ? ? ? ? ? color: #000;
  60. ? ? ? ? }
  61.  
  62. ? ? ? ? .login-input-content {
  63. ? ? ? ? ? ? margin: 15px 20px 0;
  64. ? ? ? ? ? ? line-height: 30px;
  65. ? ? ? ? }
  66.  
  67. ? ? ? ? .login-button {
  68. ? ? ? ? ? ? width: 200px;
  69. ? ? ? ? ? ? height: 20px;
  70. ? ? ? ? ? ? margin: 10px auto;
  71. ? ? ? ? ? ? border: 1px solid rgb(77, 73, 73);
  72. ? ? ? ? ? ? text-align: center;
  73.  
  74. ? ? ? ? }
  75.  
  76. ? ? ? ? .login-button a {
  77. ? ? ? ? ? ? text-decoration: none;
  78. ? ? ? ? ? ? color: #000;
  79. ? ? ? ? ? ? font-size: 14px;
  80. ? ? ? ? }
  81.  
  82. ? ? ? ? #bg {
  83. ? ? ? ? ? ? display: none;
  84. ? ? ? ? ? ? background-color: #000;
  85. ? ? ? ? ? ? width: 100%;
  86. ? ? ? ? ? ? height: 100%;
  87. ? ? ? ? ? ? opacity: 0.3;
  88. ? ? ? ? ? ? z-index: -1;
  89. ? ? ? ? ? ? position: absolute;
  90. ? ? ? ? ? ? top: 0;
  91. ? ? ? ? ? ? left: 0;
  92. ? ? ? ? }
  93. ? ? </style>
  94. </head>
  95.  
  96. <body>
  97. ? ? <div class="login-header"><a href="javascript:;" id="link">点击,弹出登录框</a></div>
  98. ? ? <div class="login" id="login">
  99. ? ? ? ? <div class="login-title">登录会员
  100. ? ? ? ? ? ? <span><a id="colseBth" href="javascript:void(0);" class="close-login">关闭</a></span>
  101. ? ? ? ? </div>
  102. ? ? ? ? <div class="login-input-content">
  103. ? ? ? ? ? ? <div class="login-input">
  104. ? ? ? ? ? ? ? ? <label for="">用 户 名:</label>
  105. ? ? ? ? ? ? ? ? <input type="text" placeholder="请输入用户名" name="info[sername]" id="username" class="username">
  106. ? ? ? ? ? ? </div>
  107. ? ? ? ? ? ? <div class="login-inpit">
  108. ? ? ? ? ? ? ? ? <label for="">登录密码:</label>
  109. ? ? ? ? ? ? ? ? <input type="password" placeholder="请输入登录密码" name="info[password]" id="password">
  110.  
  111. ? ? ? ? ? ? </div>
  112. ? ? ? ? </div>
  113. ? ? ? ? <div class="login-button" id="loginBtn"><a href="javascript:void(0);" id="login-button-submit">会员登录</a></div>
  114. ? ? </div>
  115. ? ? <!-- 遮罩层 -->
  116. ? ? <div class="login-bg" id="bg"></div>
  117.  
  118. </body>
  119. <script>
  120. ? ? // 1.点击,弹出模态框和遮罩层
  121. ? ? // 3.点击关闭模态框和遮罩层自动隐藏
  122. ? ? // 4.页面拖拽原理:鼠标按下且移动,之后松开鼠标
  123. ? ? // 5.拖拽过程:鼠标移动的时候获得新的值赋值给模态框的left和top值。
  124. ?? ?
  125. ?? ?//1.获取DOM元素
  126. ? ? var oLink = document.querySelector('#link');
  127. ? ? var oLogin = document.querySelector('.login');
  128. ? ? var oBg = document.querySelector('#bg');
  129. ? ? var oClose = oLogin.querySelector('#colseBth');
  130. ? ? var title = oLogin.querySelector('.login-title');
  131.  
  132. ?? ?//2.点击弹出层这个链接link,让mask和login显示出来
  133. ? ? oLink.addEventListener('click', function () {
  134. ? ? ? ? oLogin.style.display = 'block';
  135. ? ? ? ? oBg.style.display = 'block';
  136. ? ? })
  137. ?? ?//3.点击closeBtn就隐藏mask和login
  138. ? ? oClose.addEventListener('click', function () {
  139. ? ? ? ? oLogin.style.display = 'none';
  140. ? ? ? ? oBg.style.display = 'none';
  141. ? ? })
  142. ?? ?//4.开始拖拽
  143. ?? ?//(1)当我们鼠标按下,就获得鼠标在盒子内的坐标
  144. ? ? title.addEventListener('mousedown', function (e) {
  145. ? ? ? ? var x = e.pageX - oLogin.offsetLeft;
  146. ? ? ? ? var y = e.pageY - oLogin.offsetTop;
  147. ? ? ? ? console.log(x, y)
  148. ?? ??? ?
  149. ?? ??? ?//(2)鼠标移动的时候,把鼠标在页面中的坐标 减去 鼠标在盒子内的坐标就是模态框的left和top值
  150. ? ? ? ? document.addEventListener('mousemove', move)?
  151. ? ? ? ? ? ? function move(e) {
  152. ? ? ? ? ? ? ? ? oLogin.style.left = e.pageX - x + 'px';
  153. ? ? ? ? ? ? ? ? oLogin.style.top = e.pageY - y + 'px';
  154. ? ? ? ? ? ? }
  155. ?? ??? ??? ?
  156. ?? ??? ??? ?//(3)鼠标弹起,就让鼠标移动事件移除
  157. ? ? ? ? ? ? document.addEventListener('mouseup', function () {
  158. ? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move);
  159. ? ? ? ? ? ? ? ??
  160. ? ? ? ? ? ? })
  161. ? ? ? ? })
  162. ? ??
  163. </script>
  164.  
  165. </html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

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

本站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号