HTML 模态弹出窗口

17

如何为以下代码制作简单的模态弹出窗口。并且在单击背景时,模态弹出窗口不应消失。

<html>
<input type="textarea"></input>
</html>
7个回答

19
这是一个纯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();
}
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);
}
<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>

你可以从这里进行各种改进,例如使用iframe hack来修复IE的z-indexing问题,或将其封装成可重用的对象,但这就是基本的方式。


@bobince..您好,我在我的应用程序中使用了这个例子,我的应用程序包含视口和一个背景图像。每当我点击开始按钮时,不应该有滚动条,并且模态框也应该在中心位置,即使滚动后模态框也应该在中心位置。但是我只能得到模态框在顶部的结果。 - Mallikarjun
1
很棒终于看到一个非jQuery的答案,我把你的代码放进了一个JavaScript对象中,http://jsfiddle.net/orwellophile/yWMdv/ - Orwellophile

5

您也可以使用原生的HTML5.1对话框。目前,该对话框元素仅在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>


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});
});

注意:bgiframe是另一个插件,用于解决IE6的表单渲染问题。 - Sampson

1
一个不会在点击后消失的模态弹出框:

var popup = document.getElementById("popup");

function openPopup() {
  popup.style.display = "block";
}

function closePopup() {
  popup.style.display = "none";
}
/* Popup container */

.popup {
  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: rgba(0, 0, 0, 0.4);
  /* Black background with opacity */
}


/* Popup content */

.popup-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 */
}


/* Close button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<button onclick="openPopup()">Edit</button>
<div id="popup" class="popup">
  <div class="popup-content"><span class="close" onclick="closePopup()">&times;
</span>
    <form><label for="name">Name:</label><input type="text" id="name" name="name" value="shdhd"><br><br><label for="email">Email:</label><input type="email" id="email" name="email"><br><br><input type="submit" value="Save"></form>
  </div>
</div>


目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0

在Chrome上,HTML5有<dialog>元素。可以尝试使用它进行实验。


0

模态窗口的定义是需要用户在返回主窗口之前执行某些操作。因此,您要寻找的不是模态窗口,而是一个普通窗口。


1
他需要在模态弹出窗口上放置一个文本区域。 - Adam Kozlowski

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>


通常情况下,如果答案包含代码的意图和解决问题的原因,而不会引入其他问题,那么这些答案会更有帮助。 - DCCoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接