您好,登錄后才能下訂單哦!
今天小編給大家分享一下js怎么實現響應按鈕點擊彈出可拖拽的非模態對話框的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
.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; }
<button id="openBtn">打開對話框</button> <div id="dialog" class="dialog"> <div class="dialog-header"> <span class="dlgtitle">對話框標題</span> <span class="close-btn">×</span> </div> <div class="dialog-content"> <p>這里是對話框內容</p> </div> </div>
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">×</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>
以上就是“js怎么實現響應按鈕點擊彈出可拖拽的非模態對話框”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。