使用原生的css写一个模态框

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>简单的模态框(Modal)</title>
    <style>
        .wrap {
            padding: 40px;
            text-align: center;
        }
        .btn {
            background: #428bca;
            border: #357ebd solid 1px;
            border-radius: 3px;
            color: #fff;
            display: inline-block;
            font-size: 14px;
            padding: 8px 15px;
            text-decoration: none;
            text-align: center;
            min-width: 60px;
            position: relative;
            transition: color .1s ease;
        }
        .btn:hover {
            background: #357ebd;
        }
        .btn.btn-big {
            font-size: 18px;
            padding: 15px 20px;
            min-width: 100px;
        }
        .btn-close {
            color: #aaa;
            font-size: 30px;
            text-decoration: none;
            position: absolute;
            right: 5px;
            top: 0;
        }
        .btn-close:hover {
            color: #919191;
        }
        .modal:before {
            content: "";
            display: none;
            background: rgba(0, 0, 0, 0.6);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 10;
        }
        .modal:target:before {
            display: block;
        }
        .modal:target .modal-dialog {
            -webkit-transform: translate(0, 0);
            -ms-transform: translate(0, 0);
            transform: translate(0, 0);
            top: 10%;
        }
        .modal-dialog {
            background: #fefefe;
            border: #333 solid 1px;
            border-radius: 5px;
            margin-left: -120px;
            position: fixed;
            left: 50%;
            top: -100%;
            z-index: 11;
            width: 240px;
            -webkit-transform: translate(0, -500%);
            -ms-transform: translate(0, -500%);
            transform: translate(0, -500%);
            -webkit-transition: -webkit-transform 0.3s ease-out;
            -moz-transition: -moz-transform 0.3s ease-out;
            -o-transition: -o-transform 0.3s ease-out;
            transition: transform 0.3s ease-out;
        }
        .modal-body {
            padding: 10px 20px;
        }
        .modal-header,
        .modal-footer {
            padding: 10px 20px;
        }
        .modal-header {
            border-bottom: #eee solid 1px;
        }
        .modal-header h2 {
            font-size: 20px;
        }
        .modal-footer {
            border-top: #eee solid 1px;
            text-align: right;
        }
    </style>
</head>
<body>
<div class="wrap">
    <a href="#modal-one" class="btn btn-big">模态框</a>
</div>
<div class="modal" id="modal-one" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-header">
            <h2>42度空间</h2>
            <a href="#" class="btn-close" aria-hidden="true">×</a>
        </div>
        <div class="modal-body">
            <p>一个纯CSS的模态框!</p>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn">确 定</a>
        </div>
    </div>
</div>
</body>
</html>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您回答关于前端开发的问题。对于弹窗组件,您可以使用原生 JavaScript 来创建一个模态框,并利用相关的 CSS 样式进行美化。您可以参考以下示例代码: HTML 代码: ``` <button id="myBtn">打开弹窗</button> <div id="myModal" class="modal"> <div class="modal-content"> <span class="close">×</span> <p>这是一个模态框。请在此处添加你的内容。</p> </div> </div> ``` CSS 代码: ``` .modal { display: none; position: fixed; z-index: 10; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0, 0, 0, 0.5); } .modal-content { background-color: #fefefe; margin: 15% auto; padding: 20px; border: 1px solid #888; width: 80%; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } ``` JavaScript 代码: ``` var modal = document.getElementById("myModal"); var btn = document.getElementById("myBtn"); var span = document.getElementsByClassName("close")[0]; btn.onclick = function() { modal.style.display = "block"; } span.onclick = function() { modal.style.display = "none"; } window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } ``` 以上就是一个简单的模态框代码实现。当点击按钮时,模态框会弹出显示。当点击关闭按钮或模态框的任意位置时,模态框会关闭隐藏。您可以根据自己的需求进一步优化这个组件。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值