W3.CSSドロップダウン
W3.CSSドロップダウンクラス
W3.CSSは、次のドロップダウンクラスを提供します。
クラス | 定義 |
---|---|
w3-ドロップダウン-ホバー | ホバー可能なドロップダウン要素 |
w3-ドロップダウンコンテンツ | 表示するドロップダウン部分 |
w3-ドロップダウン-クリック | クリック可能なドロップダウン要素 |
ドロップダウン要素
w3-dropdown- hoverクラスは、ホバー可能なドロップダウン要素を定義します。
w3-dropdown-contentクラスは、表示されるドロップダウンコンテンツを定義します。
例
<div class="w3-dropdown-hover">
<button class="w3-button">Hover Over Me!</button>
<div class="w3-dropdown-content w3-bar-block w3-border">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#" class="w3-bar-item w3-button">Link 3</a>
</div>
</div>
ホバー可能な要素とドロップダウンコンテンツ要素はどちらも任意のHTML要素にすることができます。
上記の例では、ホバー要素は<button>であり、ドロップダウンコンテンツは<div>でした。
次の例では、ホバー要素は<p>であり、ドロップダウンコンテンツは<span>です。
例
<p class="w3-dropdown-hover">Hover over me!
<span class="w3-dropdown-content w3-green">Hello World!</span>
</p>
メニューのドロップダウン
w3- dropdown-hoverクラスは、ドロップダウンメニューを備えたナビゲーションバーを作成するのに最適です。
例
<div class="w3-bar w3-light-grey">
<a href="#" class="w3-bar-item
w3-button">Home</a>
<a href="#" class="w3-bar-item w3-button">Link
1</a>
<div class="w3-dropdown-hover">
<button
class="w3-button">Dropdown</button>
<div
class="w3-dropdown-content w3-bar-block w3-card-4">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#"
class="w3-bar-item w3-button">Link
2</a>
<a href="#" class="w3-bar-item
w3-button">Link 3</a>
</div>
</div>
</div>
注:ナビゲーションバーの詳細については、このチュートリアルの後半で学習します。
クリック可能なドロップダウン
w3-dropdown-clickクラスは、クリック可能なドロップダウン要素を作成します。
このクラスでは、ドロップダウンはJavaScriptによって開かれます。
例
<div class="w3-dropdown-click">
<button onclick="myFunction()" class="w3-button w3-black">Click Me!</button>
<div id="Demo" class="w3-dropdown-content
w3-bar-block w3-border">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#" class="w3-bar-item w3-button">Link 3</a>
</div>
</div>
<script>
function myFunction() {
var x = document.getElementById("Demo");
if (x.className.indexOf("w3-show")
== -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
</script>
画像のドロップダウン
画像の上にマウスを移動します。
例
<div class="w3-dropdown-hover">
<img src="img_snowtops.jpg" alt="Norway" style="width:20%">
<div
class="w3-dropdown-content" style="width:300px">
<img src="img_snowtops.jpg"
alt="Norway" style="width:100%">
</div>
</div>
カードのドロップダウン
以下の都市の1つにマウスを移動します。
ロンドンはイギリスの首都です。
イギリスで最も人口の多い都市であり、人口は900万人を超えます。
Tokyo is the capital city of Japan.
13 million inhabitants.
例
<div class="w3-dropdown-hover">London
<div class="w3-dropdown-content
w3-card-4" style="width:250px">
<img src="img_london.jpg"
alt="London" style="width:100%">
<div
class="w3-container">
<p>London is the
capital city of England.</p>
<p>It is the
most populous city in the UK.</p>
</div>
</div>
</div>
アニメーションドロップダウン
ドロップダウンコンテンツをフェード、ズーム、またはスライドするには、 w3-animate-クラスのいずれかを使用します。
例
<div class="w3-dropdown-click">
<button onclick="myFunction()"
class="w3-button">Click Me</button>
<div id="Demo"
class="w3-dropdown-content w3-bar-block w3-animate-zoom">
<a href="#"
class="w3-bar-item w3-button">Link
1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#"
class="w3-bar-item w3-button">Link 3</a>
</div>
</div>
右揃えのドロップダウン
w3-rightクラスを使用して、ドロップダウンを右にフロートさせます。CSSを使用してドロップダウンコンテンツを配置します(right:0は、ドロップダウンメニューを左から右ではなく右から左に移動します):
例
<div class="w3-dropdown-hover w3-right">
<button class="w3-button">Dropdown</button>
<div class="w3-dropdown-content
w3-bar-block w3-border" style="right:0">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#" class="w3-bar-item w3-button">Link 3</a>
</div>
</div>