モーダルウィンドウ:アニメーション付|Modal Window: Animated

HTML
<div class="container">
    <h1>Animated Modal</h1>
    <button id="modal-btn">Open Animated Modal</button>
</div>

<div id="mask" class="mask"></div>
<div id="modal" class="modal">
    <div class="modal-content">
        <p>【Animated Modal】</p>
        <p>A modal window with smooth animations. Features slide-in effects and hover animations for enhanced user experience.</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, #ff6b6b, #4ecdc4);
    color: #fff;
    border: none;
    border-radius: 25px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.4s ease;
    box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
    position: relative;
    overflow: hidden;
}

#modal-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
}

#modal-btn:hover::before {
    left: 100%;
}

#modal-btn:hover {
    transform: translateY(-3px) scale(1.05);
    box-shadow: 0 8px 25px rgba(255, 107, 107, 0.4);
}

#mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.4s ease;
    backdrop-filter: blur(8px);
}

#mask.appear {
    opacity: 1;
    visibility: visible;
}

#modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.8) rotateY(90deg);
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 40px;
    border-radius: 20px;
    z-index: 1001;
    opacity: 0;
    visibility: hidden;
    transition: all 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    max-width: 500px;
    width: 90%;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

#modal.appear {
    opacity: 1;
    visibility: visible;
    transform: translate(-50%, -50%) scale(1) rotateY(0deg);
}

#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.3);
}

#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);
}
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');
        
        // 特別なアニメーション処理
        setTimeout(() => {
            modal.style.transform = 'translate(-50%, -50%) scale(1) rotateY(0deg)';
        }, 50);
    });
    
    // マスクをクリックして閉じる
    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');
        modal.style.transform = 'translate(-50%, -50%) scale(0.8) rotateY(90deg)';
    }
});
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次