gpt4 book ai didi

html - 带有顶部三 Angular 形的下拉菜单

转载 作者:行者123 更新时间:2023-11-28 00:08:23 26 4
gpt4 key购买 nike

我想创建一个这样的下拉菜单:

http://www.howdyjeff.com/

但我无法将顶部三 Angular 形和下拉菜单水平居中(基于 li a)并且我遇到了一些 :hover 问题,因为下拉菜单 ul 逐渐消失。

我该怎么做?

JSFiddle:

https://jsfiddle.net/zw5rqzut/

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;
}

演示:居中下拉菜单和箭头:

http://jsfiddle.net/esjrhg5n/

$(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>

关于html - 带有顶部三 Angular 形的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55572073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com