亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

js實(shí)現(xiàn)響應(yīng)按鈕點(diǎn)擊彈出可拖拽的非模態(tài)對(duì)話框完整實(shí)例【測(cè)試可用】 原創(chuàng)

原創(chuàng)  更新時(shí)間:2023年04月23日 18:44:00   原創(chuàng) 投稿:shichen2014  
這篇文章主要介紹了js實(shí)現(xiàn)響應(yīng)按鈕點(diǎn)擊彈出可拖拽的非模態(tài)對(duì)話框,結(jié)合完整實(shí)例形式分析了原生JavaScript實(shí)現(xiàn)的可拖拽非模態(tài)對(duì)話框?qū)崿F(xiàn)技巧與使用方法,需要的朋友可以參考下

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">打開(kāi)對(duì)話框</button>

<div id="dialog" class="dialog">
? <div class="dialog-header">
? ? <span class="dlgtitle">對(duì)話框標(biāo)題</span>
? ? <span class="close-btn">&times;</span>
? </div>
? <div class="dialog-content">
? ? <p>這里是對(duì)話框內(nèi)容</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;

// 鼠標(biāo)按下時(shí)記錄鼠標(biāo)位置以及對(duì)話框位置
dialogHeaderMouseDown = function(e) {
? isDragging = true;
? mouseX = e.clientX;
? mouseY = e.clientY;
? dialogLeft = dialog.offsetLeft;
? dialogTop = dialog.offsetTop;
}

// 鼠標(biāo)移動(dòng)時(shí)移動(dòng)對(duì)話框
// 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';
? }
}

// 鼠標(biāo)松開(kāi)時(shí)停止拖動(dòng)
// document.onmouseup = function() {
dialogHeaderMouseup = function() {
? isDragging = false;
}

// 點(diǎn)擊打開(kāi)按鈕顯示對(duì)話框
openBtn.onclick = function() {
? dialog.style.display = 'block';
}

// 點(diǎn)擊關(guān)閉按鈕或?qū)υ捒蛲獠筷P(guān)閉對(duì)話框
closeBtn.onclick = function() {
? dialog.style.display = 'none';
}

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

// 鼠標(biāo)按下對(duì)話框頭部,開(kāi)始拖動(dòng)對(duì)話框
var header = document.querySelector('.dialog-header');
header.addEventListener('mousedown', dialogHeaderMouseDown);
header.addEventListener('mousemove', dialogHeaderMouseMove);
header.addEventListener('mouseup', dialogHeaderMouseup);

可以使用本站在線工具:http://tools.jb51.net/code/WebCodeRun 測(cè)試上述代碼運(yùn)行效果。

附:完整示例代碼:

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>可拖拽非模態(tài)對(duì)話框</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">打開(kāi)對(duì)話框</button>
<div id="dialog" class="dialog">
  <div class="dialog-header">
    <span class="dlgtitle">對(duì)話框標(biāo)題</span>
    <span class="close-btn">&times;</span>
  </div>
  <div class="dialog-content">
    <p>這里是對(duì)話框內(nèi)容</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;

// 鼠標(biāo)按下時(shí)記錄鼠標(biāo)位置以及對(duì)話框位置
dialogHeaderMouseDown = function(e) {
  isDragging = true;
  mouseX = e.clientX;
  mouseY = e.clientY;
  dialogLeft = dialog.offsetLeft;
  dialogTop = dialog.offsetTop;
}

// 鼠標(biāo)移動(dòng)時(shí)移動(dòng)對(duì)話框
// 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';
  }
}

// 鼠標(biāo)松開(kāi)時(shí)停止拖動(dòng)
// document.onmouseup = function() {
dialogHeaderMouseup = function() {
  isDragging = false;
}

// 點(diǎn)擊打開(kāi)按鈕顯示對(duì)話框
openBtn.onclick = function() {
  dialog.style.display = 'block';
}

// 點(diǎn)擊關(guān)閉按鈕或?qū)υ捒蛲獠筷P(guān)閉對(duì)話框
closeBtn.onclick = function() {
  dialog.style.display = 'none';
}

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

// 鼠標(biāo)按下對(duì)話框頭部,開(kāi)始拖動(dòng)對(duì)話框
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 測(cè)試上述代碼運(yùn)行效果!

相關(guān)文章

最新評(píng)論