<!DOCTYPE html>
<html>
<head>
	<title>弹窗示例</title>
	<style>
		.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 */
		}
<pre><code>	/* 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: #aaaaaa;
	  float: right;
	  font-size: 28px;
	  font-weight: bold;
	}

	.close:hover,
	.close:focus {
	  color: #000;
	  text-decoration: none;
	  cursor: pointer;
	}
&lt;/style&gt;
</code></pre>
</head>
<body>
<pre><code>&lt;h2&gt;示例弹窗&lt;/h2&gt;
&lt;!-- Trigger/Open The Modal --&gt;
&lt;button id=&quot;myBtn&quot;&gt;打开弹窗&lt;/button&gt;

&lt;!-- The Modal --&gt;
&lt;div id=&quot;myModal&quot; class=&quot;modal&quot;&gt;

	&lt;!-- Modal content --&gt;
	&lt;div class=&quot;modal-content&quot;&gt;
		&lt;span class=&quot;close&quot;&gt;&amp;times;&lt;/span&gt;
		&lt;p&gt;这是一个弹窗的示例内容。&lt;/p&gt;
	&lt;/div&gt;

&lt;/div&gt;

&lt;script&gt;
	// Get the modal
	var modal = document.getElementById(&quot;myModal&quot;);

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

	// Get the &lt;span&gt; element that closes the modal
	var span = document.getElementsByClassName(&quot;close&quot;)[0];

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

	// When the user clicks on &lt;span&gt; (x), close the modal
	span.onclick = function() {
	  modal.style.display = &quot;none&quot;;
	}

	// When the user clicks anywhere outside of the modal, close it
	window.onclick = function(event) {
	  if (event.target == modal) {
		modal.style.display = &quot;none&quot;;
	  }
	}
&lt;/script&gt;
</code></pre>
</body>
</html>
写一个html弹窗的代码

原文地址: http://www.cveoy.top/t/topic/bLFK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录