返回顶部
首页 > 资讯 > 前端开发 > JavaScript >js实现响应按钮点击弹出可拖拽的非模态对话框完整实例【测试可用】 原创
  • 363
分享到

js实现响应按钮点击弹出可拖拽的非模态对话框完整实例【测试可用】 原创

js响应按钮点击弹出可拖拽非模态对话框 2023-05-17 11:05:49 363人浏览 八月长安
摘要

1.CSS部分: .dialog {   display: none;   position: absolute;   left: 50%;   top: 50%;   transf

1.CSS部分:

.dialog {
  display: none;
  position: absolute;
  left: 50%;
  top: 50%;
  transfORM: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, .3);
  z-index: 9999;
}

.dialog-header {
  background-color: #f6f7f8;
  padding: 10px;
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
  cursor: move;
}

.dlgtitle {
  font-weight: bold;
  font-size: 16px;
}

.close-btn {
  float: right;
  cursor: pointer;
}

.dialog-content {
  padding: 20px;
}

2.html部分:

<button id="openBtn">打开对话框</button>

<div id="dialog" class="dialog">
  <div class="dialog-header">
    <span class="dlgtitle">对话框标题</span>
    <span class="close-btn">&times;</span>
  </div>
  <div class="dialog-content">
    <p>这里是对话框内容</p>
  </div>
</div>

3.javascript部分:

var dialog = document.getElementById('dialog');
var openBtn = document.getElementById('openBtn');
var closeBtn = document.querySelector('.close-btn');
var isDragging = false;
var mouseX, mouseY, dialogLeft, dialogTop;

// 鼠标按下时记录鼠标位置以及对话框位置
dialogHeaderMouseDown = function(e) {
  isDragging = true;
  mouseX = e.clientX;
  mouseY = e.clientY;
  dialogLeft = dialog.offsetLeft;
  dialogTop = dialog.offsetTop;
}

// 鼠标移动时移动对话框
// document.onmousemove = function(e) {
dialogHeaderMouseMove = function(e) {
  if (isDragging) {
    var moveX = e.clientX - mouseX;
    var moveY = e.clientY - mouseY;
    dialog.style.left = dialogLeft + moveX + 'px';
    dialog.style.top = dialogTop + moveY + 'px';
  }
}

// 鼠标松开时停止拖动
// document.onmouseup = function() {
dialogHeaderMouseup = function() {
  isDragging = false;
}

// 点击打开按钮显示对话框
openBtn.onclick = function() {
  dialog.style.display = 'block';
}

// 点击关闭按钮或对话框外部关闭对话框
closeBtn.onclick = function() {
  dialog.style.display = 'none';
}

dialog.onclick = function(e) {
  if (e.target == dialog) {
    dialog.style.display = 'none';
  }
}

// 鼠标按下对话框头部,开始拖动对话框
var header = document.querySelector('.dialog-header');
header.addEventListener('mousedown', dialogHeaderMouseDown);
header.addEventListener('mousemove', dialogHeaderMouseMove);
header.addEventListener('mouseup', dialogHeaderMouseup);

可以使用本站在线工具Http://tools.jb51.net/code/WEBCodeRun 测试上述代码运行效果。

附:完整示例代码:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>可拖拽非模态对话框</title>
<style>
.dialog {
  display: none;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, .3);
  z-index: 9999;
}

.dialog-header {
  background-color: #f6f7f8;
  padding: 10px;
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
  cursor: move;
}

.dlgtitle {
  font-weight: bold;
  font-size: 16px;
}

.close-btn {
  float: right;
  cursor: pointer;
}

.dialog-content {
  padding: 20px;
}
</style>
</head>
<body>
<button id="openBtn">打开对话框</button>
<div id="dialog" class="dialog">
  <div class="dialog-header">
    <span class="dlgtitle">对话框标题</span>
    <span class="close-btn">&times;</span>
  </div>
  <div class="dialog-content">
    <p>这里是对话框内容</p>
  </div>
</div>
<script>
var dialog = document.getElementById('dialog');
var openBtn = document.getElementById('openBtn');
var closeBtn = document.querySelector('.close-btn');
var isDragging = false;
var mouseX, mouseY, dialogLeft, dialogTop;

// 鼠标按下时记录鼠标位置以及对话框位置
dialogHeaderMouseDown = function(e) {
  isDragging = true;
  mouseX = e.clientX;
  mouseY = e.clientY;
  dialogLeft = dialog.offsetLeft;
  dialogTop = dialog.offsetTop;
}

// 鼠标移动时移动对话框
// document.onmousemove = function(e) {
dialogHeaderMouseMove = function(e) {
  if (isDragging) {
    var moveX = e.clientX - mouseX;
    var moveY = e.clientY - mouseY;
    dialog.style.left = dialogLeft + moveX + 'px';
    dialog.style.top = dialogTop + moveY + 'px';
  }
}

// 鼠标松开时停止拖动
// document.onmouseup = function() {
dialogHeaderMouseup = function() {
  isDragging = false;
}

// 点击打开按钮显示对话框
openBtn.onclick = function() {
  dialog.style.display = 'block';
}

// 点击关闭按钮或对话框外部关闭对话框
closeBtn.onclick = function() {
  dialog.style.display = 'none';
}

dialog.onclick = function(e) {
  if (e.target == dialog) {
    dialog.style.display = 'none';
  }
}

// 鼠标按下对话框头部,开始拖动对话框
var header = document.querySelector('.dialog-header');
header.addEventListener('mousedown', dialogHeaderMouseDown);
header.addEventListener('mousemove', dialogHeaderMouseMove);
header.addEventListener('mouseup', dialogHeaderMouseup);
</script>
</body>
</html>

还可以使用本站在线工具:http://tools.jb51.net/code/HtmljsRun 测试上述代码运行效果!

--结束END--

本文标题: js实现响应按钮点击弹出可拖拽的非模态对话框完整实例【测试可用】 原创

本文链接: https://lsjlt.com/news/211046.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作