纯css 实现模态框


对于模态框我个人一直都是用js + css 的方式实现,这里却采用css target 属性,很有想法

<!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>

 

 

 

 

 <!-- 初始化模态框 -->
    <div class="modal">
        <div class="mtk">
            <h1>hello world</h1>
        </div>
    </div>

.modal:before {
    content: "";
    display: block;
    background: rgba(0, 0, 0, 0.6);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1001;
}
.modal .mtk{
    width: 560px;
    height: 560px;
    display: block;
    border-radius: 5px;
    position: fixed;
    margin-left: -120px;
    z-index: 1001;
    top: 20%;
    right: 0;
    bottom: 0;
    left: 39%;
    background:#fff;
    transition: opacity 400ms ease-in;
}

转载:https://blog.csdn.net/qq_37026254/article/details/88716812

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM