Let's take a look at an example where the dropdown menu, named dm-example
, will be displayed.
The HTML structure closely resembles Bootstrap5 examples:
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul id="dm-example" class="dropdown-menu " aria-labelledby="dropdownMenuButton1 ">
<li><a class="dropdown-item " href="# ">Action</a></li>
<li><a class="dropdown-item " href="# ">Another action</a></li>
<li><a class="dropdown-item " href="# ">Something else here</a></li>
</ul>
</div>
When it comes to CSS animations:
#dm-example.show
- this sets the initial styles for the block;
#dm-example.show .dropdown-item
- this sets the initial styles for inner items;
dm-animation
- refers to the animation on the block;
item-animation
- refers to the animation on each item.
<!-- Animate dropdown -->
<style>
#dm-example.show {
position: fixed;
animation-name: dm-animation;
animation-duration: 2s;
}
#dm-example.show .dropdown-item {
margin-bottom: 0;
animation-name: item-animation;
animation-duration: 2s;
}
@keyframes dm-animation {
0% {
margin-top: 0px;
}
50% {
margin-top: 25px;
}
75% {
margin-top: 5px;
}
100% {
margin-top: 0px;
}
}
@keyframes item-animation {
0% {
margin-bottom: 0px;
}
50% {
margin-bottom: 25px;
}
75% {
margin-bottom: 5px;
}
100% {
margin-bottom: 0px;
}
}
</style>