• 利用 js 实现弹出蒙板(model)功能


    关于 js 实现一个简单的蒙板功能(model)

    思路:

    • 创建一个蒙板, 设置蒙板的堆叠顺序保证能将其它元素盖住
    
    position: absolute;
    top: 0;
    left: 0;
    display: none;
    background-color: rgba(9, 9, 9, 0.63);
     100%;
    height: 100%;
    z-index: 1000;
    
    • 设置蒙板中内容的背景色和展示格式
    
     50%;
    text-align: center;
    background: #ffffff;
    border-radius: 6px;
    margin: 100px auto;
    line-height: 30px;
    z-index: 10001;
    
    • 绑定事件, 动态切换蒙板的 display 属性
    
    function showModel() {
        document.getElementById('myModel').style.display = 'block';
    }
    function closeModel() {
        document.getElementById('myModel').style.display = 'none';
    }
    

    注意事项: 蒙板要采用绝对定位, 宽和高要占満整个页面, 堆叠顺序要大

    源代码

    
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>蒙板</title>
        <style>
            .container {
                 900px;
                margin: 50px auto;
                text-align: center;
            }
    
            #myModel {
                position: absolute;
                top: 0;
                left: 0;
                display: none;
                background-color: rgba(9, 9, 9, 0.63);
                 100%;
                height: 100%;
                z-index: 1000;
            }
    
            .model-content {
                 50%;
                text-align: center;
                background: #ffffff;
                border-radius: 6px;
                margin: 100px auto;
                line-height: 30px;
                z-index: 10001;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <button onclick="showModel()">弹出蒙板</button>
        <div id="myModel" onclick="closeModel()">
            <div class="model-content">
                <p>你好啊,我是内容~~</p>
                <p>
                    <button id="closeModel" onclick="closeModel()">关闭</button>
                </p>
            </div>
        </div>
    </div>
    <script>
        function showModel() {
            document.getElementById('myModel').style.display = 'block';
        }
        function closeModel() {
            document.getElementById('myModel').style.display = 'none';
        }
    </script>
    </body>
    </html>
    
  • 相关阅读:
    clear ,refresh,free
    记录一次vxworks下使用NFS组件的过程
    [dart学习]第七篇:类(构造函数)
    [dart学习]第六篇:流程控制语句
    [沉痛哀悼宁滨院士]
    [dart学习]第五篇:操作符
    [dart学习]第四篇:函数
    [dart学习]第三篇:dart变量介绍 (二)
    [dart学习]第二篇:dart变量介绍 (一)
    [dart学习]第一篇:windows下安装配置dart编译环境,写出helloworld
  • 原文地址:https://www.cnblogs.com/shinejaie/p/5737359.html
Copyright © 2020-2023  润新知