モーダルは、現在のページの最上部に表示されるダイアログ・ボックス/ポップアップウィンドウです:
<!-- Trigger/Open the Modal -->
<button onclick="document.getElementById('id01').style.display='block'"
class="w3-btn">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-closebtn">×</span>
<p>Some text in the Modal..</p>
<p>Some text in the Modal..</p>
</div>
</div>
</div>
Try It Yourself ❯
"w3-modal" クラス
class="w3-modal" を持つ全ての HTML コンテナ (例えば <div>) をモーダルにすることができます。
"w3-modal-content" クラス
すべてのモーダル・コンテンツは、class="w3-modal-content" を持つ HTML コンテナに配置する必要があります。
モーダル・コンテンツは、全ての HTML 要素 (heading、paragraph、image、等) することができます。
モーダルを開く
モーダルを開くには、全ての HTML 要素を使用できますが、大抵はボタンまたはリンクを使用します。
onclick 属性を追加し、document.getElementById() メソッドを使用してモーダル id (この例では id01)をポイントして、"trigger" ボタンに一致するユニークな ID(id01)を指定します。
Closing a Modal
モーダルを閉じる
モーダルを閉じるには、モーダルの id (id01)を指す onclick 属性を持つ要素と一緒に w3-closebtn クラス を追加します。モーダルの外側をクリックしても閉じることができます(下の例を参照)
× (×) は、文字 "x" よりも閉じるボタンに適したアイコンの HTML エンティティです。
class="w3-modal-content" を持つ <div> 内で、モーダルに異なったセクションを作るためには w3-container クラスを使用します:
<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-closebtn">×</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>
Try It Yourself ❯
カードのようなモーダルを表示するには、w3-modal-content コンテナに w3-card-* クラスを 追加します:
指定の方向からモーダルへスライドインさせるために、w3-animate-zoom|top|bottom|right|left クラスの いずれかを使用します:
<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">
Try It Yourself ❯
また、モーダルの背景色をフェードインすることもできます(w3-modal):
フルサイズで表示するには画像をクリックしてください:
<img src="img_fjords.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_fjords.jpg">
</div>
Try It Yourself ❯
フルサイズで表示するには画像をクリックしてください:
<div class="w3-row-padding">
<div class="w3-container w3-third">
<img src="img_fjords.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>
Try It Yourself ❯
この例では、ログイン用のモーダルを作成します:
この例では、タブ付きコンテンツを持つモーダルを作成します:
上の例では、モーダルを閉じるためにボタンを使用しています。しかし、わずかな JavaScript を使用すれば、 モーダルボックスの外側をクリックしたときにモーダルを閉じることができるようになります:
// Get the modal
var modal = document.getElementById('id01');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target
== modal) {
modal.style.display = "none";
}
}
Try It Yourself ❯
この例では、"lightbox" を作成するために、モーダル内の画像のスライドショーを使用する方法を示しています:
チップ: スライドショーの詳細については、W3.CSS スライドショー の章をご覧ください。