HTML
<div class="container">
<h1>Fairy Tale Modal</h1>
<button id="modal-btn">Open Fairy Tale Modal</button>
</div>
<div id="mask" class="mask"></div>
<div id="modal" class="modal">
<div class="modal-content">
<p>【Fairy Tale Modal】</p>
<p>A magical and dreamy modal window with pastel colors and floating particles. Perfect for creating a whimsical and enchanting atmosphere.</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: linear-gradient(45deg, #ff9a9e, #fecfef, #fecfef, #ff9a9e);
background-size: 300% 300%;
color: #fff;
border: none;
border-radius: 25px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
animation: gradientShift 3s ease infinite;
box-shadow: 0 4px 15px rgba(255, 154, 158, 0.4);
}
@keyframes gradientShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
#modal-btn:hover {
transform: translateY(-3px);
box-shadow: 0 8px 25px rgba(255, 154, 158, 0.6);
}
#mask {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 154, 158, 0.8);
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
backdrop-filter: blur(10px);
}
#mask.appear {
opacity: 1;
visibility: visible;
}
#modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0.9);
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 50%, #fad0c4 100%);
padding: 40px;
border-radius: 20px;
z-index: 1001;
opacity: 0;
visibility: hidden;
transition: all 0.4s ease;
max-width: 500px;
width: 90%;
box-shadow:
0 20px 40px rgba(255, 154, 158, 0.3),
0 0 0 1px rgba(255, 255, 255, 0.2);
}
#modal.appear {
opacity: 1;
visibility: visible;
transform: translate(-50%, -50%) scale(1);
}
#modal p {
color: #fff;
line-height: 1.7;
margin-bottom: 30px;
font-size: 16px;
text-align: center;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
#modal .close-btn {
padding: 12px 24px;
background: rgba(255, 255, 255, 0.2);
color: #fff;
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 25px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
display: block;
margin: 0 auto;
backdrop-filter: blur(10px);
}
#modal .close-btn:hover {
background: rgba(255, 255, 255, 0.3);
border-color: rgba(255, 255, 255, 0.5);
transform: translateY(-2px);
}
/* パーティクルアニメーション */
@keyframes float {
0%, 100% { transform: translateY(0px) rotate(0deg); opacity: 0.6; }
50% { transform: translateY(-20px) rotate(180deg); opacity: 1; }
}
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');
// パーティクルエフェクトを追加
createParticles(modal);
});
// マスクをクリックして閉じる
mask.addEventListener('click', () => {
closeModal();
});
// 閉じるボタンをクリックして閉じる
closeBtn.addEventListener('click', () => {
closeModal();
});
// ESCキーで閉じる
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
closeModal();
}
});
function closeModal() {
mask.classList.remove('appear');
modal.classList.remove('appear');
clearParticles();
}
// パーティクルエフェクト作成関数
function createParticles(modal) {
const particlesContainer = document.createElement('div');
particlesContainer.className = 'particles-container';
particlesContainer.style.cssText = `
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
overflow: hidden;
border-radius: 20px;
`;
for (let i = 0; i < 20; i++) {
const particle = document.createElement('div');
particle.className = 'particle';
particle.style.cssText = `
position: absolute;
width: 4px;
height: 4px;
background: rgba(255, 255, 255, 0.6);
border-radius: 50%;
animation: float 3s ease-in-out infinite;
animation-delay: ${Math.random() * 3}s;
`;
particle.style.left = Math.random() * 100 + '%';
particle.style.top = Math.random() * 100 + '%';
particlesContainer.appendChild(particle);
}
modal.appendChild(particlesContainer);
}
// パーティクルクリア関数
function clearParticles() {
const particlesContainer = document.querySelector('.particles-container');
if (particlesContainer) {
particlesContainer.remove();
}
}
});