效果如下:

代码如下:

<div class="clickBtn">点击,弹出登录框</div>
<div class="login">
<h3>登录会员</h3>
<table>
<tr>
<td class="left">用户名:</td>
<td><input type="text" placeholder="请输入用户名" name="" id=""></td>
</tr>
<tr>
<td class="left">登陆密码:</td>
<td><input type="text" placeholder="请输入登录密码" name="" id=""></td>
</tr>
</table>
<button>登录会员</button>
<div class="closebtn">关闭</div>
</div> <div class="mask"></div>

style:

<style>
* {
margin: 0;
padding: 0;
} .mask {
display: none;
position: absolute;
top: 0;
left: 0;
height: 100vh;
width: 100%;
background-color: rgba(0, 0, 0, .3);
} .clickBtn {
margin: 10px auto;
width: 200px;
border: 0px;
font-size: 20px;
font-weight: 700;
background-color: #fff;
cursor: pointer;
} .login {
visibility: hidden; position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); display: flex;
flex-direction: column;
align-items: center; padding: 20px;
box-sizing: border-box; width: 500px;
height: 300px;
border: 1px solid #eee;
background-color: #fff;
z-index: 1;
} .login h3 {
margin-bottom: 20px;
font-size: 20px;
font-weight: 400;
height: 30px;
width: 100%;
text-align: center;
cursor: move;
} .login table {
flex: 1;
display: flex;
margin: 0;
padding: 0; } .login table tr {
height: 50px;
text-align: right;
} .login table tr input {
width: 300px;
height: 30px;
border-radius: 2px;
border: 1px solid #eee;
} .login table .left {
font-weight: 400;
width: 40%;
} .login button {
width: 260px;
height: 40px;
background-color: #fff;
border: 1px solid #eee;
border-radius: 2px;
cursor: pointer;
} .login button:hover {
color: #fff;
background-color: skyblue;
} .login .closebtn {
position: absolute;
right: 0;
top: 0;
transform: translate(50%, -50%);
width: 50px;
height: 50px;
text-align: center;
line-height: 50px;
background-color: orange;
border-radius: 50%;
cursor: pointer;
}
</style>

js:

<script>
let btn = document.querySelector('.clickBtn')
let login = document.querySelector('.login')
let closebtn = document.querySelector('.closebtn')
let mask = document.querySelector('.mask') let title = document.querySelector('h3')
let body = document.querySelector('body')
btn.addEventListener('click', function () {
mask.style.display = 'block'
login.style.visibility = 'visible'
}) closebtn.addEventListener('click', function () {
mask.style.display = 'none'
login.style.visibility = 'hidden'
}) title.addEventListener('mousedown', function (e) {
let x = e.pageX - login.offsetLeft
let y = e.pageY - login.offsetTop
function move(e) {
// 如果此处仍然用 x=e.pageX - x;login.style.left=x+'px' 就会出现错误,因为这里是浅拷贝,引用的是同一个地址,而mousedown事件一直触发mousemove事件,导致x对应地址的值一直在改变,且login.style.left=x+'px' 指向的也是这个地址,所以页面的区域就会出现晃动
var xx = e.pageX - x;
var yy = e.pageY - y;
console.log(x, y);
login.style.top = yy + 'px';
login.style.left = xx + 'px'; } // 在页面任何地方都应触发mousemove,故监听document的mousemove事件
document.addEventListener('mousemove', move) // 在页面任何地方都应触发mouseup,故监听document的mouseup事件
document.addEventListener('mouseup', function () {
document.removeEventListener('mousemove', move)
})
})
</script>

js-模态框的拖动的更多相关文章

  1. 完美解决bootstrap模态框允许拖动后拖出边界的问题

    使用bootstrap3版本 在网上看了很多方法,我觉得jquery-ui的实现方法是最简单有效的,具体实现方法 1.下载并引入jquery-ui插件 2.全局添加模态框允许拖动事件 $(docume ...

  2. [TimLinux] JavaScript 模态框可拖动功能实现——jQuery版

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. JS模态框 简单案例

    演示: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...

  4. js模态框实现原理

    <!DOCTYPE> <html> <head> <style>/* 定义模态对话框外面的覆盖层样式 */ #modal-overlay { visib ...

  5. 原生js模态框实现

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  6. [TimLinux] JavaScript 模态框可拖动功能实现——节流版

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. [TimLinux] JavaScript 模态框可拖动功能实现——原始版

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. js弹框3秒后自动消失

    开发中有时候会需要最出弹框后,过几秒自动消失的效果,下面给大家分享一下我自己做的一个小案例. 案例中的弹框使用的是bootstrap里面的模态框,实现自动消失则用的是js中的setInterval方法 ...

  9. Bootstrap——可拖动模态框(Model)

    还是上一个小项目,o(╥﹏╥)o,要实现点击一个div或者button或者一个东西然后可以弹出一个浮在最上面的弹框.网上找了找,发现Bootstrap的Model弹出框可以实现该功能,因此学习了一下, ...

  10. js控制Bootstrap 模态框(Modal)插件

    js控制Bootstrap 模态框(Modal)插件 http://www.cnblogs.com/zzjeny/p/5564400.html

随机推荐

  1. ubuntu18.04 安装cython_bbox

    方式一: pip install cython_bbox 方式二:下载地址 https://github.com/samson-wang/cython_bbox 解压后并cd cython_bbox- ...

  2. ElasticSearch (Es) 分组查询 记录

    首先表对应的实体类型: public class bm_info{ /// <summary> /// 单位 /// </summary> public  string sou ...

  3. hexo博客重新部署

    date: 2020-08-04 updated: 2020-12-31 summary: 博客重新部署到国内Gitee(加速访问速度) hexo博客重新部署(从GitHub到Gitee) (博客迁移 ...

  4. sxt_(003_007)_tomcat

    一.web服务器 接受浏览器请求,进行结果响应. 常见的web服务器:tomcat.jboss.weblogic.websphere.resin.jetty.二.tomcat下载: 去apache官网 ...

  5. 快速上手SpringBoot

    快速上手SpringBoot SpringBoot是用来简化Spring应用的初始化搭建以及开发过程 三个不需要,这是springboot使用mvc区别于其它框架的特点 tomcatd的端口 下一行是 ...

  6. 【根文件系统 】开发板通过nfs挂载ubuntu上的根文件系统

    在此基础上制作可用的根文件系统:https://www.cnblogs.com/hengqiu/p/15908597.html 1.ubuntu搭建nfs服务器 sudo apt-get instal ...

  7. win10彻底关闭系统更新 - 禁用 Windows Update Medic Service服务(1803和1809版本)

      这几天有点苦恼呀,不,应该说是挺苦恼,我的新本儿到了几天之后,就开始推荐更新,可以我从心里就不想着更新,那么就要阻止它了,按照原来的办法,进行了双重阻止,这在之前是屡试不爽的,但是还是一直在提醒我 ...

  8. mysql 导入问题排查

    ERR] 2006 - MySQL server has gone away -- 查询最大数 show global variables like 'max_allowed_packet'; -- ...

  9. scrapy框架学习(六)日志设置和数据存储

    日志设置 CRITICAL : 严重错误 ERROR : 一般错误 WARNING : 警告 INFO : 一般的信息 DEBUG : 调试信息 默认的显示级别是DEBUG # 设置错误显示级别 LO ...

  10. springboot集成Thumbnailator压缩图片

    一.参考大神博客 1.https://blog.51cto.com/u_11269274/5118649 2.https://blog.csdn.net/weixin_44722978/article ...