モーダルウィンドウ:フローティング|Modal Window: Floating
【Floating Modal】
A floating modal window with subtle shadows and rounded corners. Gives a modern, elevated appearance.
HTML
<div class="container">
<h1>Floating Modal</h1>
<button id="modal-btn">Open Floating Modal</button>
</div>
<div id="mask" class="mask"></div>
<div id="modal" class="modal">
<div class="modal-content">
<p>【Floating Modal】</p>
<p>A floating modal window with subtle shadows and rounded corners. Gives a modern, elevated appearance.</p>
<div class="close-btn-container">
<button class="close-btn">Close</button>
</div>
</div>
</div>
CSS
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.container {
text-align: center;
color: white;
}
h1 {
margin-bottom: 30px;
font-size: 2.5em;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}
/* フローティングモーダル */
#modal-btn {
padding: 15px 30px;
background: #fff;
color: #2c3e50;
border: 2px solid #ecf0f1;
border-radius: 50px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
#modal-btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
border-color: #3498db;
}
#mask {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(52, 73, 94, 0.7);
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
backdrop-filter: blur(5px);
}
#mask.appear {
opacity: 1;
visibility: visible;
}
#modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0.9);
background: #fff;
padding: 40px;
border-radius: 25px;
z-index: 1001;
opacity: 0;
visibility: hidden;
transition: all 0.4s ease;
max-width: 500px;
width: 90%;
box-shadow:
0 20px 40px rgba(0, 0, 0, 0.1),
0 0 0 1px rgba(255, 255, 255, 0.1);
}
#modal.appear {
opacity: 1;
visibility: visible;
transform: translate(-50%, -50%) scale(1);
}
#modal p {
color: #2c3e50;
line-height: 1.7;
margin-bottom: 30px;
font-size: 16px;
text-align: center;
}
#modal .close-btn {
padding: 12px 24px;
background: #3498db;
color: #fff;
border: none;
border-radius: 25px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
display: block;
margin: 0 auto;
box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
}
#modal .close-btn:hover {
background: #2980b9;
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
}
JavaScript
document.addEventListener('DOMContentLoaded', function() {
const modalBtn = document.getElementById('modal-btn');
const mask = document.getElementById('mask');
const modal = document.getElementById('modal');
const closeBtn = modal.querySelector('.close-btn');
// モーダルを開く
modalBtn.addEventListener('click', () => {
mask.classList.add('appear');
modal.classList.add('appear');
});
// マスクをクリックして閉じる
mask.addEventListener('click', () => {
closeModal();
});
// 閉じるボタンをクリックして閉じる
closeBtn.addEventListener('click', () => {
closeModal();
});
// ESCキーで閉じる
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
closeModal();
}
});
// ホバーエフェクト強化
modal.addEventListener('mouseenter', function() {
this.style.transform = 'translate(-50%, -50%) scale(1.02)';
this.style.boxShadow = '0 25px 50px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(255, 255, 255, 0.2)';
});
modal.addEventListener('mouseleave', function() {
this.style.transform = 'translate(-50%, -50%) scale(1)';
this.style.boxShadow = '0 20px 40px rgba(0, 0, 0, 0.1), 0 0 0 1px rgba(255, 255, 255, 0.1)';
});
function closeModal() {
mask.classList.remove('appear');
modal.classList.remove('appear');
}
});