Div居中弹出层

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Div居中弹出层</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            height: 100vh;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .trigger-button {
            padding: 12px 24px;
            background: linear-gradient(to right, #4776E6, #8E54E9);
            color: white;
            border: none;
            border-radius: 30px;
            font-size: 16px;
            cursor: pointer;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
        }
        
        .trigger-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
        }
        
        .trigger-button:active {
            transform: translateY(1px);
        }
        
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.6);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
        }
        
        .modal-overlay.active  {
            opacity: 1;
            visibility: visible;
        }
        
        .modal-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            width: 90%;
            max-width: 500px;
            transform: translateY(20px);
            transition: all 0.3s ease;
        }
        
        .modal-overlay.active  .modal-container {
            transform: translateY(0);
        }
        
        .modal-header {
            padding: 20px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .modal-title {
            font-size: 20px;
            font-weight: bold;
            color: #333;
        }
        
        .close-button {
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            color: #888;
            transition: color 0.2s;
        }
        
        .close-button:hover {
            color: #333;
        }
        
        .modal-content {
            padding: 20px;
            line-height: 1.6;
            color: #555;
        }
        
        .modal-footer {
            padding: 15px 20px;
            border-top: 1px solid #eee;
            display: flex;
            justify-content: flex-end;
        }
        
        .modal-footer button {
            padding: 8px 16px;
            margin-left: 10px;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.2s;
        }
        
        .confirm-button {
            background: linear-gradient(to right, #4776E6, #8E54E9);
            color: white;
            border: none;
        }
        
        .confirm-button:hover {
            opacity: 0.9;
        }
        
        .cancel-button {
            background: #f0f0f0;
            border: 1px solid #ddd;
            color: #555;
        }
        
        .cancel-button:hover {
            background: #e6e6e6;
        }
        
        .website-link {
            display: block;
            text-align: center;
            margin-top: 30px;
            color: #666;
            text-decoration: none;
            font-size: 14px;
            transition: color 0.2s;
        }
        
        .website-link:hover {
            color: #8E54E9;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <button class="trigger-button" id="openModal">打开弹出层</button>
    
    <div class="modal-overlay" id="modalOverlay">
        <div class="modal-container">
            <div class="modal-header">
                <h3 class="modal-title">提示信息</h3>
                <button class="close-button" id="closeModal">&times;</button>
            </div>
            <div class="modal-content">
                <p>这是一个居中显示的弹出层,点击关闭按钮或外部遮罩可以关闭它。</p>
                <p>弹出层具有平滑的动画效果,包括淡入淡出和轻微的上移动画。</p>

            </div>
            <div class="modal-footer">
                <button class="cancel-button" id="cancelButton">取消</button>
                <button class="confirm-button" id="confirmButton">确定</button>
            </div>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded',  function() {
            const openModalBtn = document.getElementById('openModal'); 
            const closeModalBtn = document.getElementById('closeModal'); 
            const cancelBtn = document.getElementById('cancelButton'); 
            const confirmBtn = document.getElementById('confirmButton'); 
            const modalOverlay = document.getElementById('modalOverlay'); 
            
            // 打开弹出层 
            openModalBtn.addEventListener('click',  function() {
                modalOverlay.classList.add('active'); 
                document.body.style.overflow  = 'hidden'; // 防止背景滚动 
            });
            
            // 关闭弹出层 
            function closeModal() {
                modalOverlay.classList.remove('active'); 
                document.body.style.overflow  = 'auto'; // 恢复滚动 
            }
            
            closeModalBtn.addEventListener('click',  closeModal);
            cancelBtn.addEventListener('click',  closeModal);
            
            // 点击遮罩层关闭 
            modalOverlay.addEventListener('click',  function(e) {
                if (e.target  === modalOverlay) {
                    closeModal();
                }
            });
            
            // 确定按钮 
            confirmBtn.addEventListener('click',  function() {
                alert('您点击了确定按钮');
                closeModal();
            });
            
            // ESC键关闭 
            document.addEventListener('keydown',  function(e) {
                if (e.key  === 'Escape' && modalOverlay.classList.contains('active'))  {
                    closeModal();
                }
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值