js實(shí)現(xiàn)響應(yīng)按鈕點(diǎn)擊彈出可拖拽的非模態(tài)對(duì)話框完整實(shí)例【測(cè)試可用】 原創(chuàng)
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">打開對(duì)話框</button> <div id="dialog" class="dialog"> ? <div class="dialog-header"> ? ? <span class="dlgtitle">對(duì)話框標(biāo)題</span> ? ? <span class="close-btn">×</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)松開時(shí)停止拖動(dòng)
// document.onmouseup = function() {
dialogHeaderMouseup = function() {
? isDragging = false;
}
// 點(diǎn)擊打開按鈕顯示對(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ì)話框頭部,開始拖動(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">打開對(duì)話框</button>
<div id="dialog" class="dialog">
<div class="dialog-header">
<span class="dlgtitle">對(duì)話框標(biāo)題</span>
<span class="close-btn">×</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)松開時(shí)停止拖動(dòng)
// document.onmouseup = function() {
dialogHeaderMouseup = function() {
isDragging = false;
}
// 點(diǎn)擊打開按鈕顯示對(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ì)話框頭部,開始拖動(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)行效果!
- JS 模態(tài)對(duì)話框和非模態(tài)對(duì)話框操作技巧匯總
- 利用javascript打開模態(tài)對(duì)話框(示例代碼)
- javascript showModalDialog模態(tài)對(duì)話框使用說(shuō)明
- JavaScript 實(shí)現(xiàn)模態(tài)對(duì)話框 源代碼大全
- 詳解AngularJS 模態(tài)對(duì)話框
- JS對(duì)話框_JS模態(tài)對(duì)話框showModalDialog用法總結(jié)
- 兩種WEB下的模態(tài)對(duì)話框 (asp.net或js的分別實(shí)現(xiàn))
- js模態(tài)對(duì)話框使用方法詳解
- js實(shí)現(xiàn)div模擬模態(tài)對(duì)話框展現(xiàn)URL內(nèi)容
- ModelDialog JavaScript模態(tài)對(duì)話框類代碼
- JavaScript實(shí)現(xiàn)模態(tài)對(duì)話框?qū)嵗?/a>
相關(guān)文章
JS實(shí)現(xiàn)深拷貝的幾種簡(jiǎn)單方法示例
深拷貝和淺拷貝是在JavaScript中復(fù)制對(duì)象或數(shù)組時(shí)經(jīng)常遇到的概念,下面這篇文章主要給大家介紹了關(guān)于JS實(shí)現(xiàn)深拷貝的幾種簡(jiǎn)單方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
JavaScript獲得當(dāng)前網(wǎng)頁(yè)來(lái)源頁(yè)面(即上一頁(yè))的方法
這篇文章主要介紹了JavaScript獲得當(dāng)前網(wǎng)頁(yè)來(lái)源頁(yè)面(即上一頁(yè))的方法,涉及javascript中document.referrer方法的使用技巧,需要的朋友可以參考下2015-04-04
在js里怎么實(shí)現(xiàn)Xcode里的callFuncN方法(詳解)
下面小編就為大家?guī)?lái)一篇在js里怎么實(shí)現(xiàn)Xcode里的callFuncN方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
JS獲取子窗口中返回的數(shù)據(jù)實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇JS獲取子窗口中返回的數(shù)據(jù)實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
js多個(gè)物體運(yùn)動(dòng)功能實(shí)例分析
這篇文章主要介紹了js多個(gè)物體運(yùn)動(dòng)功能,結(jié)合實(shí)例形式分析了js實(shí)現(xiàn)多物體運(yùn)動(dòng)功能的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12
HTML+CSS+JavaScript實(shí)現(xiàn)下拉菜單效果
這篇文章主要為大家詳細(xì)介紹了HTML+CSS+JavaScript實(shí)現(xiàn)下拉菜單效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
微信小程序 連續(xù)旋轉(zhuǎn)動(dòng)畫(this.animation.rotate)詳解
這篇文章主要介紹了微信小程序 連續(xù)旋轉(zhuǎn)動(dòng)畫(this.animation.rotate)詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

