原生javascript 自定义弹出窗口, 嵌入iframe

1,523 阅读1分钟

1.引言

这几天维护了一个原生Js项目(jquery),没有使用vue框架等,没有弹框组件,所以自定义了一个弹框类

2.弹框效果

image.png

3.弹框对象MyLayer(文件名myLayer.js)


/**
 * @param options 弹窗基本配置信息
 * @constructor 构造方法
 */

 function MyLayer(options) {
    this.options = options ;
  }
  
  /**
   * 打开弹窗
   */

   MyLayer.prototype.openLayer = function () {
     // 弹框窗div 绝对定位、设置zindex值1001
    var background_layer = document.createElement("div");
    background_layer.style.display = "none";
    background_layer.style.position = "absolute";
    background_layer.style.top =  "0px";
    background_layer.style.left = "0px";
    background_layer.style.width = "100%";
    background_layer.style.height = "100%";
    background_layer.style.backgroundColor = "gray";
    background_layer.style.zIndex = "1001";
    background_layer.style.opacity = "0.8" ;
    // 弹框顶层div 绝对定位、设置zindex值1002
    var open_layer = document.createElement("div");
    open_layer.style.display = "none";
    open_layer.style.position = "absolute";
    open_layer.style.top = this.options.top === undefined ? "10%" : this.options.top;
    open_layer.style.left = this.options.left === undefined ? "10%" :this.options.left;
    open_layer.style.width = this.options.width === undefined ? "80%" : this.options.width;
    open_layer.style.height = this.options.height === undefined ? "80%" : this.options.height;
    open_layer.style.border = "1px solid lightblue";
    open_layer.style.borderRadius = "15px" ;
    open_layer.style.boxShadow = "4px 4px 10px #171414";
    open_layer.style.backgroundColor = "white";
    open_layer.style.zIndex = "1002";
    open_layer.style.overflow = "auto";
    // 弹框标题行p---两个子节点--标题节点span---span_title--关闭节点span_close
    var div_toolBar = document.createElement("div");
    div_toolBar.style.textAlign = "right";
    div_toolBar.style.paddingTop = "10px" ;
    div_toolBar.style.backgroundColor = "aliceblue";
    div_toolBar.style.height = "40px";
    //标题节点span---span_title 子节点有一个文本节点
    var span_title = document.createElement("span");
    span_title.style.fontSize = "18px";
    span_title.style.color = "blue" ;
    span_title.style.float = "left";
    span_title.style.marginLeft = "20px";
    var span_title_content = document.createTextNode(this.options.title === undefined ? "" : this.options.title);
    span_title.appendChild(span_title_content);
    div_toolBar.appendChild(span_title);
    // 关闭节点span_close 添加关闭点击事件,子节点也有一个文本节点
    var span_close = document.createElement("span");
    span_close.style.fontSize = "16px";
    span_close.style.color = "blue" ;
    span_close.style.cursor = "pointer";
    span_close.style.marginRight = "20px";
    span_close.onclick = function () {
        open_layer.style.display = "none";
        background_layer.style.display = "none";
    };
    var span_close_content = document.createTextNode("关闭");
    span_close.appendChild(span_close_content);
    div_toolBar.appendChild(span_close);
    open_layer.appendChild(div_toolBar);
    // 内容div 添加到open_layer中
    var div_content = document.createElement("div");
    div_content.style.textAlign = "center";
    var content_area = document.createTextNode(this.options.content === undefined ? "" : this.options.content);
    div_content.appendChild(content_area);
    open_layer.appendChild(div_content);
    // 内嵌页面iframe,添加到open_layer中
    var iframe = document.createElement("iframe");
    iframe.src = this.options.url === undefined ? "" : this.options.url;
    iframe.style.width = "100%";
    iframe.style.height = "100%";
    open_layer.appendChild(iframe);
    // open_layer, background_layer添加到网页中
    document.body.appendChild(open_layer);
    document.body.appendChild(background_layer);
    // 在调用MyLayer实例的openLayer方法时显示弹框
    open_layer.style.display = "block" ;
    background_layer.style.display = "block";
};
  

4.html打开弹窗(实例化Mylayer类)

<html>
<head>
    <title>自定义弹出窗口</title>
    <script src="js/myLayer.js"></script>
    <style type="text/css">
        button{
            width: 50px;
            height: 50px;
            border: 1px solid blue;
            background-color: blue;
            color: red;
            border-radius: 5px;
            -webkit-box-shadow: 2px 2px 2px gray;
            -moz-box-shadow: 2px 2px 2px gray ;
            box-shadow: 2px 2px 2px gray ;
        }
        button:hover{
            background-color: green;
            cursor: pointer;
        }
    </style>
    <script type="text/javascript">
        function openWindow() {
            new MyLayer({
                top:"10%",
                left:"10%",
                width:"80%",
                height:"80%",
                title:"我的标题",
                content:"操作成功",
                url: "https://juejin.cn/"
            }).openLayer();
        }
    </script>
</head>
<body>
    <button type="button" onclick="openWindow()">打开弹窗</button>
</body>
</html>

5 使用浏览器打开上边html文件即可实现弹框效果