我有一个动画导航栏,我想在窄屏幕上将其从水平布局更改为垂直布局。
我尝试添加图像,但该帖子一直坚称我的代码格式不正确,所以我只是复制并粘贴了我的代码。第一部分是导航栏 html,第二部分是导航栏 CSS,第三部分是媒体查询 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;
}
}
<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>
使用flexbox。
flex-direction
属性允许您在水平和垂直布局之间轻松切换。对于窄屏幕,更改动画悬停元素的 top
;对于宽屏幕,更改其 left
。
nav {
display: flex;
flex-direction: column;
position: relative;
width: 120px;
background-color: #7dabd9;
border-radius: 8px;
margin: 0 auto;
}
@media (min-width: 1000px) {
nav {
flex-direction: row;
width: 430px;
height: 50px;
}
}
nav a {
line-height: 50px;
height: 100%;
font-size: 15px;
z-index: 1;
text-decoration: none;
text-transform: uppercase;
color: white;
padding-left: 10px;
box-sizing: border-box;
}
@media (min-width: 1000px) {
nav a {
text-align: center;
padding-left: 0;
}
}
nav .animation {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 50px;
z-index: 0;
transition: all .5s ease 0s;
border-radius: 8px;
}
@media (min-width: 1000px) {
nav .animation {
top: 0;
height: 100%;
}
}
a:nth-child(1):hover~.animation {
top: 0;
background-color: #1abc9c;
}
a:nth-child(2):hover~.animation {
top: 50px;
background-color: #e74c3c;
}
a:nth-child(3):hover~.animation {
top: 100px;
background-color: #3498db;
}
a:nth-child(4):hover~.animation {
top: 150px;
background-color: #9b59b6;
}
a:nth-child(5):hover~.animation {
top: 200px;
background-color: #e67e22;
}
@media (min-width: 1000px) {
a:nth-child(1) {
width: 90px;
}
a:nth-child(2) {
width: 110px;
}
a:nth-child(3) {
width: 110px;
}
a:nth-child(4) {
width: 120px;
}
a:nth-child(1):hover~.animation {
width: 90px;
left: 0;
}
a:nth-child(2):hover~.animation {
top: 0;
width: 110px;
left: 90px;
}
a:nth-child(3):hover~.animation {
top: 0;
width: 110px;
left: 200px;
}
a:nth-child(4):hover~.animation {
top: 0;
width: 120px;
left: 310px;
}
}
<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>
运行此代码片段后,使用完整页面链接来测试响应能力。