如何创建如下所示的菜单效果
目前,我的应用程序菜单如下所示,
HTML:
<nav id="nav">
<ul id="navigation">
<li><a href="#" class="first">Reports</a></li>
<li><a href="#">Analytics</a></li>
<li><a href="#">Dashboards</a></li>
<li><a href="#">Monitors »</a>
<ul>
<li><a href="#">Real time</a></li>
<li><a href="#">Real time monitoring</a></li>
</ul>
</li>
</ul>
</nav>
这是我对李的css,
#nav {
display: inline;
position:relative;
margin:0;
padding:0px;
}
ul#navigation {
position:relative;
float:left;
border-left:0px solid #c4dbe7;
list-style-type: none;
margin: 0px;
padding: 0px;
border-right:0px solid #c4dbe7;
}
ul#navigation li {
display:inline;
font-size:12px;
font-weight:bold;
margin:0;
padding:0;
float:left;
position:relative;
border-top:0px solid #c4dbe7;
border-bottom:0px solid #c4dbe7;
}
ul#navigation li a {
padding:15px 15px;
color:#858585;
/*text-shadow:1px 1px 0px #fff;*/
text-decoration:none;
display:inline-block;
border-right:0px solid #fff;
border-left:0px solid #C2C2C2;
border-top:0px solid #fff;
-webkit-transition:color 0.2s linear, background 0.2s linear;
-moz-transition:color 0.2s linear, background 0.2s linear;
-o-transition:color 0.2s linear, background 0.2s linear;
transition:color 0.2s linear, background 0.2s linear;
}
ul#navigation li a:hover {
background:#df191a;
color:#282828;
}
ul#navigation li a.first {
border-left: 0 none;
}
ul#navigation li a.last {
border-right: 0 none;
}
ul#navigation li:hover > a {
background:#df191a;
}
/* Drop-Down Navigation */
ul#navigation li:hover > ul
{
/*these 2 styles are very important,
being the ones which make the drop-down to appear on hover */
visibility:visible;
opacity:1;
}
ul#navigation ul, ul#navigation ul li ul {
list-style: none;
margin: 0;
padding: 0;
/*the next 2 styles are very important,
being the ones which make the drop-down to stay hidden */
visibility:hidden;
opacity:0;
position: absolute;
z-index: 99999;
width:180px;
background:#f8f8f8;
box-shadow:1px 1px 3px #ccc;
/* css3 transitions for smooth hover effect */
-webkit-transition:opacity 0.2s linear, visibility 0.2s linear;
-moz-transition:opacity 0.2s linear, visibility 0.2s linear;
-o-transition:opacity 0.2s linear, visibility 0.2s linear;
transition:opacity 0.2s linear, visibility 0.2s linear;
}
ul#navigation ul {
top: 47px;
left: 0px;
}
ul#navigation ul li ul {
top: 0;
left: 181px; /* strong related to width:180px; from above */
}
ul#navigation ul li {
clear:both;
width:100%;
border:0 none;
border-bottom:1px solid #c9c9c9;
}
答案 0 :(得分:1)
在任何子菜单的第一个li
上使用边框技术制作的伪元素“三角形”似乎就是答案。
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
a {
color:white;
text-decoration: none;
}
ul#navigation li {
display: inline-block;
background: #ccc;
vertical-align: top;
position: relative;
}
ul#navigation > li > ul {
position: absolute;
top:100%;
left:0;
display: none;
}
ul#navigation > li:hover > ul {
display: block;
}
ul#navigation > li > ul > li {
width:100%;
position: relative;
background: white;
color:grey;
border-bottom: 1px solid grey;
}
ul#navigation > li > ul > li:first-child:before {
position: absolute;
content:"";
bottom:100%;
left:25%;
transform:translateX(-50%);
border: solid transparent;
content:" ";
height: 0;
width: 0;
border-bottom-color: white;
border-width: 12px;
}
ul#navigation li a {
display: block;
height: 0 1rem;
line-height: 25px;
padding: 0.5rem;
}
ul#navigation > li > ul > li a {
color: grey;
white-space:nowrap;
}
<nav id="nav">
<ul id="navigation">
<li><a href="#" class="first">Reports</a>
</li>
<li><a href="#">Analytics</a>
<ul>
<li><a href="#">Real time</a>
</li>
<li><a href="#">Real time monitoring</a>
</li>
</ul>
</li>
<li><a href="#">Dashboards</a>
</li>
<li><a href="#">Monitors »</a>
<ul>
<li><a href="#">Real time</a>
</li>
<li><a href="#">Real time monitoring</a>
</li>
</ul>
</li>
</ul>
</nav>
答案 1 :(得分:0)
我不知道你的整个代码,但是,它是这样的:
ul#navigation:before {
bottom: 100%;
left: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(136, 183, 213, 0);
border-bottom-color: #cbcbcb;
border-width: 12px;
margin-left: -12px;
}
您只需要在菜单上定位三角形......