JS Web API 拖拽对话框案例

    <style>
        .login-header {
            width: 100%;
            text-align: right;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
            margin-left: -100px;
        }
        ul, li, ol, dl, dt, dd, div, p, span, h1, h2, h3, h4, h5, h6, a {
            padding: 0px;
            margin: 0px;
        }
        .login {
            width: 512px;
            position: absolute;
            border: #ebebeb solid 1px;
            height: 280px;
            left: 50%;
            right: 50%;
            background: #ffffff;
            box-shadow: 0px 0px 20px #ddd;
            z-index: 9999;
            margin-left: -256px;
            margin-top: 140px;
            display: none;
        }
        .login-title {
            width: 100%;
            margin: 10px 0px 0px 0px;
            text-align: center;
            line-height: 40px;
            height: 40px;
            font-size: 18px;
            position: relative;
            cursor: move;
            -moz-user-select:none;/*火狐*/
            -webkit-user-select:none;/*webkit浏览器*/
            -ms-user-select:none;/*IE10*/
            -khtml-user-select:none;/*早期浏览器*/
            user-select:none;
        }
        .login-input-content {
            margin-top: 20px;
        }
        .login-button {
            width: 50%;
            margin: 30px auto 0px auto;
            line-height: 40px;
            font-size: 14px;
            border: #ebebeb 1px solid;
            text-align: center;
        }
        .login-bg {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0px;
            left: 0px;
            background: #000000;
            filter: alpha(opacity=30);
            -moz-opacity: 0.3;
            -khtml-opacity: 0.3;
            opacity: 0.3;
            display: none;
        }
        a {
            text-decoration: none;
            color: #000000;
        }
        .login-button a {
            display: block;
        }
        .login-input input.list-input {
            float: left;
            line-height: 35px;
            height: 35px;
            width: 350px;
            border: #ebebeb 1px solid;
            text-indent: 5px;
        }
        .login-input {
            overflow: hidden;
            margin: 0px 0px 20px 0px;
        }
        .login-input label {
            float: left;
            width: 90px;
            padding-right: 10px;
            text-align: right;
            line-height: 35px;
            height: 35px;
            font-size: 14px;
        }
        .login-title span {
            position: absolute;
            font-size: 12px;
            right: -20px;
            top: -30px;
            background: #ffffff;
            border: #ebebeb solid 1px;
            width: 40px;
            height: 40px;
            border-radius: 20px;
        }
    </style>
    <script src="common.js"></script>
</head>
<body>
<div class="login-header"><a id="link" href="javascript:void(0);">会员登录</a></div>
<div id="login" class="login" >
    <div id="title" class="login-title">会员登录
        <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
    </div>
    <div class="login-input-content">
        <div class="login-input">
            <label>账号:</label>
            <input type="text" placeholder="请输入账号" name="info[username]" id="username" class="list-input">
        </div>
        <div class="login-input">
            <label>密码:</label>
            <input type="password" placeholder="请输入密码" name="info[password]" id="password" class="list-input">
        </div>
    </div>
    <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录</a></div>
</div>
<!-- 遮盖层 -->
<div id="bg" class="login-bg" ></div>
<script>
    // 1、点击超链接显示遮挡层登录框,在页面任何位置都可以关闭登录框
    //获取link源事件
    var link=document.getElementById(‘link‘);
    //获取登录框
    var login=document.getElementById(‘login‘);
    var bg=document.getElementById(‘bg‘);
    var closeBtn=document.getElementById(‘closeBtn‘);
    //link绑定鼠标点击事件
    link.onclick=function (e) {
        //设置login登录框的显示样式-----元素(样式)属性值:对象名.style.属性名=“属性值”
        login.style.display=‘block‘;
        //设置遮挡层背景显示
        bg.style.display=‘block‘;
        console.log(‘出来 了‘)
        e.stopPropagation();
    }
    //页面任何位置都可以关闭登录框
    closeBtn.onclick=function () {
        login.style.display=‘none‘;
        console.log("隐藏了")
        bg.style.display=‘none‘;
    }
    //按下鼠标,移动鼠标,移动登录框
    var title=document.getElementById(‘title‘);
    title.onmousedown=function (e) {
        //鼠标在盒子中的X横坐标=获取此时可视区域的横坐标-此时登录框距离左侧页面的横坐标
        var spaceX=e.clientX-login.offsetLeft;
        //鼠标在盒子中的Y纵坐标=获取此时可视区域的纵坐标-此时登录框距离左侧页面的纵坐标
        var spaceY=e.clientY-login.offsetTop;
        //移动的事件
        document.onmousemove=function (e) {
            //盒子的横坐标X=新的可视区域的横坐标-spaceX   ====>新的值----> 给登录框的left属性,
            var leftX=e.clientX - spaceX+250;
            //盒子的纵坐标Y=新的可视区域的纵坐标-spaceY  ====>新的值----> 设置登录框的left属性,
            var topY=e.clientY - spaceY-140;
            login.style.left=leftX  +‘px‘;
            login.style.top=topY +‘px‘;
        }
        //事件移出
        title.onmouseup=function () {
            document.onmousemove=null;
        }
    }
</script>

思路:
1、当鼠标按下时,求出鼠标在盒子中的位置;
鼠标在盒子中的位置 = 鼠标在页面中的位置 - 盒子在页面中的位置

2、当鼠标移动时,保持鼠标在盒子中的位置不变(即盒子跟随鼠标移动)
盒子的坐标 = 鼠标当前在页面中的位置 - 鼠标在盒子中的位置

使用到的属性有:offsetLeft、offsetTop、pageX、pageY
注意:盒子要脱离文档流,即设置 position:absolute

====================问题解决l====================
问题:鼠标弹起了,盒子还黏在鼠标上。
解决:当鼠标弹起时,移除鼠标移动事件。
关闭按钮:当鼠标点击关闭按钮时,隐藏盒子
注意:使用getPage(e)解决pageX和pageY的浏览器兼容性问题

原文地址:https://www.cnblogs.com/liout/p/10992440.html

时间: 2024-08-07 10:45:31

JS Web API 拖拽对话框案例的相关文章

js实现鼠标拖拽div-------Day44

如果去问这样一个问题"你觉得鼠标操作简单,还是键盘操作简单",相信会有多数人都会回答鼠标吧,毕竟键盘按钮那么多,如果手小了或者手法不规范了,太容易出问题了,也对操作的速度和有效性是大大的降低了,当然不可否认,会有那么一批人,操作起键盘来完全可以忽略鼠标的,但这些都应该是骨灰级别的了吧,起码我现在的层次还接触不到,只能向往. 然而,当面对这么一个问题时,我突然有点纠结,因为毕竟以前我也是那么想的,但是这次开发让我是大跌眼镜,键盘操作我在开发中,我只能判定其按键是否按键,然后根据不同按键进

JS Event 鼠标拖拽事件

<!DOCTYPE html><html> <head>        <meta charset="UTF-8">        <title>JS Event鼠标拖拽事件</title>                <style>            #box{width:200px;height:200px;background:#000;position:absolute;}       

HTML5 File API — 拖拽显示

1.HTML5拖拽 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 如果是html中的元素,要进行拖动,需要设置draggable为true. 下面的代码显示了img在两个div里任意拖动. 1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <style type="text/css"> 5 #div1, #div2 6 {float:left; width:100px; height:35px;

JS打造的拖拽翻页效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

js实现鼠标拖拽多选功能

最近做了一个用js实现鼠标拖拽多选的功能,于是整理了一下思路,写了一个小demo:遮罩出现:被遮罩盖住的,即为选中的块(背景色为粉色)下面是具体代码,注释已在文中,与大家交流. <!DOCTYPE html> <html> <head> <title>鼠标拖拽多选功能</title> <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">

JS实现拖拽小案例

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>简单的拖拽</title> <link rel="stylesheet" href="../toolkit/reset.min.css"> <style> #box{ height: 200px

vuejs2.0使用Sortable.js实现的拖拽功能( 转)

文章目录 简介 实现效果 html主要代码 css代码 js代码 简介 在使用vue1.x之前的版本的时候,页面中的拖拽功能,我在项目中是直接用的jquery ui中的sortable.js,只是在拖拽完成后,在update的回调函数中又重新排序了存放数据的数组.但是当把vue升级到2.0以上后发现拖拽功能失效了,于是使用了下面代码. 该案例主要是在用于vuejs2.0中实现的拖拽功能,用到的的js有Sortable.js,vuedraggable.js,当然还有vue.min.js,提供的案例

js实现可拖拽的div

实现一个div可以被拖拽,代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>zzw_drap</title> <style> * { margin: 0; padding: 0; } #box { position: absolute; top: 100px; left: 200

js可以随意拖拽的div的实现

最近花了点时间用纯JS写了一个扫雷程序,写出来效果很差,自己长时间使用面向过程的方式编写代码,写的程序到后面都乱了,有必要找时间好好的深入理解一下OOP了.有时间会把写的东西拿上来.就当是留个纪念吧.打算用OOP重新写个扫雷程序,希望令自己满意. ——————————————碎碎念 记录一个实现随意拖拽div的实现方法,当作备忘吧,指不定什么时候用到了呢. <!DOCTYPE html> <html lang="en"> <head> <met