You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将CSS导航栏hover滑动效果适配到ul li层级结构

问题原因

原实现的核心逻辑依赖两个条件:

  1. 导航链接<a>是<nav>的直接子元素,和.animation元素属于同一父级下的兄弟节点
  2. 用a:nth-child(n)选择器匹配对应位置的导航,结合兄弟选择器~控制悬浮时.animation的位置
    你改用ul li嵌套结构后,<a>被包裹在li和ul两层元素内,原有选择器全部失效,且ul/li的默认样式会破坏导航横向布局。

修改方案

方案1:仅修改CSS(无需改HTML结构,要求浏览器支持:has()选择器,Chrome 105+、Firefox 121+、Safari 15.4+均支持)

完整修改后的CSS如下:

@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');
body {
    font-family: 'Open Sans', sans-serif;
    background: #2c3e50;
}
nav{
    position: relative;
    width: 590px;
    height: 50px;
    background: #34495e;
    border-radius: 8px;
    font-size: 0;
    box-shadow: 0 2px 3px 0 rgba(0,0,0,.1);
}
/* 新增:重置ul默认样式 */
nav ul {
    margin: 0;
    padding: 0;
    list-style: none;
    height: 100%;
    position: relative;
    z-index: 1;
}
/* 新增:设置li横向排列,对应原a的宽度 */
nav li {
    display: inline-block;
    height: 100%;
}
nav a{
    font-size: 15px;
    text-transform: uppercase;
    color: white;
    text-decoration: none;
    line-height: 50px;
    display: block;
    width: 100%;
    text-align: center;
}
nav .animation{
    position: absolute;
    height: 100%;
    top: 0;
    z-index: 0;
    background: #1abc9c;
    border-radius: 8px;
    transition: all .5s ease 0s;
}
/* 修改:匹配嵌套结构的li,调整选择器 */
nav li:nth-child(1){
    width: 100px;
}
nav .start-home, 
/* 用:has()匹配第一个li悬浮时的后续animation元素 */
nav ul:has(li:nth-child(1):hover) ~ .animation{
    width: 100px;
    left: 0;
}
nav li:nth-child(2){
    width: 110px;
}
nav ul:has(li:nth-child(2):hover) ~ .animation{
    width: 110px;
    left: 100px;
}
nav li:nth-child(3){
    width: 100px;
}
nav ul:has(li:nth-child(3):hover) ~ .animation{
    width: 100px;
    left: 210px;
}
nav li:nth-child(4){
    width: 160px;
}
nav ul:has(li:nth-child(4):hover) ~ .animation{
    width: 160px;
    left: 310px;
}
nav li:nth-child(5){
    width: 120px;
}
nav ul:has(li:nth-child(5):hover) ~ .animation{
    width: 120px;
    left: 470px;
}

方案2:微调HTML结构+修改CSS(兼容所有现代浏览器,无需依赖:has())

先调整HTML结构,把.animation元素放到ul内部的最后一位:

<nav>
  <ul>
    <li>  <a href="#">Home</a> </li>
    <li>  <a href="#">About</a> </li>
    <li>  <a href="#">Blog</a> </li>
    <li>  <a href="#">Portfolio</a> </li>
    <li>  <a href="#">Contact</a> </li>
    <!-- 将animation移到ul内部最后 -->
    <div class="animation start-home"></div>
  </ul>
</nav>

对应修改的CSS悬浮控制部分即可,其余公共样式和方案1一致:

/* 悬浮控制部分修改为 */
nav .start-home, 
nav li:nth-child(1):hover ~ .animation{
    width: 100px;
    left: 0;
}
nav li:nth-child(2):hover ~ .animation{
    width: 110px;
    left: 100px;
}
nav li:nth-child(3):hover ~ .animation{
    width: 100px;
    left: 210px;
}
nav li:nth-child(4):hover ~ .animation{
    width: 160px;
    left: 310px;
}
nav li:nth-child(5):hover ~ .animation{
    width: 120px;
    left: 470px;
}

内容的提问来源于stack exchange,提问作者user2090879

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 22:39:03