记录:拖拽对话框

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title></title>
  <style>
    .login-header {
      width: 100%;
      text-align: center;
      height: 30px;
      font-size: 24px;
      line-height: 30px;
    }

    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: -250px;
      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>
</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 src="common.js"></script>

<script>

  //获取超链接,注册点击事件,显示登录框和遮挡层
  my$("link").onclick = function () {
    my$("login").style.display = "block";
    my$("bg").style.display = "block";
  };

  //获取关闭,注册点击事件,隐藏登录框和遮挡层
  my$("closeBtn").onclick = function () {
    my$("login").style.display = "none";
    my$("bg").style.display = "none";
  };

  //按下鼠标,移动鼠标,移动登录框
  my$("title").onmousedown = function (e) {
    //获取此时的可视区域的横坐标-此时登录框距离左侧页面的横坐标
    var spaceX = e.clientX - my$("login").offsetLeft;
    var spaceY = e.clientY - my$("login").offsetTop;
    //移动的事件
    document.onmousemove = function (e) {
      //新的可视区域的横坐标-spaceX====>新的值--->登录框的left属性
      var x = e.clientX - spaceX+250;
      var y = e.clientY - spaceY-140;
      my$("login").style.left = x + "px";
      my$("login").style.top = y + "px";
    }
  };

  document.οnmοuseup=function () {
    document.οnmοusemοve=null;//当鼠标抬起的时候,把鼠标移动事件干掉
  };

</script>
</body>
</html>

转载于:https://my.oschina.net/u/3848851/blog/1814807

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的拖拽对话框案例的实现步骤: 1. HTML 结构 首先我们需要在 HTML 中创建一个对话框的结构,包括标题栏、内容区和关闭按钮,如下所示: ``` <div class="dialog"> <div class="title">对话框标题</div> <div class="content">对话框内容</div> <div class="close-btn">X</div> </div> ``` 2. CSS 样式 接着我们需要为对话框添加一些样式,包括设置对话框的位置、大小、边框、背景色等,以及设置标题栏和关闭按钮的样式,如下所示: ``` .dialog { position: absolute; top: 50%; left: 50%; width: 400px; height: 300px; margin-top: -150px; margin-left: -200px; border: 1px solid #ccc; background-color: #fff; } .title { height: 30px; line-height: 30px; padding: 0 10px; background-color: #eee; cursor: move; } .close-btn { position: absolute; top: 5px; right: 5px; width: 20px; height: 20px; line-height: 20px; text-align: center; border-radius: 50%; background-color: #ccc; cursor: pointer; } ``` 其中,我们通过设置对话框的 `position` 属性为 `absolute`,然后再通过设置 `top`、`left`、`margin-top` 和 `margin-left` 属性将对话框居中显示。同时,我们还为标题栏设置了一个 `cursor` 属性为 `move`,以便用户可以通过拖拽标题栏来移动整个对话框。 3. JS 代码 最后,我们需要编写一些 JS 代码来实现对话框拖拽功能。具体步骤如下: 首先,我们需要获取对话框、标题栏和关闭按钮的 DOM 元素: ``` var dialog = document.querySelector('.dialog'); var title = dialog.querySelector('.title'); var closeBtn = dialog.querySelector('.close-btn'); ``` 然后,我们需要定义一个变量 `dragging` 来表示当前是否正在拖拽对话框: ``` var dragging = false; ``` 接着,我们需要为标题栏添加 `mousedown` 事件监听器,用来开始拖拽对话框: ``` title.addEventListener('mousedown', function(e) { dragging = true; // 记录鼠标相对于对话框左上角的偏移量 var offsetX = e.clientX - dialog.offsetLeft; var offsetY = e.clientY - dialog.offsetTop; // 添加鼠标移动事件监听器 document.addEventListener('mousemove', function(e) { if (dragging) { // 计算对话框的新位置 var newLeft = e.clientX - offsetX; var newTop = e.clientY - offsetY; // 设置对话框的新位置 dialog.style.left = newLeft + 'px'; dialog.style.top = newTop + 'px'; } }); }); ``` 在 `mousedown` 事件监听器中,我们将 `dragging` 设置为 `true`,表示开始拖拽对话框。然后,我们记录鼠标相对于对话框左上角的偏移量,以便在后面计算对话框的新位置。接着,我们为 `document` 添加 `mousemove` 事件监听器,用来实时更新对话框的位置。 最后,我们需要为关闭按钮添加 `click` 事件监听器,用来关闭对话框: ``` closeBtn.addEventListener('click', function() { dialog.style.display = 'none'; }); ``` 完整的 JS 代码如下: ``` var dialog = document.querySelector('.dialog'); var title = dialog.querySelector('.title'); var closeBtn = dialog.querySelector('.close-btn'); var dragging = false; title.addEventListener('mousedown', function(e) { dragging = true; var offsetX = e.clientX - dialog.offsetLeft; var offsetY = e.clientY - dialog.offsetTop; document.addEventListener('mousemove', function(e) { if (dragging) { var newLeft = e.clientX - offsetX; var newTop = e.clientY - offsetY; dialog.style.left = newLeft + 'px'; dialog.style.top = newTop + 'px'; } }); }); document.addEventListener('mouseup', function() { dragging = false; }); closeBtn.addEventListener('click', function() { dialog.style.display = 'none'; }); ``` 这样,我们就实现了一个简单的拖拽对话框案例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值