自定义模态框

因组件无法满足所需模态框的样式,干脆自己写了一个js模态框 可以自定义改变样式
效果如下图
在这里插入图片描述
下面贴上完整代码可以直接套用 若不满足想要的样式 可以修改

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>change tab</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        .box{
            width: 100%;
            height: 100%;
            position: fixed;
            background: rgba(0, 0, 0, .3);
        }
        .modal-dialog{
            width: 600px;
            height: 400px;
            background: #fff;
            border: 1px solid #ccc;
            border-radius: 10px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%);
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }
        .modal-close{
            width: 20px;
            height: 20px;
            line-height: 20px;
            text-align: center;
            display: inline-block;
            position: absolute;
            color: #000;
            right: 10px;
            top: 10px;
            cursor: pointer;
        }
        .modal-header{
            height: 40px;
            line-height: 40px;
            border-bottom: 1px solid #ccc;
            padding-left: 10px;
            padding-right: 40px;
            overflow: hidden;
        }
        .modal-footer{
            height: 50px;
            border-top: 1px solid #ccc;
            padding-left: 10px;
            padding-right: 40px;
            overflow: hidden;
            text-align: center;
        }
        .modal-main{
            flex: 1;
            overflow-y: auto;
            overflow-x: hidden;
        }
        .modal-button{
            width: 100px;
            height: 30px;
            outline: none;
            border: none;
            cursor: pointer;
            color: #fff;
            margin-top: 10px;
            border-radius: 5px;
        }
        .ceject{
            background: #007bff;
            margin-right: 10px;
        }
        .cancle{
            background: #6c757d;
            margin-left: 10px;
        }
        .ceject:hover{
            background: #5599e2;
        }
        .cancle:hover{
            background: #878f96;
        }
        .modal-main::-webkit-scrollbar {/*滚动条整体样式*/
        width: 10px;     /*高宽分别对应横竖滚动条的尺寸*/
        height: 1px;
        }
        .modal-main::-webkit-scrollbar-thumb {/*滚动条里面小方块*/
            border-radius: 10px;
            -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
            background: #535353;
        }
        .modal-main::-webkit-scrollbar-track {/*滚动条里面轨道*/
            -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
            border-radius: 10px;
            background: #EDEDED;
        }
        .modal-block{
            display: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="modal-dialog">
            <span class="modal-close">X</span>
            <div class="modal-header">此处是标题</div>
            <div class="modal-main">
                <p>此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容</p>
                <p>此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容</p>
                <p>此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容</p>
                <p>此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容</p>
                <p>此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容</p>
                <p>此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容此处是内容</p>
            </div>
            <div class="modal-footer">
                <button class="modal-button ceject">确定</button>
                <button class="modal-button cancle">取消</button>
            </div>
        </div>
    </div>
    <script>
        $('.modal-close').click(function(){
            $('.box').addClass("modal-block")
        })
        $('.modal-button.ceject').click(function(){
            $('.box').addClass("modal-block")
        })
        $('.modal-button.cancle').click(function(){
            $('.box').addClass("modal-block")
        })
    </script>
</body>

</html>
发布了17 篇原创文章 · 获赞 0 · 访问量 194

猜你喜欢

转载自blog.csdn.net/who_become_gods/article/details/104539721