带有顶部三角形的下拉菜单
dropdown menu with top triangle
我想创建一个这样的下拉菜单:
但我无法将顶部三角形和下拉菜单水平居中(基于 li a
)并且我遇到了一些 :hover
问题,因为下拉菜单 ul
逐渐消失.
我该怎么做?
JSFiddle:
html {
text-align: center;
}
nav {
width: 100%;
height: auto;
background-color: #000;
}
ul li {
display: inline-block;
position: relative;
margin: 1em
}
ul ul {
display: none;
position: absolute;
background-color: #000;
top: 50px;
}
ul ul:after {
position: absolute;
left: 50%;
top: -8px;
width: 0;
height: 0;
content: '';
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 20px solid #000000;
}
a {
color: #fff;
text-decoration: none;
}
li:hover ul {
display:block;
}
<nav>
<ul>
<li>
<a href="#">Dropdown1</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown2</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown3</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
</ul>
</nav>
要使箭头居中,您可以添加:
ul ul:after {
margin-left: -20px;
}
要修复下拉列表不保留的问题,请像这样提供足够的底部填充:
nav > ul > li > a {
padding-bottom: 50px;
}
演示: 居中下拉和箭头:
$(document).ready(function() {
$("nav > ul > li").mouseover(function() {
var the_width = $(this).find("a").width();
var child_width = $(this).find("ul").width();
var width = parseInt((child_width - the_width)/2);
$(this).find("ul").css('left', -width);
});
});
更新:以下非JS版本:
https://jsfiddle.net/2Ly1h6uz/
nav {
background-color: black;
font-family: sans-serif;
text-align: center;
}
nav ul,
nav li {
list-style: none;
padding: 0;
margin: 0;
}
nav a {
text-decoration: none;
color: white;
}
nav a:hover {
color: yellow;
}
nav>ul {
display: flex;
justify-content: center;
}
nav>ul>li {
position: relative;
}
nav>ul>li>a {
display: block;
padding: 10px 20px;
}
nav>ul>li>ul {
display: none;
white-space: nowrap;
position: absolute;
left: 50%;
transform: translateX(-50%);
padding-top: 15px;
}
nav>ul>li>ul>li>a {
display: block;
padding: 10px 20px;
background-color: black;
border-bottom: 1px solid darkgray;
}
nav>ul>li>ul:after {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 5px;
width: 0;
height: 0;
content: '';
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid black;
}
nav>ul>li:hover>ul {
display: block;
}
<nav>
<ul>
<li>
<a href="#">Dropdown1</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2 A Long One</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown2</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown3</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
</ul>
</nav>
要使三角形水平居中,只需使用 transform
,如下所示:
left: 50%; //as you did
transform: translateX(-50%);
我想创建一个这样的下拉菜单:
但我无法将顶部三角形和下拉菜单水平居中(基于 li a
)并且我遇到了一些 :hover
问题,因为下拉菜单 ul
逐渐消失.
我该怎么做?
JSFiddle:
html {
text-align: center;
}
nav {
width: 100%;
height: auto;
background-color: #000;
}
ul li {
display: inline-block;
position: relative;
margin: 1em
}
ul ul {
display: none;
position: absolute;
background-color: #000;
top: 50px;
}
ul ul:after {
position: absolute;
left: 50%;
top: -8px;
width: 0;
height: 0;
content: '';
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 20px solid #000000;
}
a {
color: #fff;
text-decoration: none;
}
li:hover ul {
display:block;
}
<nav>
<ul>
<li>
<a href="#">Dropdown1</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown2</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown3</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
</ul>
</nav>
要使箭头居中,您可以添加:
ul ul:after {
margin-left: -20px;
}
要修复下拉列表不保留的问题,请像这样提供足够的底部填充:
nav > ul > li > a {
padding-bottom: 50px;
}
演示: 居中下拉和箭头:
$(document).ready(function() {
$("nav > ul > li").mouseover(function() {
var the_width = $(this).find("a").width();
var child_width = $(this).find("ul").width();
var width = parseInt((child_width - the_width)/2);
$(this).find("ul").css('left', -width);
});
});
更新:以下非JS版本:
https://jsfiddle.net/2Ly1h6uz/
nav {
background-color: black;
font-family: sans-serif;
text-align: center;
}
nav ul,
nav li {
list-style: none;
padding: 0;
margin: 0;
}
nav a {
text-decoration: none;
color: white;
}
nav a:hover {
color: yellow;
}
nav>ul {
display: flex;
justify-content: center;
}
nav>ul>li {
position: relative;
}
nav>ul>li>a {
display: block;
padding: 10px 20px;
}
nav>ul>li>ul {
display: none;
white-space: nowrap;
position: absolute;
left: 50%;
transform: translateX(-50%);
padding-top: 15px;
}
nav>ul>li>ul>li>a {
display: block;
padding: 10px 20px;
background-color: black;
border-bottom: 1px solid darkgray;
}
nav>ul>li>ul:after {
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 5px;
width: 0;
height: 0;
content: '';
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid black;
}
nav>ul>li:hover>ul {
display: block;
}
<nav>
<ul>
<li>
<a href="#">Dropdown1</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2 A Long One</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown2</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
<li>
<a href="#">Dropdown3</a>
<ul>
<li><a href="#">Option1</a></li>
<li><a href="#">Option2</a></li>
<li><a href="#">Option3</a></li>
</ul>
</li>
</ul>
</nav>
要使三角形水平居中,只需使用 transform
,如下所示:
left: 50%; //as you did
transform: translateX(-50%);