利用 overflow: hidden 容器配合 :hover 狀態改變內部元素位移,實現「文字向上滑出、新文字滑入」的動態選單效果,**零 JavaScript**,效能極佳。
<nav id="menu" class="menu" aria-label="主導航選單">
<ul>
<li><a href="#"><span>HOME</span>首頁</a></li>
<li><a href="#"><span>NEWS</span>新聞</a></li>
<li><a href="#"><span>ABOUT</span>關於</a></li>
<li><a href="#"><span>CONTACT</span>聯繫</a></li>
<li><a href="#"><span>PHOTO</span>相簿</a></li>
</ul>
</nav>
結構關鍵: <a> 內包含兩個文字節點:<span>(英文/主要標籤)與緊隨其後的中文文字。兩者視為「兩行」內容,透過位移切換顯示。
/* 1. 選單容器 */
.menu {
width: 500px;
margin: 50px auto;
border: 1px solid #ccc;
overflow: hidden; /* 關鍵:裁切溢出的文字 */
background: #fff;
border-radius: 4px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans TC", sans-serif;
}
.menu ul {
margin: 0; padding: 0;
list-style: none;
display: flex; /* 現代佈局:水平排列 */
}
.menu li {
flex: 1; /* 平均分配寬度 */
height: 50px; /* 固定高度 = 單行文字高度 × 2 */
line-height: 25px; /* 單行高度 */
text-align: center;
position: relative; /* 為絕對定位 span 參考 */
}
/* 3. 連結區塊:佔滿 li,裁切溢出 */
.menu a {
display: block;
width: 100%;
height: 100%;
overflow: hidden; /* 關鍵:隱藏未懸停時的第二行文字 */
color: #333;
text-decoration: none;
transition: padding-top 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94); /* 平滑滑動 */
}
/* 4. 英文標籤:絕對定位,初始隱藏在頂部上方 */
.menu span {
display: block;
height: 25px; /* 等於 line-height */
line-height: 25px;
margin-top: -25px; /* 初始位置:向上位移一行,隱藏在可視區外 */
color: #fff;
background: #2c3e50; /* 懸停時顯示的背景色 */
transition: margin-top 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
/* 5. 懸停狀態:連結下移露出中文,span 上移露出英文 */
.menu a:hover {
padding-top: 25px; /* 向下推 25px,中文被推出可視區,英文滑入 */
background: #2c3e50; /* 整體背景變色 */
color: #fff;
}
.menu a:hover span {
margin-top: 0; /* 英文標籤滑入可視區頂部 */
}
| 狀態 | <a> padding-top | <span> margin-top | 可視內容 |
|---|---|---|---|
| 預設 | 0 | -25px (隱藏於頂部上方) | 中文文字(首頁、新聞...) |
| 懸停 | 25px | 0 (滑入頂部) | 英文標籤(HOME、NEWS...)+ 深色背景 |
.menu-bidir a {
display: flex;
flex-direction: column;
height: 50px; /* 兩行總高 */
overflow: hidden;
justify-content: flex-start; /* 預設顯示第一行 */
transition: justify-content 0.3s ease;
}
.menu-bidir a:hover {
justify-content: flex-end; /* 懸停顯示第二行 */
}
.menu-bidir span { line-height: 25px; } /* 固定行高 */
HTML 結構不變,純 CSS 切換 justify-content 實現雙向切換,更語意化。
<a href="#">
<span class="icon" aria-hidden="true"><svg>...</svg></span>
<span class="label">首頁</span>
</a>
.menu-icon a { display: flex; flex-direction: column; align-items: center; }
.menu-icon .icon { margin-top: -50px; height: 50px; transition: margin-top 0.3s; }
.menu-icon .label { height: 50px; line-height: 50px; }
.menu-icon a:hover .icon { margin-top: 0; }
.menu-icon a:hover .label { margin-top: -50px; } /* 推出可視區 */
.menu-fade a {
position: relative;
height: 50px;
}
.menu-fade .text-default,
.menu-fade .text-hover {
position: absolute;
left: 0; right: 0; top: 50%;
transform: translateY(-50%);
transition: opacity 0.3s, transform 0.3s;
white-space: nowrap;
}
.menu-fade .text-hover { opacity: 0; transform: translateY(-50%) translateX(10px); }
.menu-fade a:hover .text-default { opacity: 0; transform: translateY(-50%) translateX(-10px); }
.menu-fade a:hover .text-hover { opacity: 1; transform: translateY(-50%) translateX(0); }
<a> 內,螢幕閱讀器會連續朗讀「HOME 首頁」,語意無損。.menu a:focus { /* 模擬 hover 樣式 */
padding-top: 25px;
background: #2c3e50;
color: #fff;
}
.menu a:focus span { margin-top: 0; }
@media (prefers-reduced-motion: reduce) 關閉動畫。
@media (prefers-reduced-motion: reduce) {
.menu a, .menu span { transition: none !important; }
}
transform、opacity、padding-top(非佈局重排屬性),可觸發 GPU 加速。:hover 修改 width/height/margin/left/top 觸發 Layout Reflow。will-change: transform, padding-top 提前建立合成層。這個經典的「滑動門」技巧,以極簡的 DOM 結構實現豐富的微互動,是 CSS 動畫威力的絕佳展示。理解其「容器裁切 + 子元素位移」的核心邏輯,即可衍生出無數變體,豐富介面細節體驗。