功能

  • 点击按钮弹出登录框
  • 将鼠标放在登录框上边,可以拖动

    效果图

    image.png

代码

  1. <!DOCTYPE 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. <title>Document</title>
  8. <style>
  9. .login-header {
  10. width: 100%;
  11. text-align: center;
  12. height: 30px;
  13. font-size: 24px;
  14. line-height: 30px;
  15. }
  16. ul,
  17. li,
  18. ol,
  19. dl,
  20. dt,
  21. dd,
  22. div,
  23. p,
  24. span,
  25. h1,
  26. h2,
  27. h3,
  28. h4,
  29. h5,
  30. h6,
  31. a {
  32. padding: 0px;
  33. margin: 0px;
  34. }
  35. .login {
  36. /* 点击之前隐藏 */
  37. display: none;
  38. width: 512px;
  39. height: 280px;
  40. position: fixed;
  41. border: #ebebeb solid 1px;
  42. left: 50%;
  43. top: 50%;
  44. background: #ffffff;
  45. box-shadow: 0px 0px 20px #ddd;
  46. z-index: 9999;
  47. transform: translate(-50%, -50%);
  48. /* 垂直水平居中 */
  49. }
  50. .login-title {
  51. width: 100%;
  52. margin: 10px 0px 0px 0px;
  53. text-align: center;
  54. line-height: 40px;
  55. height: 40px;
  56. font-size: 18px;
  57. position: relative;
  58. cursor: move;
  59. }
  60. .login-input-content {
  61. margin-top: 20px;
  62. }
  63. .login-button {
  64. width: 50%;
  65. margin: 30px auto 0px auto;
  66. line-height: 40px;
  67. font-size: 14px;
  68. border: #ebebeb 1px solid;
  69. text-align: center;
  70. }
  71. .login-bg {
  72. /* 点击之前隐藏 */
  73. display: none;
  74. width: 100%;
  75. height: 100%;
  76. position: fixed;
  77. top: 0px;
  78. left: 0px;
  79. background: rgba(0, 0, 0, .3);
  80. }
  81. a {
  82. text-decoration: none;
  83. color: #000000;
  84. }
  85. .login-button a {
  86. display: block;
  87. }
  88. .login-input input.list-input {
  89. float: left;
  90. line-height: 35px;
  91. height: 35px;
  92. width: 350px;
  93. border: #ebebeb 1px solid;
  94. text-indent: 5px;
  95. }
  96. .login-input {
  97. overflow: hidden;
  98. margin: 0px 0px 20px 0px;
  99. }
  100. .login-input label {
  101. float: left;
  102. width: 90px;
  103. padding-right: 10px;
  104. text-align: right;
  105. line-height: 35px;
  106. height: 35px;
  107. font-size: 14px;
  108. }
  109. .login-title span {
  110. position: absolute;
  111. font-size: 12px;
  112. right: -20px;
  113. top: -30px;
  114. background: #ffffff;
  115. border: #ebebeb solid 1px;
  116. width: 40px;
  117. height: 40px;
  118. border-radius: 20px;
  119. }
  120. </style>
  121. </head>
  122. <body>
  123. <div class="login-header"><a id="link" href="javascript:;">点击,弹出登录框</a></div>
  124. <div id="login" class="login">
  125. <div id="title" class="login-title">登录会员
  126. <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
  127. </div>
  128. <div class="login-input-content">
  129. <div class="login-input">
  130. <label>用户名:</label>
  131. <input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
  132. </div>
  133. <div class="login-input">
  134. <label>登录密码:</label>
  135. <input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input">
  136. </div>
  137. </div>
  138. <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
  139. </div>
  140. <!-- 遮盖层 -->
  141. <div id="bg" class="login-bg"></div>
  142. <script>
  143. let login = document.querySelector('.login');
  144. let mask = document.querySelector('.login-bg');
  145. let link = document.querySelector('#link');
  146. let closeBtn = document.querySelector('#closeBtn');
  147. let title = document.querySelector('#title');
  148. // 点击link 弹出登录框和灰色的背景
  149. link.addEventListener('click',function() {
  150. mask.style.display = 'block';
  151. login.style.display = 'block';
  152. })
  153. // 点击closeBtn 隐藏
  154. closeBtn.addEventListener('click',function() {
  155. mask.style.display = 'none';
  156. login.style.display = 'none';
  157. })
  158. // 实现拖拽效果
  159. // 鼠标坐标-鼠标在盒子内的坐标=模态框真正的位置
  160. title.addEventListener('mousedown',function(e) {
  161. let x = e.pageX - login.offsetLeft;
  162. let y = e.pageY - login.offsetTop;
  163. // move
  164. document.addEventListener('mousemove',move);
  165. function move(e) {
  166. login.style.left = e.pageX - x + 'px';
  167. login.style.top = e.pageY - y + 'px';
  168. console.log(e.pageX - x + 'px');
  169. }
  170. // stop
  171. document.addEventListener('mouseup',function() {
  172. document.removeEventListener('mousemove',move);
  173. })
  174. })
  175. </script>
  176. </body>
  177. </html>