zoukankan      html  css  js  c++  java
  • 弹出框

            方法一

    自定义弹出框:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>PopAlert</title>
    <link rel="stylesheet" href="res/Alert.css">
    </head>
    <body>

    <input type="button" value="SHOW" id="show">

    </body>
    <script>

    //绑定事件
    var showBtn = document.getElementById("show");
    // var cover = document.getElementsByClassName("cover")[0];

    showBtn.onclick = function(){
    var cover = document.createElement("div");
    cover.className = "cover";
    // cover.style.display = "block";
    // 1.创建弹出层
    var html = '<div class="alert">';
    html += '<div class="content"></div>';
    html += '<div class="tool">';
    html += '<input type="button" value="Ok">';
    html += '<input type="button" value="Cancel" id="cancel">';
    html += '</div></div>';

    cover.innerHTML = html;
    // appendChild可以在DOM对象后面追加其他的DOM内容
    document.body.appendChild(cover);
    adjust();
    }

    function adjust(){
    var cover = document.getElementsByClassName("cover")[0];
    // 2.让弹出层出现在合适的位置
    cover.style.width = window.innerWidth+"px";
    cover.style.height = window.innerHeight+"px";
    var alertDiv = document.getElementsByClassName("alert")[0];
    alertDiv.style.marginTop = -(alertDiv.offsetHeight/2)+"px";
    alertDiv.style.marginLeft = -(alertDiv.offsetWidth/2)+"px";
    }

    window.onresize = function (){
    adjust();
    }

    // var cancel = document.getElementById("cancel");
    // cancel.onclick = function(){
    // cover.style.display = "none";
    // }

    </script>
    </html>

    Alert.css

    * {
    padding: 0;
    margin: 0;
    }

    .cover {
    position: fixed;
    100%;
    background: rgba(100,100,100);
    z-index: 999;
    top: 0;
    left: 0;
    /*display: none;*/
    }

    .alert {
    border-radius: 8px;
    border: 8px purple solid;
    300px;
    height: 150px;
    top: 50%;
    left: 50%;
    position: absolute;
    background: cyan;
    text-align: center;
    }

    .content {
    padding: 25px 15px;
    }

    .tool {
    100%;
    padding: 15px 0;
    background: yellow;
    text-align: right;
    position: absolute;
    bottom: 0;
    }

    .tool input {
    50px;
    height: 25px;
    margin-right: 15px;
    background: green;
    border: none;
    }

     方法二

     

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>PopAlert</title>
    <link rel="stylesheet" href="res/Alert.css">
    </head>
    <body>

    <input type="button" value="SHOW" id="show">

    </body>
    <script>

    function PopAlert(c){
    this.cover = document.createElement("div");
    this.cover.className = "cover";
    // cover.style.display = "block";
    // 1.创建弹出层
    var html = '<div class="alert">';
    html += '<div class="content">'+c+'</div>';
    html += '<div class="tool">';
    html += '<input type="button" value="Ok">';
    html += '<input type="button" value="Cancel" id="cancel">';
    html += '</div></div>';

    this.cover.innerHTML = html;

    this.ajdust = function(){
    // 2.让弹出层出现在合适的位置
    this.cover.style.width = window.innerWidth+"px";
    this.cover.style.height = window.innerHeight+"px";
    this.alert.style.marginTop = -(this.alert.offsetHeight/2)+"px";
    this.alert.style.marginLeft = -(this.alert.offsetWidth/2)+"px";
    }

    this.show = function(){
    // appendChild可以在DOM对象后面追加其他的DOM内容
    document.body.appendChild(this.cover);
    this.alert = document.getElementsByClassName("alert")[0];
    this.ajdust();
    }

    this.close = function(){
    document.body.removeChild(this.cover);
    }
    }


    //绑定事件
    var showBtn = document.getElementById("show");
    var popAlert = null;

    showBtn.onclick = function(){
    popAlert = new PopAlert("你好么?");
    popAlert.show();

    // //
    // popAlert.close();
    }

    window.onresize = function (){
    if (popAlert) {
    popAlert.ajdust();
    }
    }

    </script>
    </html>

  • 相关阅读:
    UNIX网络编程--学习日记
    VC下ffmpeg例程调试报错处理
    Cholesky Decomposition
    [置顶] ORM框架Hibernate (二) 对象到关系多对一映射
    [置顶] rails2 升级到 rails3 过程详细记录
    python 连接MYSQL数据库(入门帖)
    【PAT】1005 Spell It Right
    Eclipse & MyEclipse下常用快捷键介绍
    HDU 2544
    你如何只用一个数组实现三个栈?
  • 原文地址:https://www.cnblogs.com/qh926/p/6099553.html
Copyright © 2011-2022 走看看