معاينة الكود المباشر
قسم الاكواد
HTML Code
<div class="container">
<div class="dropdown">
<button class="dropdown-toggle" id="dropdown-button-id" aria-expanded="false" aria-haspopup="true" aria-controls="dropdown-menu-id">Dropdown</button>
<ul id="dropdown-menu-id" class="dropdown-menu" role="menu" aria-labelledby="dropdown-button-id">
<li role="menuitem"><a href="#">Item 1</a></li>
<li role="menuitem"><a href="#">Item 2</a></li>
<li role="menuitem"><a href="#">Item 3</a></li>
</ul>
</div>
</div>
CSS Code
/* === Variables for customization === */
:root {
--btn-bg: #0b0b0b;
--btn-color: #ffffff;
--btn-hover-bg: #2b2d33;
--btn-hover-color: #f1f1f1;
--btn-focus-ring: #444857;
--btn-active-bg: #23272a;
--btn-active-color: #e6e6e6;
--btn-disabled-bg: #b3b3b3;
--btn-disabled-color: #666666;
--dropdown-bg: #ffffff;
--dropdown-link-hover-bg: #f5f5f5;
--text-color: #444444;
}
/* === Global reset === */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 100%;
}
body {
align-content: center;
min-height: 75vh; /* for demo */
font-size: 1rem;
font-family: "Poppins", sans-serif;
}
/* === Utility styles === */
button {
outline: none;
border: none;
font-size: inherit;
font-family: inherit;
}
ul {
list-style: none;
}
.container {
display: flex;
justify-content: center;
align-items: center;
}
/* === Dropdown styles === */
.dropdown {
display: inline-block;
position: relative;
}
/* === Dropdown button styles === */
.dropdown-toggle {
-webkit-transition: background-color 0.3s, color 0.3s;
transition: background-color 0.3s, color 0.3s;
will-change: background-color, color;
cursor: pointer;
margin: .25rem;
border-radius: .25rem;
background-color: var(--btn-bg);
padding: 0.5rem 1rem;
color: var(--btn-color);
font-weight: 600;
display: flex;
align-items: center; /* Gère l'alignement de l'icon */
}
/* === Dropdown icon styles === */
.dropdown-toggle::after {
display: inline-block;
margin-left: .5rem;
content: "\F282";
color: inherit;
font-size: .875rem;
font-family: bootstrap-icons;
}
/* === Hover state === */
.dropdown-toggle:hover {
background-color: var(--btn-hover-bg);
color: var(--btn-hover-color);
}
/* === Focus-visible state === */
.dropdown-toggle:focus-visible {
outline: 3px solid var(--btn-focus-ring);
outline-offset: 2px;
}
/* === Active state === */
.dropdown-toggle:active {
background-color: var(--btn-active-bg);
color: var(--btn-active-color);
}
/* === Disabled state === */
.dropdown-toggle:disabled {
opacity: 0.7;
cursor: not-allowed;
background-color: var(--btn-disabled-bg);
color: var(--btn-disabled-color);
}
/* === Dropdown menu styles === */
.dropdown-menu {
-webkit-transform: translateY(-10px);
-webkit-transition: opacity 0.3s ease, -webkit-transform 0.3s ease, visibility 0.3s;
position: absolute;
top: calc(100% + .25rem);
left: .25rem;
transform: translateY(-10px);
visibility: hidden;
opacity: 0;
z-index: 1000;
transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s;
will-change: opacity, transform, visibility;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
border-radius: .25rem;
background-color: var(--dropdown-bg);
padding: .5rem 0;
min-width: 150px;
max-width: 175px;
line-height: 1.6;
}
/* === Dropdown open state === */
.dropdown.open .dropdown-menu {
-webkit-transform: translateY(0);
transform: translateY(0);
visibility: visible;
opacity: 1;
}
/* === Menu links styles === */
.dropdown-menu a {
-webkit-transition: background-color .15s;
display: block;
transition: background-color .15s;
will-change: background-color;
padding: .5rem 1.25rem;
color: var(--text-color);
font-weight: 500;
font-size: .875rem;
text-decoration: none;
}
.dropdown-menu a:hover {
background-color: var(--dropdown-link-hover-bg);
}
/* === Animation for dropdown links === */
.dropdown.open .dropdown-menu li {
-webkit-transform: translateY(10px);
-webkit-animation: fadeIn 0.3s ease forwards;
transform: translateY(10px);
opacity: 0;
animation: fadeIn 0.3s ease forwards;
}
@keyframes fadeIn {
to {
transform: translateY(0);
opacity: 1;
}
}
@-webkit-keyframes fadeIn {
to {
-webkit-transform: translateY(0);
opacity: 1;
}
}
JS Code
document.addEventListener("DOMContentLoaded", () => {
const dropdowns = document.querySelectorAll(".dropdown");
document.addEventListener("click", (event) => {
let clickedDropdown = null;
dropdowns.forEach((dropdown) => {
const toggle = dropdown.querySelector(".dropdown-toggle");
if (!dropdown.contains(event.target)) {
toggle.setAttribute("aria-expanded", false);
dropdown.classList.remove("open");
} else if (event.target === toggle) {
clickedDropdown = dropdown;
}
});
if (clickedDropdown) {
const toggle = clickedDropdown.querySelector(".dropdown-toggle");
const menu = clickedDropdown.querySelector(".dropdown-menu");
const isExpanded = toggle.getAttribute("aria-expanded") === "true";
toggle.setAttribute("aria-expanded", !isExpanded);
clickedDropdown.classList.toggle("open", !isExpanded);
if (!isExpanded) {
menu.querySelectorAll("li").forEach((item, index) => {
item.style.animationDelay = `${index * 0.1}s`;
});
}
}
});
});
// EN:
// Select all elements with the class "dropdown" once the DOM is fully loaded.
// Add a global click event listener to the document.
// Variable to track the currently clicked dropdown.
// Iterate through all dropdown elements to handle open/close logic.
// If the click happened outside the current dropdown, close it.
// Update ARIA attribute to indicate it's collapsed.
// Remove the "open" class to close the dropdown.
// If the toggle button of the current dropdown was clicked, mark it as the active dropdown.
// If a dropdown toggle was clicked, toggle its open state.
// Toggle the ARIA attribute and the "open" class.
// Apply dynamic animation delay to each child element in the dropdown menu.
// Delay increases with the index for cascading effect.
// FR:
// Sélectionne tous les éléments avec la classe "dropdown" une fois que le DOM est entièrement chargé.
// Ajoute un écouteur d'événements global pour les clics sur le document.
// Variable pour suivre le dropdown actuellement cliqué.
// Parcourt tous les éléments dropdown pour gérer la logique d'ouverture/fermeture.
// Si le clic s'est produit en dehors du dropdown actuel, le fermer.
// Met à jour l'attribut ARIA pour indiquer qu'il est replié.
// Retire la classe "open" pour fermer le dropdown.
// Si le bouton toggle du dropdown actuel a été cliqué, le marquer comme dropdown actif.
// Si un bouton toggle de dropdown a été cliqué, basculer son état d'ouverture.
// Basculer l'attribut ARIA et la classe "open".
// Appliquer un délai d'animation dynamique à chaque élément enfant dans le menu déroulant.
// Le délai augmente avec l'index pour un effet en cascade.
المشاهدات : 78
اسم المورد
زر القائمة المنسدلة 3
الوصف
زر القائمة المنسدلة 3