lima-city: Webhosting, Domains und Cloud
1 Pluspunkt 0 Minuspunkte
Wie kann ich eine Css Animation für ein Html Select Dropdown erstellen?
von  

2 Antworten

0 Pluspunkte 0 Minuspunkte

Du kannst ein Select Element mit CSS stylen aber animieren kann man das nur begrenzt.

<style>
.styled-select {
  padding: 10px;
  border: 2px solid #ccc;
  border-radius: 5px;
  background: white;
  font-size: 16px;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.styled-select:hover, .styled-select:focus {
  border-color: #007bff;
  box-shadow: 0 0 5px rgba(0,123,255,0.5);
}
</style>

<select class="styled-select">
  <option>Option 1</option>
  <option>Option 2</option>
  <option>Option 3</option>
</select>
von (458 Punkte)  
0 Pluspunkte 0 Minuspunkte

Du kannst ein benutzerdefiniertes Dropdown mit div, ul, li u.s.w statt mit einem select erstellen. JSBin Demo

<style>
body {
  font-family: Arial, sans-serif;
  padding: 50px;
}

.dropdown {
  position: relative;
  display: inline-block;
  user-select: none;
}

.dropdown-toggle {
  padding: 10px 20px;
  background-color: #007BFF;
  color: white;
  cursor: pointer;
  border-radius: 8px;
  transition: background-color 0.3s;
}

.dropdown-toggle:hover {
  background-color: #0056b3;
}

.dropdown-menu {
  position: absolute;
  top: 110%;
  left: 0;
  background: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.3s ease, opacity 0.3s ease;
  pointer-events: none;
  list-style: none;     /* entfernt die Punkte */
  margin: 0;             /* entfernt äußeren Abstand */
  padding: 0;            /* entfernt inneren Einzug */
}

.dropdown-menu.open {
  max-height: 300px;
  opacity: 1;
  pointer-events: auto;
}

.dropdown-menu li {
  list-style: none;
  padding: 10px 20px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.dropdown-menu li:hover {
  background-color: #f1f1f1;
}
</style>

<div class="dropdown">
  <div class="dropdown-toggle">Menü öffnen</div>
  <ul class="dropdown-menu">
    <li>Option 1</li>
    <li>Option 2</li>
    <li>Option 3</li>
  </ul>
</div>

Mit Javascript kannst du das Dropdown steuern.

<script>
const toggle = document.querySelector('.dropdown-toggle');
const menu = document.querySelector('.dropdown-menu');

toggle.addEventListener('click', () => {
  menu.classList.toggle('open');
});

// Optional: Schließen beim Klick außerhalb
document.addEventListener('click', (e) => {
  if (!e.target.closest('.dropdown')) {
    menu.classList.remove('open');
  }
});
</script>
von (389 Punkte)