part:
.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; }
part:
<button >Open the dialog box</button> <div class="dialog"> <div class="dialog-header"> <span class="dlgtitle">Dialog Title</span> <span class="close-btn">&times;</span> </div> <div class="dialog-content"> <p>Here is the dialog box content</p> </div> </div>
part:
var dialog = ('dialog'); var openBtn = ('openBtn'); var closeBtn = ('.close-btn'); var isDragging = false; var mouseX, mouseY, dialogLeft, dialogTop; // Record the mouse position and dialog box position when the mouse is presseddialogHeaderMouseDown = function(e) { isDragging = true; mouseX = ; mouseY = ; dialogLeft = ; dialogTop = ; } // Move the dialog box when the mouse is moved// = function(e) { dialogHeaderMouseMove = function(e) { if (isDragging) { var moveX = - mouseX; var moveY = - mouseY; = dialogLeft + moveX + 'px'; = dialogTop + moveY + 'px'; } } // Stop dragging when the mouse is released// = function() { dialogHeaderMouseup = function() { isDragging = false; } // Click the Open button to display the dialog box = function() { = 'block'; } // Click the Close button or close the dialog box outside the dialog box = function() { = 'none'; } = function(e) { if ( == dialog) { = 'none'; } } // Press the head of the dialog box with the mouse and start dragging the dialog boxvar header = ('.dialog-header'); ('mousedown', dialogHeaderMouseDown); ('mousemove', dialogHeaderMouseMove); ('mouseup', dialogHeaderMouseup);
You can use the online tools of this site:http://tools./code/WebCodeRunTest the above code running effect.
Attachment: Complete sample code:
<!DOCTYPE html> <html lang="zh_CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Drag-and-drop non-modal dialog box</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 >Open the dialog box</button> <div class="dialog"> <div class="dialog-header"> <span class="dlgtitle">Dialog Title</span> <span class="close-btn">&times;</span> </div> <div class="dialog-content"> <p>Here is the dialog box content</p> </div> </div> <script> var dialog = ('dialog'); var openBtn = ('openBtn'); var closeBtn = ('.close-btn'); var isDragging = false; var mouseX, mouseY, dialogLeft, dialogTop; // Record the mouse position and dialog box position when the mouse is presseddialogHeaderMouseDown = function(e) { isDragging = true; mouseX = ; mouseY = ; dialogLeft = ; dialogTop = ; } // Move the dialog box when the mouse is moved// = function(e) { dialogHeaderMouseMove = function(e) { if (isDragging) { var moveX = - mouseX; var moveY = - mouseY; = dialogLeft + moveX + 'px'; = dialogTop + moveY + 'px'; } } // Stop dragging when the mouse is released// = function() { dialogHeaderMouseup = function() { isDragging = false; } // Click the Open button to display the dialog box = function() { = 'block'; } // Click the Close button or close the dialog box outside the dialog box = function() { = 'none'; } = function(e) { if ( == dialog) { = 'none'; } } // Press the head of the dialog box with the mouse and start dragging the dialog boxvar header = ('.dialog-header'); ('mousedown', dialogHeaderMouseDown); ('mousemove', dialogHeaderMouseMove); ('mouseup', dialogHeaderMouseup); </script> </body> </html>
You can also use the online tools of this site:http://tools./code/HtmlJsRunTest the above code running effect!