我制作了一个css下拉菜单,它会淡入/退出子菜单。
但是,我想将效果更改为淡入并向上飞,就像这里: http://css3menu.com/paradox-amethyst.html
怎么可能这样做?
现在我有以下html和css:
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">Photos</a>
<ul>
<li><a href="#">Zoo</a></li>
<li><a href="#">Sky</a></li>
<li><a href="#">Pie</a>
<ul>
<li><a href="#">Zoo</a></li>
<li><a href="#">Sky</a></li>
<li><a href="#">Pie</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
position: relative;
background: none repeat scroll 0 0 #444;
}
ul.menu > li {
float: left;
min-width: 100px;
padding: 5px;
}
ul.menu a {
display: inline-block;
width: 100%;
color: white;
}
ul.menu > li > ul {
position: absolute;
top: 30px;
left: 0;
width: 100%;
visibility: hidden;
opacity: 0;
transition: all 0.2s;
}
ul.menu li ul > li > ul {
position: absolute;
top: 00px;
left: 100%;
width: 100%;
visibility: hidden;
opacity: 0;
transition: all 0.2s;
}
.menu li:hover {
background: maroon;
}
.menu li:hover > ul {
visibility: visible;
opacity: 1;
}
答案 0 :(得分:0)
您应该查看CSS transition属性。你把它放在元素上(不是它的悬停状态),然后设置top / left / etc。您希望该元素从哪个属性开始(因此,您希望子菜单从低于主菜单栏开始)。在:hover
声明中,您将要转换的位置结束(因此对于您而言,这就在菜单栏下方)。
您还应该查看CSS 2D Tranforms,因为它们可以让您在不依赖于向元素添加position: absolute
的情况下移动内容(因为上/左/右动画是hacky以及W3C做事的方式是通过CSS动画,是transform
属性。
答案 1 :(得分:0)
嗯,你可以很容易地把它拉下来:
<ul>
,当你将鼠标悬停在他们的父母身上时,他们会出现,从顶部给他们一些空间,比如top:200px; 查看 demo here 或下面的摘录:
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
position: relative;
background: none repeat scroll 0 0 #444;
}
ul.menu > li {
float: left;
min-width: 100px;
padding: 5px;
}
ul.menu a {
display: inline-block;
width: 100%;
color: white;
}
ul.menu > li > ul {
position: absolute;
top: 100px;
/*this is the offset from the top, play around with it, pixel percentage what ever works best for you*/
left: 0;
width: 100%;
visibility: hidden;
opacity: 0;
transition: all 0.2s;
}
ul.menu li ul > li > ul {
position: absolute;
top: 100px;
/*this is the offset from the top, play around with it, pixel percentage what ever works best for you*/
left: 100%;
width: 100%;
visibility: hidden;
opacity: 0;
transition: all 0.2s;
}
.menu li:hover {
background: maroon;
}
/*the direct children*/
.menu li:hover > ul {
visibility: visible;
opacity: 1;
top: 100%;
}
/*the secondary children*/
.menu li li:hover > ul {
top: 0;
}
<ul class="menu">
<li><a href="#">Home</a>
</li>
<li><a href="#">Photos</a>
<ul>
<li><a href="#">Zoo</a>
</li>
<li><a href="#">Sky</a>
</li>
<li><a href="#">Pie</a>
<ul>
<li><a href="#">Zoo</a>
</li>
<li><a href="#">Sky</a>
</li>
<li><a href="#">Pie</a>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Contact</a>
</li>
</ul>