如何实现动画导航栏响应式?窄屏时切换为垂直布局
导航栏响应式垂直布局修复方案
问题描述
我有一个带动画效果的导航栏,希望在窄屏设备上从水平布局切换为垂直布局。尝试设置媒体查询并将float设为none后,未达到预期效果。现有代码如下:
原HTML代码
<nav> <a href="home.html">Home</a> <a href="about.html">About Us</a> <a href="products.html">Products</a> <a href="contact.html">Contact Us</a> <div class="animation start-home"></div> </nav>
原CSS代码
nav { margin: 27px auto 0; position: relative; width: 495px; height: 50px; background-color: #7dabd9; border-radius: 8px; font-size: 0; } nav a { line-height: 50px; height: 100%; font-size: 15px; display: inline-block; position: relative; z-index: 1; text-decoration: none; text-transform: uppercase; text-align: center; color: white; cursor: pointer; } nav .animation { position: absolute; height: 100%; top: 0; z-index: 0; transition: all .5s ease 0s; border-radius: 8px; } a:nth-child(1) { width: 100px; } a:nth-child(2) { width: 110px; } a:nth-child(3) { width: 100px; } a:nth-child(4) { width: 160px; } a:nth-child(5) { width: 120px; } nav .start-home, a:nth-child(1):hover~.animation { width: 100px; left: 0; background-color: #1abc9c; } nav .start-about, a:nth-child(2):hover~.animation { width: 110px; left: 100px; background-color: #e74c3c; } nav .start-blog, a:nth-child(3):hover~.animation { width: 100px; left: 210px; background-color: #3498db; } nav .start-portefolio, a:nth-child(4):hover~.animation { width: 160px; left: 310px; background-color: #9b59b6; } nav .start-contact, a:nth-child(5):hover~.animation { width: 120px; left: 470px; background-color: #e67e22; } main { box-sizing: border-box; color: black; background-color: #7dabd9; float: none; width: 100vw; clear: both; } aside { float: none; width: 100vw; } @media screen and (min-width:500px) and (max-width:799px) { header, footer { width: 100vw; } main { box-sizing: border-box; color: black; background-color: #7dabd9; float: none; width: 100vw; clear: both; } aside { display: none; float: none; width: 100vw; } @media screen and (min-width:50px) and (max-width:499px) { header, footer { width: 100vw; } main { box-sizing: border-box; color: black; background-color: #7dabd9; float: none; width: 90vw; clear: both; } aside { display: none; float: none; width: 90vw; } }
修改后的完整代码
HTML(无需修改)
<nav> <a href="home.html">Home</a> <a href="about.html">About Us</a> <a href="products.html">Products</a> <a href="contact.html">Contact Us</a> <div class="animation start-home"></div> </nav>
CSS
nav { margin: 27px auto 0; position: relative; width: 495px; height: 50px; background-color: #7dabd9; border-radius: 8px; font-size: 0; } nav a { line-height: 50px; height: 100%; font-size: 15px; display: inline-block; position: relative; z-index: 1; text-decoration: none; text-transform: uppercase; text-align: center; color: white; cursor: pointer; } nav .animation { position: absolute; height: 100%; top: 0; z-index: 0; transition: all .5s ease 0s; border-radius: 8px; } a:nth-child(1) { width: 100px; } a:nth-child(2) { width: 110px; } a:nth-child(3) { width: 100px; } a:nth-child(4) { width: 160px; } nav .start-home, a:nth-child(1):hover~.animation { width: 100px; left: 0; background-color: #1abc9c; } nav .start-about, a:nth-child(2):hover~.animation { width: 110px; left: 100px; background-color: #e74c3c; } nav .start-blog, a:nth-child(3):hover~.animation { width: 100px; left: 210px; background-color: #3498db; } nav .start-portefolio, a:nth-child(4):hover~.animation { width: 160px; left: 310px; background-color: #9b59b6; } main { box-sizing: border-box; color: black; background-color: #7dabd9; float: none; width: 100vw; clear: both; } aside { float: none; width: 100vw; } /* 修复媒体查询嵌套问题,并添加导航栏垂直布局样式 */ @media screen and (min-width:500px) and (max-width:799px) { header, footer { width: 100vw; } main { box-sizing: border-box; color: black; background-color: #7dabd9; float: none; width: 100vw; clear: both; } aside { display: none; float: none; width: 100vw; } } @media screen and (max-width:499px) { header, footer { width: 100vw; } main { box-sizing: border-box; color: black; background-color: #7dabd9; float: none; width: 90vw; clear: both; } aside { display: none; float: none; width: 90vw; } /* 导航栏垂直布局核心修改 */ nav { width: 90vw; height: auto; font-size: 15px; } nav a { display: block; width: 100%; height: 50px; } /* 重置每个链接的单独宽度 */ a:nth-child(1), a:nth-child(2), a:nth-child(3), a:nth-child(4) { width: 100%; } /* 调整动画元素的定位方式 */ nav .animation { width: 100%; height: 50px; left: 0; } nav .start-home, a:nth-child(1):hover~.animation { top: 0; width: 100%; } nav .start-about, a:nth-child(2):hover~.animation { top: 50px; width: 100%; } nav .start-blog, a:nth-child(3):hover~.animation { top: 100px; width: 100%; } nav .start-portefolio, a:nth-child(4):hover~.animation { top: 150px; width: 100%; } }
关键修改说明
- 修复媒体查询嵌套错误:原代码将小屏媒体查询嵌套在中屏查询内,导致样式无法生效,已拆分为独立的媒体查询。
- 导航容器自适应:窄屏时将
nav的固定宽度改为90vw,高度设为auto以适应垂直布局的高度。 - 链接布局改为垂直:将
nav a的display从inline-block改为block,并设置width:100%,让每个链接单独占一行。 - 动画元素适配垂直布局:将动画元素的宽度改为
100%,高度固定为单个链接的高度(50px),并将定位方式从left改为top,根据链接位置设置对应的top值。 - 重置链接单独宽度:取消窄屏下每个链接的固定宽度设置,统一为100%。
内容的提问来源于stack exchange,提问作者user3076395
相关产品推荐
相关产品推荐

