我正在嘗試用CSS實現一個下拉菜單。差不多完成了,但是我需要下拉列表下面的項目向下移動,這樣就有空間放下拉列表項目了。當我關閉下拉菜單時,我希望項目再次上升,就像下拉菜單顯示之前一樣。
以下是我使用的代碼:
function toggleDropdown(id) {
var dropdown = document.getElementById(id);
dropdown.style.display = dropdown.style.display === 'block' ? 'none' : 'block';
}
document.addEventListener('click', function(event) {
var dropdown = document.getElementById('introduccion-dropdown');
var trigger = document.querySelector('.dropdown');
if (!trigger.contains(event.target)) {
dropdown.style.display = 'none';
}
});
@import url('font-sets.css');
@import url('common.css');
/* Styles for the dropdown menu */
* {
list-style: none;
}
.dropdown-content {
display: none;
position: absolute;
min-width: 160px;
z-index: 1;
top: 70%;
left: 50%;
transform: translate(-50%, -50%);
left: 49%;
}
.dropdown-content li {
display: block;
}
.items-below-dropdown {
position: relative;
margin-top: 400px; /* Adjust the value based on the height of the dropdown menu */
}
/* Styles for the lesson and test cards */
.button-stack {
margin-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
.card {
background-color: var(--primary-color);
color: var(--text-color);
position: relative;
width: 700px;
height: 113px;
border-radius: 15px;
border: none;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
cursor: pointer;
transition: transform 0.2s;
}
.card:hover {
transform: scale(1.15);
}
.card::before {
content: "";
position: absolute;
top: 10%;
bottom: 10%;
left: 20%;
transform: translateX(-50%);
width: 2px;
background-color: var(--text-color);
}
.card .number,
.card .letter {
position: absolute;
top: 50%;
left: 10%;
transform: translate(-50%, -50%);
font-size: 48px;
font-weight: bold;
}
.card-content {
width: 55%;
padding: 0 20px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
}
.card-title {
font-size: 25px;
font-weight: bold;
margin-bottom: 10px;
}
.card-subtitle {
font-size: 18px;
color: var(--text-color);
}
.lesson-card {
width: 600px;
height: 90px;
background: var(--primary-color);
color: var(--text-color);
}
.test-card {
width: 600px;
height: 90px;
background: var(--accent-color);
color: var(--text-color);
}
<div class="button-stack">
<li class="dropdown">
<button type="submit" class="card" onclick="toggleDropdown('introduction-dropdown')">
<div class="card-content">
<h2 class="card-title">Introduccion a Haskell</h2>
</div>
<div class="card-divider"></div>
<span class="letter">1</span>
</button>
<ul class="dropdown-content" id="introduction-dropdown">
<li>
<form action="/home" method="post">
<input type="hidden" name="lesson_id" value="1">
<button type="submit" class="card lesson-card">
<div class="card-content">
<h2 class="card-title">Titulo de leccion</h2>
</div>
<div class="card-divider"></div>
<span class="number">1.1</span>
</button>
</form>
</li>
<li>
<form action="/home" method="post">
<input type="hidden" name="lesson_id" value="1">
<button type="submit" class="card lesson-card">
<div class="card-content">
<h2 class="card-title">Titulo de leccion</h2>
</div>
<div class="card-divider"></div>
<span class="number">1.2</span>
</button>
</form>
</li>
<li>
<form action="/home" method="post">
<input type="hidden" name="lesson_id" value="1">
<button type="submit" class="card lesson-card">
<div class="card-content">
<h2 class="card-title">Titulo de leccion</h2>
</div>
<div class="card-divider"></div>
<span class="number">1.3</span>
</button>
</form>
</li>
<li>
<form action="/home" method="post">
<input type="hidden" name="test_id" value="1">
<button type="submit" class="card test-card">
<div class="card-content">
<h2 class="card-title">Test</h2>
<h3 class="card-subtitle">Temas del test</h3>
</div>
<div class="card-divider"></div>
<span class="letter">1.A</span>
</button>
</form>
</li>
</ul>
</li>
<div class="items-below-dropdown">
<li>
<button type="submit" class="card">
<div class="card-content">
<h2 class="card-title">Funciones</h2>
</div>
<div class="card-divider"></div>
<span class="letter">2</span>
</button>
</li>
<li>
<button type="submit" class="card">
<div class="card-content">
<h2 class="card-title">Monadas</h2>
</div>
<div class="card-divider"></div>
<span class="letter">3</span>
</button>
</li>
<li>
<button type="submit" class="card">
<div class="card-content">
<h2 class="card-title">Functores</h2>
</div>
<div class="card-divider"></div>
<span class="letter">4</span>
</button>
</li>
</div>
</div>
試試這個。我剛剛刪除了所有的位置:絕對的東西和巨大的利潤頂部。什么都沒加,我只注釋掉了幾行CSS,我用/*注釋掉*/做了標記,這樣很容易找到。
function toggleDropdown(id) {
var dropdown = document.getElementById(id);
dropdown.style.display = dropdown.style.display === 'block' ? 'none' : 'block';
}
document.addEventListener('click', function(event) {
var dropdown = document.getElementById('introduccion-dropdown');
var trigger = document.querySelector('.dropdown');
if (!trigger.contains(event.target)) {
dropdown.style.display = 'none';
}
});
@import url('font-sets.css');
@import url('common.css');
/* Styles for the dropdown menu */
* {
list-style: none;
}
.dropdown-content {
display: none;
/*COMMENTED OUT*/
/*position: absolute;*/
min-width: 160px;
z-index: 1;
/*COMMENTED OUT*/
/*top: 70%;
left: 50%;
transform: translate(-50%, -50%);
left: 49%;*/
}
.dropdown-content li {
display: block;
}
.items-below-dropdown {
position: relative;
/*COMMENTED OUT*/
/*margin-top: 400px;*/ /* Adjust the value based on the height of the dropdown menu */
}
/* Styles for the lesson and test cards */
.button-stack {
margin-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
.card {
background-color: var(--primary-color);
color: var(--text-color);
position: relative;
width: 700px;
height: 113px;
border-radius: 15px;
border: none;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
cursor: pointer;
transition: transform 0.2s;
}
.card:hover {
transform: scale(1.15);
}
.card::before {
content: "";
position: absolute;
top: 10%;
bottom: 10%;
left: 20%;
transform: translateX(-50%);
width: 2px;
background-color: var(--text-color);
}
.card .number,
.card .letter {
/*COMMENTED OUT*/
/*position: absolute;
top: 50%;
left: 10%;
transform: translate(-50%, -50%);*/
font-size: 48px;
font-weight: bold;
}
.card-content {
width: 55%;
padding: 0 20px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
}
.card-title {
font-size: 25px;
font-weight: bold;
margin-bottom: 10px;
}
.card-subtitle {
font-size: 18px;
color: var(--text-color);
}
.lesson-card {
width: 600px;
height: 90px;
background: var(--primary-color);
color: var(--text-color);
}
.test-card {
width: 600px;
height: 90px;
background: var(--accent-color);
color: var(--text-color);
}
<div class="button-stack">
<li class="dropdown">
<button type="submit" class="card" onclick="toggleDropdown('introduction-dropdown')">
<div class="card-content">
<h2 class="card-title">Introduccion a Haskell</h2>
</div>
<div class="card-divider"></div>
<span class="letter">1</span>
</button>
<ul class="dropdown-content" id="introduction-dropdown">
<li>
<form action="/home" method="post">
<input type="hidden" name="lesson_id" value="1">
<button type="submit" class="card lesson-card">
<div class="card-content">
<h2 class="card-title">Titulo de leccion</h2>
</div>
<div class="card-divider"></div>
<span class="number">1.1</span>
</button>
</form>
</li>
<li>
<form action="/home" method="post">
<input type="hidden" name="lesson_id" value="1">
<button type="submit" class="card lesson-card">
<div class="card-content">
<h2 class="card-title">Titulo de leccion</h2>
</div>
<div class="card-divider"></div>
<span class="number">1.2</span>
</button>
</form>
</li>
<li>
<form action="/home" method="post">
<input type="hidden" name="lesson_id" value="1">
<button type="submit" class="card lesson-card">
<div class="card-content">
<h2 class="card-title">Titulo de leccion</h2>
</div>
<div class="card-divider"></div>
<span class="number">1.3</span>
</button>
</form>
</li>
<li>
<form action="/home" method="post">
<input type="hidden" name="test_id" value="1">
<button type="submit" class="card test-card">
<div class="card-content">
<h2 class="card-title">Test</h2>
<h3 class="card-subtitle">Temas del test</h3>
</div>
<div class="card-divider"></div>
<span class="letter">1.A</span>
</button>
</form>
</li>
</ul>
</li>
<div class="items-below-dropdown">
<li>
<button type="submit" class="card">
<div class="card-content">
<h2 class="card-title">Funciones</h2>
</div>
<div class="card-divider"></div>
<span class="letter">2</span>
</button>
</li>
<li>
<button type="submit" class="card">
<div class="card-content">
<h2 class="card-title">Monadas</h2>
</div>
<div class="card-divider"></div>
<span class="letter">3</span>
</button>
</li>
<li>
<button type="submit" class="card">
<div class="card-content">
<h2 class="card-title">Functores</h2>
</div>
<div class="card-divider"></div>
<span class="letter">4</span>
</button>
</li>
</div>
</div>