w3-dropdown-hover クラスは、ホバー効果を持つドロップダウン要素を定義します。
w3-dropdown-content クラスは、表示するドロップダウン部分を定義します。
<div class="w3-dropdown-hover">
<button class="w3-btn w3-red">Hover Me!</button>
<div class="w3-dropdown-content w3-border">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
全ての HTML 要素を、ホバー効果を持つ要素やドロップダウン要素にすることができます。
前の例では、ホバー効果を持つ部分が <button> で、ドロップダウン部分が <div> になっています。
次の例では、ホバー効果を持つ部分が <p> 要素で、ドロップダウン部分が <span> です:
<div class="w3-dropdown-hover">
<p>Hover Me!
<span class="w3-dropdown-content w3-green">
Hello World!
</span>
</p>
</div>
w3-dropdown-hover クラスは、ドロップダウン・ナビゲーション・メニューに最適です。
ナビゲーションバーについての詳細は、後の章で学習します。
<ul class="w3-navbar w3-card-2 w3-light-grey">
<li><a href="#">Home</a></li>
<li><a href="#">Link 1</a></li>
<li class="w3-dropdown-hover">
<a href="#">Dropdown <i class="fa fa-caret-down"></i></a>
<div
class="w3-dropdown-content w3-white w3-card-4">
<a
href="#">Link 1</a>
<a href="#">Link
2</a>
<a href="#">Link 3</a>
</div>
</li>
</ul>
Try It Yourself ❯
w3-dropdown-click クラスは、ドロップダウンが JavaScript で開かれることを除き、 Ww3-dropdown-hover に同じです。
<div class="w3-dropdown-click">
<button onclick="myFunction()" class="w3-btn">Click Me</button>
<div id="Demo" class="w3-dropdown-content w3-card">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">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_fjords.jpg" alt="Norway" style="width:20%">
<div
class="w3-dropdown-content" style="width:300px">
<img src="img_fjords.jpg"
alt="Norway" style="width:100%">
</div>
</div>
"London"、"Tokyo" の上にマウスを移動してください:
London is the capital city of England.
It is the most populous city in the United Kingdom, with a metropolitan area of over 9 million inhabitants.
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-classes の何れかを使用します:
<div class="w3-dropdown-click">
<button onclick="myFunction()"
class="w3-btn">Click Me</button>
<div id="Demo"
class="w3-dropdown-content w3-animate-zoom">
<a href="#">Link
1</a>
<a href="#">Link 2</a>
<a
href="#">Link 3</a>
</div>
</div>
ドロップダウンを右揃えにするには w3-right を使用し、ドロップダウン・コンテンツを配置するには CSS を使用します (right:0 は、ドロップダウン・メニューの方向を、右から左だったものを左から右にします):
<div class="w3-dropdown-hover w3-right">
<button class="w3-btn w3-blue">Dropdown</button>
<div class="w3-dropdown-content w3-border" style="right:0">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>