JS实现拖动模态框

目录

核心思路讲解:

HTML和CSS代码: 

 JavaScript代码:


核心思路讲解:

h3.addEventListener('mousedown',function(e) { 
       h3.style.cursor = 'move';
       var x = e.pageX - wrap.offsetLeft;
       var y = e.pageY - wrap.offsetTop;

 因为是此时点击触发,所以获取到此时鼠标在框内的坐标是固定不变的。

 document.addEventListener('mousemove',move); //
        function move(e) {
            wrap.style.left = e.pageX - x + 'px';
            wrap.style.top = e.pageY - y + 'px';
        }

鼠标移动事件要添加到document,因为实在整个页面移动。移动时动态获取鼠标的坐标,将此鼠标的坐标

HTML和CSS代码: 

    <style>
        *{
            margin: 0;padding: 0;
        }
        h1{
            width: 300px;
            text-align: center;
            background-color: bisque;
            position: absolute;
            z-index: 1;
            left: 600px;
        }
        .wrap{
            width: 450px;
            height: 240px;
            border: 1px solid #ccc;
            box-shadow: 0 0 15px #aaa;
            position:absolute;
            left: 500px;
            top: 200px;
            display: none;
            background-color: white;
            z-index: 3;
        }
        label{
            display: inline-block;
            width: 100px;
            height: 50px;
            text-align: right;
            line-height: 50px;
        }
        .wrap>div>input{
            width: 260px;
            height: 35px;
            outline: none;
            border: 1px solid #ccc;
            padding-left: 12px;
        }
        .wrap>div{
            margin-bottom: 5px;
        }
        .wrap>button{
            width: 40px;height: 40px;
            border-radius: 50%;
            position: absolute;
            right: -20px; top: -20px;
            border: 1px solid #ccc;
            background-color: white;
            font-size: 16px;
        }
        h3{
            width: 100%;
            padding: 15px 0;
            text-align: center;
        }
        .wrap>input{
            display: block;
            width: 200px;
            text-align: center;
            height: 35px; line-height: 35px;
            background-color: white;
            margin: 25px auto;
            border: 1px solid #ccc;
            font-weight: bold;
        }
        .wrap>div>input.hover{
            border: 1px solid rgb(33, 155, 254);
        }
        .bg{
            width: 100%;
            height: 100%;
            position: absolute;
            display: none;
            background-color: rgba(48, 47, 47,0.5);
            z-index: 2;
        }
    </style>
    <h1>点击弹出登录框</h1>
    <div class="wrap">
        <button>关闭</button>
        <h3>登陆会员</h3>
        <div>
            <label for="uname">用户名:</label>
            <input type="text" id="uname" class="ipt" placeholder="请输入用户名">
        </div>
        <div>
            <label for="pwd">登录密码:</label>
            <input type="password" id="pwd" class="ipt" placeholder="请输入登录密码">
        </div>
        <input type="submit" value="登陆会员">
    </div>
    <div class="bg"></div>

 JavaScript代码:

 var h1 = document.querySelector('h1');
 var wrap = document.querySelector('.wrap');
 var bg = document.querySelector('.bg');
    //点击弹出登录框
 h1.onclick = function() {
       wrap.style.display = 'block';
       bg.style.display = 'block';
   }
    //禁止登录框的选中文字
 wrap.addEventListener('selectstart',function(e){
       e.preventDefault();
   })
 var btn = wrap.children[0];
    //设置关闭按钮点击事件,关闭登录框
 btn.addEventListener('click',function() {
       wrap.style.display = 'none';
       bg.style.display = 'none';
   })
 var ipt = wrap.querySelectorAll('.ipt');
    //给表单添加获得焦点和失去焦点事件,改变边框颜色
 for(var i = 0; i < ipt.length; i++){
       ipt[i].onfocus = function() {
           this.className = 'ipt hover';
   }
       ipt[i].onblur = function() {
           this.className = 'ipt';
       }
   }
  var h3 = wrap.querySelector('h3');
     // 鼠标按下,获取此时鼠标在登录框内坐标
  h3.addEventListener('mousedown',function(e) { 
       h3.style.cursor = 'move';
       var x = e.pageX - wrap.offsetLeft;
       var y = e.pageY - wrap.offsetTop;
            
    // 鼠标移动,获取鼠标在页面移动时坐标,减去之前获取到的鼠标在框内坐标即登录框移动坐标
        document.addEventListener('mousemove',move); //
        function move(e) {
            wrap.style.left = e.pageX - x + 'px';
            wrap.style.top = e.pageY - y + 'px';
        }

       // 鼠标松开,清除鼠标移动事件
        document.addEventListener('mouseup',function() {
            document.removeEventListener('mousemove',move);
            h3.style.cursor = '';
        });
    })

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一段使用原生JavaScript实现拖拽模态框的示例代码: HTML代码: ``` <div class="modal"> <div class="modal-header"> <h3 class="modal-title">Modal Title</h3> <button class="modal-close">x</button> </div> <div class="modal-body"> <p>Modal Content</p> </div> </div> ``` CSS代码: ``` .modal { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); width: 400px; height: 300px; } .modal-header { display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f5f5f5; cursor: move; } .modal-title { margin: 0; } .modal-close { border: none; background-color: transparent; font-size: 20px; font-weight: bold; cursor: pointer; } .modal-body { padding: 10px; } ``` JavaScript代码: ``` var modal = document.querySelector('.modal'); var modalHeader = document.querySelector('.modal-header'); var isDragging = false; var offsetX = 0; var offsetY = 0; modalHeader.addEventListener('mousedown', function(e) { isDragging = true; offsetX = e.offsetX; offsetY = e.offsetY; }); document.addEventListener('mousemove', function(e) { if (isDragging) { modal.style.left = e.pageX - offsetX + 'px'; modal.style.top = e.pageY - offsetY + 'px'; } }); document.addEventListener('mouseup', function() { isDragging = false; }); var modalClose = document.querySelector('.modal-close'); modalClose.addEventListener('click', function() { modal.style.display = 'none'; }); ``` 解释: 1. 首先,我们选择模态框模态框头部,然后定义一个变量isDragging表示是否正在拖拽,以及变量offsetX和offsetY保存鼠标相对于模态框左上角的偏移量。 2. 当鼠标在模态框头部按下时,设置isDragging为true,并计算offsetX和offsetY。 3. 当鼠标在文档中移动时,如果isDragging为true,则根据鼠标位置和偏移量计算模态框的新位置,并将其应用到模态框上。 4. 当鼠标松开时,将isDragging设置为false。 5. 最后,选择模态框关闭按钮,为其添加一个点击事件,当点击时将模态框隐藏。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值