The user wants me to create a draggable modal window with a blurred background effect. Let me break down the requirements:
- Modal window that can be dragged around the screen
- Blurred background effect (the content behind the modal should be blurred)
- Use HTML, CSS, and JavaScript
Let me create a comprehensive implementation:
Features I'll include:
- Modal with header that can be used to drag it
- Blurred backdrop behind the modal
- Close button
- Smooth animations
- Responsive design
- Multiple modals support potentially
- JavaScript drag functionality with mouse events
Let me write the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draggable Modal with Blur Effect</title>
<style>
/* CSS styles */
</style>
</head>
<body>
<!-- Content -->
<!-- Modal -->
<script>
// JavaScript
</script>
</body>
</html>
Let me flesh this out completely: # Draggable Modal Window with Blurred Background
Here's a complete implementation with smooth animations and touch support:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draggable Modal with Blur Effect</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
transition: filter 0.3s ease;
}
/* Main content that will be blurred */
.main-content {
padding: 40px;
transition: filter 0.3s ease, transform 0.3s ease;
}
.main-content.blurred {
filter: blur(8px);
pointer-events: none;
user-select: none;
}
.main-content h1 {
color: white;
text-align: center;
margin-bottom: 30px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.cards-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 20px;
max-width: 1200px;
margin: 0 auto;
}
.card {
background: rgba(255, 255, 255, 0.9);
border-radius: 15px;
padding: 25px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
.card h3 {
color: #333;
margin-bottom: 10px;
}
.card p {
color: #666;
line-height: 1.6;
}
/* Trigger buttons */
.btn-container {
text-align: center;
margin-top: 30px;
}
.btn-open-modal {
padding: 15px 40px;
font-size: 18px;
background: white;
color: #764ba2;
border: none;
border-radius: 50px;
cursor: pointer;
box-shadow: 0 5px 20px rgba(0,0,0,0.2);
transition: all 0.3s ease;
margin: 10px;
}
.btn-open-modal:hover {
transform: translateY(-3px);
box-shadow: 0 8px 30px rgba(0,0,0,0.3);
}
/* Modal Overlay */
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
display: flex;
justify-content: center;
align-items: center;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
z-index: 1000;
}
.modal-overlay.active {
opacity: 1;
visibility: visible;
}
/* Modal Window */
.modal {
position: absolute;
background: white;
border-radius: 15px;
box-shadow: 0 25px 80px rgba(0, 0, 0, 0.4);
min-width: 350px;
max-width: 90vw;
transform: scale(0.8);
transition: transform 0.3s ease;
overflow: hidden;
}
.modal-overlay.active .modal {
transform: scale(1);
}
/* Modal Header - Draggable Area */
.modal-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px 25px;
cursor: move;
display: flex;
justify-content: space-between;
align-items: center;
user-select: none;
}
.modal-header h2 {
font-size: 20px;
font-weight: 600;
display: flex;
align-items: center;
gap: 10px;
}
.modal-header h2::before {
content: '⋮⋮';
font-size: 14px;
opacity: 0.7;
}
.modal-close {
background: rgba(255, 255, 255, 0.2);
border: none;
color: white;
width: 35px;
height: 35px;
border-radius: 50%;
cursor: pointer;
font-size: 20px;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.3s ease;
}
.modal-close:hover {
background: rgba(255, 255, 255, 0.3);
transform: rotate(90deg);
}
/* Modal Body */
.modal-body {
padding: 25px;
max-height: 60vh;
overflow-y: auto;
}
.modal-body p {
color: #555;
line-height: 1.8;
margin-bottom: 15px;
}
.modal-body .form-group {
margin-bottom: 20px;
}
.modal-body label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: 500;
}
.modal-body input,
.modal-body textarea {
width: 100%;
padding: 12px 15px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 14px;
transition: border-color 0.3s ease;
}
.modal-body input:focus,
.modal-body textarea:focus {
outline: none;
border-color: #667eea;
}
/* Modal Footer */
.modal-footer {
padding: 20px 25px;
background: #f8f9fa;
display: flex;
justify-content: flex-end;
gap: 10px;
}
.modal-footer button {
padding: 12px 25px;
border-radius: 8px;
font-size: 14px;
cursor: pointer;
transition: all 0.3s ease;
}
.btn-cancel {
background: #e0e0e0;
border: none;
color: #555;
}
.btn-cancel:hover {
background: #d0d0d0;
}
.btn-submit {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border: none;
color: white;
}
.btn-submit:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
/* Dragging state */
.modal.dragging {
cursor: move;
box-shadow: 0 35px 100px rgba(0, 0, 0, 0.5);
transition: box-shadow 0.3s ease;
}
/* Position indicator */
.position-indicator {
position: fixed;
bottom: 20px;
right: 20px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 10px 15px;
border-radius: 8px;
font-size: 12px;
font-family: monospace;
opacity: 0;
transition: opacity 0.3s ease;
z-index: 1001;
}
.position-indicator.visible {
opacity: 1;
}
/* Resize handle */
.modal-resize {
position: absolute;
bottom: 0;
right: 0;
width: 20px;
height: 20px;
cursor: nwse-resize;
background: linear-gradient(135deg, transparent 50%, #667eea 50%);
border-bottom-right-radius: 15px;
}
/* Custom scrollbar */
.modal-body::-webkit-scrollbar {
width: 8px;
}
.modal-body::-webkit-scrollbar-track {
background: #f1f1f1;
}
.modal-body::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
}
.modal-body::-webkit-scrollbar-thumb:hover {
background: #a1a1a1;
}
/* Animation for multiple modals */
.modal:nth-child(2) {
z-index: 1002;
}
/* Responsive */
@media (max-width: 480px) {
.modal {
min-width: 90vw;
}
.modal-header {
padding: 15px 20px;
}
.modal-body {
padding: 20px;
}
}
</style>
</head>
<body>
<!-- Main Content -->
<div class="main-content" id="mainContent">
<h1>🎨 Draggable Modal Demo</h1>
<div class="cards-container">
<div class="card">
<h3>📱 Responsive Design</h3>
<p>This modal works great on all devices and screen sizes.</p>
</div>
<div class="card">
<h3>🖱️ Drag & Drop</h3>
<p>Click and drag the modal header to move it anywhere on screen.</p>
</div>
<div class="card">
<h3>✨ Smooth Animations</h3>
<p>Beautiful transitions and blur effects for a modern look.</p>
</div>
<div class="card">
<h3>📐 Boundaries</h3>
<p>The modal stays within viewport boundaries while dragging.</p>
</div>
</div>
<div class="btn-container">
<button class="btn-open-modal" onclick="openModal('modal1')">
📝 Open Form Modal
</button>
<button class="btn-open-modal" onclick="openModal('modal2')">
ℹ️ Open Info Modal
</button>
</div>
</div>
<!-- Modal 1 - Form -->
<div class="modal-overlay" id="modal1">
<div class="modal">
<div class="modal-header">
<h2>Contact Form</h2>
<button class="modal-close" onclick="closeModal('modal1')">×</button>
</div>
<div class="modal-body">
<p>Drag the header to move this modal around! Fill out the form below:</p>
<div class="form-group">
<label for="name">Full Name</label>
<input type="text" id="name" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" id="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea id="message" rows="4" placeholder="Your message..."></textarea>
</div>
</div>
<div class="modal-footer">
<button class="btn-cancel" onclick="closeModal('modal1')">Cancel</button>
<button class="btn-submit">Submit</button>
</div>
<div class="modal-resize"></div>
</div>
</div>
<!-- Modal 2 - Info -->
<div class="modal-overlay" id="modal2">
<div class="modal">
<div class="modal-header">
<h2>Information</h2>
<button class="modal-close" onclick="closeModal('modal2')">×</button>
</div>
<div class="modal-body">
<p><strong>✨ Features of this modal:</strong></p>
<p>🔹 <strong>Draggable</strong> - Move it by dragging the header</p>
<p>🔹 <strong>Blur Effect</strong> - Background content is blurred</p>
<p>🔹 <strong>Boundary Detection</strong> - Stays within the viewport</p>
<p>🔹 <strong>Touch Support</strong> - Works on mobile devices</p>
<p>🔹 <strong>Keyboard Accessible</strong> - Press ESC to close</p>
<p>🔹 <strong>Multiple Modals</strong> - Support for stacking</p>
</div>
<div class="modal-footer">
<button class="btn-submit" onclick="closeModal('modal2')">Got it!</button>
</div>
<div class="modal-resize"></div>
</div>
</div>
<!-- Position Indicator -->
<div class="position-indicator" id="posIndicator">X: 0, Y: 0</div>
<script>
// Modal Draggable Implementation
class DraggableModal {
constructor(modalOverlay) {
this.overlay = modalOverlay;
this.modal = modalOverlay.querySelector('.modal');
this.header = modalOverlay.querySelector('.modal-header');
this.resizeHandle = modalOverlay.querySelector('.modal-resize');
this.isDragging = false;
this.isResizing = false;
this.startX = 0;
this.startY = 0;
this.startLeft = 0;
this.startTop = 0;
this.startWidth = 0;
this.startHeight = 0;
this.init();
}
init() {
// Mouse events for dragging
this.header.addEventListener('mousedown', (e) => this.startDrag(e));
document.addEventListener('mousemove', (e) => this.drag(e));
document.addEventListener('mouseup', () => this.stopDrag());
// Touch events for mobile
this.header.addEventListener('touchstart', (e) => this.startDrag(e), { passive: false });
document.addEventListener('touchmove', (e) => this.drag(e), { passive: false });
document.addEventListener('touchend', () => this.stopDrag());
// Resize events
if (this.resizeHandle) {
this.resizeHandle.addEventListener('mousedown', (e) => this.startResize(e));
document.addEventListener('mousemove', (e) => this.resize(e));
document.addEventListener('mouseup', () => this.stopResize());
}
// Prevent text selection while dragging
this.header.addEventListener('selectstart', (e) => e.preventDefault());
}
startDrag(e) {
if (e.target.closest('.modal-close')) return;
this.isDragging = true;
this.modal.classList.add('dragging');
const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
const clientY = e.type.includes('touch') ? e.touches[0].clientY : e.clientY;
const rect = this.modal.getBoundingClientRect();
this.startX = clientX;
this.startY = clientY;
this.startLeft = rect.left;
this.startTop = rect.top;
// Set initial position if not already positioned
if (!this.modal.style.left) {
this.modal.style.left = rect.left + 'px';
this.modal.style.top = rect.top + 'px';
this.modal.style.transform = 'none';
}
if (e.type.includes('touch')) {
e.preventDefault();
}
}
drag(e) {
if (!this.isDragging) return;
const clientX = e.type.includes('touch') ? e.touches[0].clientX : e.clientX;
const clientY = e.type.includes('touch') ? e.touches[0].clientY : e.clientY;
const deltaX = clientX - this.startX;
const deltaY = clientY - this.startY;
let newLeft = this.startLeft + deltaX;
let newTop = this.startTop + deltaY;
// Boundary detection
const modalRect = this.modal.getBoundingClientRect();
const maxLeft = window.innerWidth - modalRect.width;
const maxTop = window.innerHeight - modalRect.height;
newLeft = Math.max(0, Math.min(newLeft, maxLeft));
newTop = Math.max(0, Math.min(newTop, maxTop));
this.modal.style.left = newLeft + 'px';
this.modal.style.top = newTop + 'px';
this.modal.style.transform = 'none';
// Update position indicator
updatePositionIndicator(Math.round(newLeft), Math.round(newTop));
if (e.type.includes('touch')) {
e.preventDefault();
}
}
stopDrag() {
if (this.isDragging) {
this.isDragging = false;
this.modal.classList.remove('dragging');
hidePositionIndicator();
}
}
startResize(e) {
this.isResizing = true;
const rect = this.modal.getBoundingClientRect();
this.startX = e.clientX;
this.startY = e.clientY;
this.startWidth = rect.width;
this.startHeight = rect.height;
e.stopPropagation();
}
resize(e) {
if (!this.isResizing) return;
const deltaX = e.clientX - this.startX;
const deltaY = e.clientY - this.startY;
const newWidth = Math.max(300, this.startWidth + deltaX);
const newHeight = Math.max(200, this.startHeight + deltaY);
this.modal.style.width = newWidth + 'px';
this.modal.style.minHeight = newHeight + 'px';
}
stopResize() {
this.isResizing = false;
}
reset() {
this.modal.style.left = '';
this.modal.style.top = '';
this.modal.style.transform = '';
this.modal.style.width = '';
this.modal.style.minHeight = '';
}
}
// Initialize modals
const modals = {};
document.querySelectorAll('.modal-overlay').forEach(overlay => {
modals[overlay.id] = new DraggableModal(overlay);
});
// Open modal function
function openModal(id) {
const overlay = document.getElementById(id);
const mainContent = document.getElementById('mainContent');
overlay.classList.add('active');
mainContent.classList.add('blurred');
// Reset position when opening
if (modals[id]) {
modals[id].reset();
}
}
// Close modal function
function closeModal(id) {
const overlay = document.getElementById(id);
const mainContent = document.getElementById('mainContent');
overlay.classList.remove('active');
// Check if any modal is still open
const anyOpen = document.querySelector('.modal-overlay.active');
if (!anyOpen) {
mainContent.classList.remove('blurred');
}
}
// Position indicator functions
function updatePositionIndicator(x, y) {
const indicator = document.getElementById('posIndicator');
indicator.textContent = `X: ${x}, Y: ${y}`;
indicator.classList.add('visible');
}
function hidePositionIndicator() {
setTimeout(() => {
document.getElementById('posIndicator').classList.remove('visible');
}, 500);
}
// Close modal on overlay click
document.querySelectorAll('.modal-overlay').forEach(overlay => {
overlay.addEventListener('click', (e) => {
if (e.target === overlay) {
closeModal(overlay.id);
}
});
});
// Close modal on ESC key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
document.querySelectorAll('.modal-overlay.active').forEach(overlay => {
closeModal(overlay.id);
});
}
});
// Double-click header to center modal
document.querySelectorAll('.modal-header').forEach(header => {
header.addEventListener('dblclick', function() {
const modal = this.closest('.modal');
const overlay = this.closest('.modal-overlay');
modal.style.left = '50%';
modal.style.top = '50%';
modal.style.transform = 'translate(-50%, -50%)';
if (modals[overlay.id]) {
modals[overlay.id].startLeft = 0;
modals[overlay.id].startTop = 0;
}
});
});
</script>
</body>
</html>
Features Included
| Feature | Description |
|---|
| 🖱️ Drag & Drop | Drag by holding the modal header |
| 🌫️ Blur Effect | Background blurs when modal opens |
| 📱 Touch Support | Works on mobile devices |
| 🔲 Boundary Detection | Modal stays within viewport |
| ⌨️ Keyboard Support | ESC key closes modal |
| 📐 Resizable | Drag corner to resize |
| 🎯 Center on Double-Click | Double-click header to re-center |
| ✨ Smooth Animations | CSS transitions for open/close |
How It Works
- Blur Effect: Uses CSS
filter: blur() on main content and backdrop-filter on overlay
- Dragging: Tracks mouse/touch position and updates modal's absolute position
- Boundaries: Calculates viewport limits to keep modal visible
- Multiple Modals: Each modal instance is managed independently