W3.CSS 模态

模态是显示在当前页面顶部的对话框/弹出窗口:

×

模态标题

Hello World!

Go back to W3.CSS Modal to learn more!

Modal Footer Close


W3.CSS 模态类

W3.CSS 为模态窗口提供了以下类:

类 定义
w3-modal 模态容器
w3-modal-content 模态内容

创建模态框

w3-modal 类定义模态框的容器。

w3-modal-content 类定义模态内容。

模态内容可以是任何 HTML 元素(div、标题、段落、图像等)。

实例

<!-- Trigger/Open the Modal -->
<button onclick="document.getElementById('id01').style.display='block'"
class="w3-button">Open Modal</button>

<!-- The Modal -->
<div id="id01" class="w3-modal">
  <div class="w3-modal-content">
    <div class="w3-container">
      <span onclick="document.getElementById('id01').style.display='none'"
      class="w3-button w3-display-topright">&times;</span>
      <p>Some text in the Modal..</p>
      <p>Some text in the Modal..</p>
    </div>
  </div>
</div>
亲自试一试 »

打开一个模态框

使用任何 HTML 元素打开模态框。 但是,这通常是一个按钮或一个链接。

使用 document.getElementById() 方法添加 onclick 属性并指向模式的 ID(在我们的示例中为 id01)。


关闭模态框

要关闭模式,请将 w3-button 类与指向模式 ID (id01) 的 onclick 属性一起添加到元素。 您也可以通过在模态框外单击来关闭它(请参见下面的示例)。

提示: &times; 是关闭图标的首选 HTML 实体,而不是字母"x"。


模态页眉和页脚

使用 w3-container 类在模态内容中创建不同的部分:

×

Modal Header

Some text..

Some text..

Modal Footer

实例

<div id="id01" class="w3-modal">
  <div class="w3-modal-content">

    <header class="w3-container w3-teal">
      <span onclick="document.getElementById('id01').style.display='none'"
      class="w3-button w3-display-topright">&times;</span>
      <h2>Modal Header</h2>
    </header>

    <div class="w3-container">
      <p>Some text..</p>
      <p>Some text..</p>
    </div>

    <footer class="w3-container w3-teal">
      <p>Modal Footer</p>
    </footer>

  </div>
</div>
亲自试一试 »

模态卡片

要将模式显示为卡片,请将 w3-card-* 类之一添加到 w3-modal-content 容器:

×

Modal Header

Some text..

Some text..

Modal Footer

实例

<div class="w3-modal-content w3-card-4">
亲自试一试 »

动态模态框

使用任何 w3-animate-zoom|top|bottom|right|left 类从特定方向在模态中滑动:

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

×

Modal Header

Some text..

Some text..

Modal Footer

实例

<div class="w3-modal-content w3-animate-zoom">
<div class="w3-modal-content w3-animate-top">
<div class="w3-modal-content w3-animate-bottom">
<div class="w3-modal-content w3-animate-left">
<div class="w3-modal-content w3-animate-right">
<div class="w3-modal-content w3-animate-opacity">
亲自试一试 »

您还可以通过在 w3-modal 元素上设置 w3-animate-opacity 类来淡入模态的背景色:

实例

<div class="w3-modal w3-animate-opacity">
亲自试一试 »

模态图像

单击图像以全尺寸显示为模态:

Norway
×
Norway

实例

<img src="img_snowtops.jpg" onclick="document.getElementById('modal01').style.display='block'" class="w3-hover-opacity">

<div id="modal01" class="w3-modal w3-animate-zoom" onclick="this.style.display='none'">
  <img class="w3-modal-content" src="img_snowtops.jpg">
</div>
亲自试一试 »

模态图片库

单击图像以全尺寸显示:

×

实例

<div class="w3-row-padding">
  <div class="w3-container w3-third">
    <img src="img_snowtops.jpg" style="width:100%" onclick="onClick(this)">
  </div>
  <div class="w3-container w3-third">
    <img src="img_lights.jpg" style="width:100%" onclick="onClick(this)">
  </div>
  <div class="w3-container w3-third">
    <img src="img_mountains.jpg" style="width:100%" onclick="onClick(this)">
  </div>
</div>

<div id="modal01" class="w3-modal" onclick="this.style.display='none'">
  <img class="w3-modal-content" id="img01" style="width:100%">
</div>

<script>
function onClick(element) {
  document.getElementById("img01").src = element.src;
  document.getElementById("modal01").style.display = "block";
}
</script>
亲自试一试 »

模态登录表单

这个例子为登录创建了一个模态: