html模态弹出窗口

时间:2010-01-02 17:11:03

标签: html popup modal-dialog

所有

如何为以下代码制作一个简单的模态弹出窗口。点击背景后,模态弹出窗口不应该消失。

<html>
<input type="textarea"></input>
</html>

感谢.........

7 个答案:

答案 0 :(得分:16)

这是一个简单的JavaScript示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Basic modal demo</title>
        <style type="text/css">
            body { margin: 0; }
            #shade, #modal { display: none; }
            #shade { position: fixed; z-index: 100; top: 0; left: 0; width: 100%; height: 100%; }
            #modal { position: fixed; z-index: 101; top: 33%; left: 25%; width: 50%; }
            #shade { background: silver; opacity: 0.5; filter: alpha(opacity=50); }
        </style>
    </head>
    <body>
        <div id="shade"></div>
        <div id="modal">
            <textarea rows="5" cols="25"></textarea>
            <button id="close">Close</button>
        </div>

        <p>
            <button id="start">Start</button>
        </p>
        <script type="text/javascript">
            var modal= document.getElementById('modal');
            var shade= document.getElementById('shade');
            document.getElementById('start').onclick= function() {
                modal.style.display=shade.style.display= 'block';
            };
            document.getElementById('close').onclick= function() {
                modal.style.display=shade.style.display= 'none';
            };

            // This code is a workaround for IE6's lack of support for the
            // position: fixed style.
            //
            if (!('maxHeight' in document.body.style)) {
                function modalsize() {
                    var top= document.documentElement.scrollTop;
                    var winsize= document.documentElement.offsetHeight;
                    var docsize= document.documentElement.scrollHeight;
                    shade.style.height= Math.max(winsize, docsize)+'px';
                    modal.style.top= top+Math.floor(winsize/3)+'px';
                };
                modal.style.position=shade.style.position= 'absolute';
                window.onscroll=window.onresize= modalsize;
                modalsize();
            }
        </script>

    </body>
</html>

您可以从中进行各种改进,例如iframe修复IE z-indexing或将其封装在可重用对象中,但这是它完成的基本方法。

答案 1 :(得分:2)

jQueryUI有一个模态对话框插件。根据您的要求,它不会仅通过单击背景来释放控件:http://jqueryui.com/demos/dialog/#modal

<a href="#" class="showModal">Show Modal Box</a>
<div id="modalContents" style="display:none;">
  <textarea>Hello World</textarea>
</div>

-

$(".showModal").click(function(e){
  e.preventDefault();
  $("#modalContents").dialog({bgiframe: true, height: 140, modal: true});
});

答案 2 :(得分:0)

chrome上的html5具有<dialog>元素。做实验

答案 3 :(得分:0)

HTML:

<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

CSS:

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

JS:

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

答案 4 :(得分:0)

您还可以使用本机HTML5.1 dailog。 当前对话框元素仅在Chrome 37 +,Safari 6+和Opera 24+中受支持。

var dailog = document.getElementById("dialog"); 

function openModal() { 
   // dailog.show(); 
      dailog.showModal();
} 

function closeModal() { 
    dailog.close(); 
} 
#dialog{width:300px;}
.right{float:right}
<button onclick="openModal()">Show dialog</button>


<dialog id="dialog">This is a dialog window<br/><br/><br/>
<button onclick="closeModal()" class="right">Close</button>
</dialog>

答案 5 :(得分:0)

我认为以下代码可以帮助您

<!DOCTYPE html>
<html>
<title>login modal</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<div class="w3-container">
  <h2>Login Modal</h2>
  <button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-green w3-large">Login</button>

  <div id="id01" class="w3-modal">
    <div class="w3-modal-content w3-card-4 w3-animate-zoom" style="max-width:600px">

      <div class="w3-center"><br>
        <span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-xlarge w3-hover-red w3-display-topright" title="Close Modal">&times;</span>
        
      </div>

      <form class="w3-container" action="/action_page.php">
        <div class="w3-section">
          <label><b>Username</b></label>
          <input class="w3-input w3-border w3-margin-bottom" type="text" placeholder="Enter Username" name="usrname" required>
          <label><b>Password</b></label>
          <input class="w3-input w3-border" type="password" placeholder="Enter Password" name="psw" required>
          <button class="w3-button w3-block w3-green w3-section w3-padding" type="submit">Login</button>
          <input class="w3-check w3-margin-top" type="checkbox" checked="checked"> Remember me
        </div>
      </form>

      <div class="w3-container w3-border-top w3-padding-16 w3-light-grey">
        <button onclick="document.getElementById('id01').style.display='none'" type="button" class="w3-button w3-red">Cancel</button>
        <span class="w3-right w3-padding w3-hide-small">Forgot <a href="#">password?</a></span>
      </div>

    </div>
  </div>
</div>
            
</body>
</html>

答案 6 :(得分:-1)

根据定义,模态窗口需要用户在将其返回到主窗口之前采取操作。所以你要找的不是模态而是窗口。