モーダルウィンドウ:マテリアルデザイン|Modal Window: Material Design

HTML
<div class="container">
    <button class="modal-button">Material Design Modal</button>
</div>

<div class="mask"></div>
<div class="modal">
    <div class="modal-content">
        <p>[Material Design Modal]</p>
        <p>A Google Material Design inspired modal window with elevation, shadows, and smooth animations. Features clean typography and intuitive interaction patterns.</p>
        <div class="close-btn-container">
            <button class="close-btn">Close</button>
        </div>
    </div>
</div>
CSS
.container {
    text-align: center;
}

.modal-button {
    padding: 15px 30px;
    background: #2196F3;
    color: #fff;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 5px rgba(33, 150, 243, 0.2);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.modal-button:hover {
    background: #1976D2;
    box-shadow: 0 4px 8px rgba(33, 150, 243, 0.3);
    transform: translateY(-1px);
}

.modal-button:active {
    transform: translateY(0);
    box-shadow: 0 2px 4px rgba(33, 150, 243, 0.2);
}

.mask {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    opacity: 0; visibility: hidden;
    transition: all 0.3s ease;
}

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

.modal {
    position: fixed;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%) scale(0.9);
    background: #fff;
    padding: 0;
    border-radius: 8px;
    z-index: 1001;
    opacity: 0; visibility: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    max-width: 500px; width: 90%;
    box-shadow: 0 11px 15px -7px rgba(0, 0, 0, 0.2), 0 24px 38px 3px rgba(0, 0, 0, 0.14), 0 9px 46px 8px rgba(0, 0, 0, 0.12);
    overflow: hidden;
}

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

.modal-content {
    padding: 24px;
}

.modal p {
    color: rgba(0, 0, 0, 0.87);
    font-size: 16px;
    line-height: 1.5;
    margin-bottom: 24px;
    font-weight: 400;
    text-align: center;
}

.close-btn-container {
    display: flex;
    justify-content: flex-end;
    padding: 8px 0;
    border-top: 1px solid rgba(0, 0, 0, 0.12);
    margin-top: 16px;
}

.close-btn {
    padding: 8px 16px;
    background: transparent;
    color: #2196F3;
    border: none;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    min-width: 64px;
    position: relative;
    overflow: hidden;
}

.close-btn:hover {
    background: rgba(33, 150, 243, 0.08);
}

.close-btn:active {
    background: rgba(33, 150, 243, 0.12);
}

@keyframes ripple {
    to {
        transform: scale(4);
        opacity: 0;
    }
}

@media (max-width: 768px) {
    .modal {
        width: 95%;
    }
    
    .modal-content {
        padding: 16px;
    }
    
    .modal p {
        font-size: 14px;
    }
    
    .modal-button {
        font-size: 14px;
        padding: 10px 20px;
    }
}
JavaScript
document.addEventListener('DOMContentLoaded', function() {
    const modalBtn = document.querySelector('.modal-button');
    const mask = document.querySelector('.mask');
    const modal = document.querySelector('.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', (e) => {
        createRipple(e);
        setTimeout(() => {
            closeModal();
        }, 150);
    });
    
    // ESCキーでモーダルを閉じる
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            closeModal();
        }
    });
    
    // モーダルを閉じる関数
    function closeModal() {
        mask.classList.remove('appear');
        modal.classList.remove('appear');
    }
    
    // リップルエフェクト作成関数
    function createRipple(e) {
        const ripple = document.createElement('span');
        const rect = e.target.getBoundingClientRect();
        const size = Math.max(rect.width, rect.height);
        const x = e.clientX - rect.left - size / 2;
        const y = e.clientY - rect.top - size / 2;
        
        ripple.style.cssText = `
            position: absolute;
            width: ${size}px;
            height: ${size}px;
            left: ${x}px;
            top: ${y}px;
            background: rgba(33, 150, 243, 0.3);
            border-radius: 50%;
            transform: scale(0);
            animation: ripple 0.6s linear;
            pointer-events: none;
        `;
        
        e.target.appendChild(ripple);
        
        setTimeout(() => {
            ripple.remove();
        }, 600);
    }
});
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次