弹窗demo

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>弹窗</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <button id="modalBtn" class="button">弹窗</button>
    <div id="simpleModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <span class="closeBtn">&times;</span>
                <h2>弹窗头部</h2>
            </div>
            <div class="modal-body">
                <p>这是一个弹窗效果</p>
                <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Corrupti odio, praesentium voluptatem dolor minima nesciunt enim, debitis in accusantium iure repudiandae explicabo placeat! Error tenetur omnis quia, voluptatem obcaecati ratione?</p>
            </div>
            <div class="modal-footer">
                <h3>弹窗底部</h3>
            </div>
        </div>
    </div>

    <script src="main.js"></script>
</body>
</html>

style.css

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 17px;
    line-height: 1.6;
}

.button {
    background-color: coral;
    padding: 1em 2em;
    color: #fff;
    border: 0;
}

.button:hover {
    background-color: #333;
}

.modal {
    display: none;
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
    background-color: #f4f4f4;
    margin: 30% auto;
    width: 80%;
    box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2), 0 7px 20px 0 rgba(0, 0, 0, 0.17);
    animation-name: modalopen;
    animation-duration: 1s;
}

.modal-header h2,
.modal-footer h3 {
    margin: 0;
}

.modal-header {
    background-color: coral;
    padding: 15px;
    color: #fff;
}

.closeBtn {
    color: #fff;
    float: right;
    font-size: 30px;
}

.closeBtn:hover,
.closeBtn:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-body {
    padding: 10px 20px;
}

.modal-footer {
    background-color: coral;
    padding: 10px;
    color: #fff;
    text-align: center;
}

@keyframes modalopen {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

main.js

// 获取弹窗元素
var modal = document.getElementById("simpleModal");
// 获取按钮元素
var modalBtn = document.getElementById("modalBtn");
// 获取关闭弹窗按钮元素
var closeBtn = document.getElementsByClassName('closeBtn')[0];
// 监听打开弹窗事件
modalBtn.addEventListener("click", openModal);
// 监听关闭弹窗事件
closeBtn.addEventListener("click", closeModal);
// 监听window关闭弹窗
window.addEventListener("click", outsiteClick);
// 弹窗事件
function openModal() {
    modal.style.display = 'block';
}
// 关闭事件
function closeModal() {
    modal.style.display = 'none';
}
// outsiteClick
function outsiteClick(e) {
    if (e.target == modal) {
        modal.style.display = 'none';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值