我是新来的,对编码也很陌生--我曾涉猎过,但仅此而已。我已经6年没有接触过任何形式的代码了。
我试图复制一个网站进行练习,我有一个导航和一个下拉子导航。我所遇到的问题是如何让子导航下拉菜单进入到它所对应的链接下面。我会附上例子和我的代码。任何帮助是感激的。请随时纠正任何你认为合适的东西--我刚刚重新开始,宁可知道正确的方法,而不是继续任何不好的做法。我也可以用子导航做我想做的事,但这是用负px的左右元素,我不想这样做。
怎么看呢?:
我是如何得到它,到目前为止: 例1
我也想让箭头按照图示链接起来。有谁还知道如何正确的修改与宽度的?
我的html。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Arnold Clark | New & Used Cars</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="ac-header">
<a href="#"><img id="ac-logo" src="ac.png" alt="Arnold Clark logo" /></a>
<div id="nav">
<span>Find a car</span>
<div id="nav-Dropdown">
<p>
<div style="font-weight: bold; font-size: 14px;">We sell</div>
</p>
<ul>
<li><a href="#">Used cars</a></li>
<li><a href="#">New cars</a></li>
<li><a href="#">Nearly new cars</a></li>
<li><a href="#">Car finance</a></li>
<li><a href="#">Vans</a></li>
<li><a href="#">Motability</a></li>
</ul>
</div>
</div>
<div id="nav">
<span class="pointer">Find a van</span>
</div>
<div id="nav">
<span class="pointer">Find a dealer</span>
</div>
<div id="nav">
<span class="pointer">Rental</span>
</div>
<div id="nav">
<span class="pointer">Servicing</span>
</div>
<div id="nav">
<span class="pointer">Other</span>
<div id="nav-Dropdown">
<ul>
<div style="font-weight: bold;">
<li>About Arnold Clark</li>
</div>
</ul>
</div>
</div>
</div>
<div id="wrapper">
Content goes in here.
</div>
</body>
</html>
我的CSS
body {
background-color: #fafafa;
font-family: "Gotham A", "Gotham B", "Gotham", "Montserrat", Verdana, sans-serif;
font-size: 14px;
/* Removed the white space on either side of the container */
margin: 0;
padding: 0;
}
#wrapper {
margin-right: auto;
margin-left: auto;
max-width: 1200px;
padding: 15px;
}
#ac-header {
max-width: 100%;
background-color: #2d3737;
height: 63px;
border-bottom: 6px solid #ffde00;
padding-top: 10px;
padding-bottom: 10px;
}
#ac-logo {
object-fit: contain;
width: 285px;
height: 43px;
padding-left: 41px;
margin-top: 10px;
position: absolute;
}
#nav {
position: relative;
display: inline-block;
bottom: 15px;
left: 500px;
top: 15px;
}
#nav > span {
color: #fff;
font-size: 15px;
text-align: center;
display: inline-block;
padding: 8px 8px 8px 8px;
margin-left: 30px;
border-radius: 3px;
cursor: pointer;
}
#nav > span:hover {
color: #ffde00;
background-color: #282e2e;
border-radius: 4px 4px 4px 4px;
-moz-border-radius: 4px 4px 4px 4px;
-webkit-border-radius: 4px 4px 4px 4px;
border: 0px solid #000000;
}
#nav > #nav-Dropdown {
display: none;
position: absolute;
background-color: #fff;
width: 590px;
top: 100px;
padding: 12px 16px;
border-radius: 4px 4px 4px 4px;
-moz-border-radius: 4px 4px 4px 4px;
-webkit-border-radius: 4px 4px 4px 4px;
border: 0px solid #000000;
}
#nav > #nav-Dropdown > ul {
list-style-type: none;
padding: 0;
margin: 0;
font-size: 14px;
}
#nav > #nav-Dropdown > ul > li {
display: inline;
padding: 10px;
right: 10px;
}
#nav > #nav-Dropdown > ul > li > a {
text-decoration: none;
color: #000;
}
#nav:hover #nav-Dropdown {
background-color: #fff;
display: block;
top: 50px;
}
我想这就是你想要的。你想改变 top
的值 ul
时出现 #nav
是 :hover
换个地方吧 50px
到 58px
它应该像你的意图一样对齐 :)
#nav:hover #nav-Dropdown {
background-color: #fff;
display: block;
top: 58px;
}
body {
background-color: #fafafa;
font-family: "Gotham A", "Gotham B", "Gotham", "Montserrat", Verdana, sans-serif;
font-size: 14px;
/* Removed the white space on either side of the container */
margin: 0;
padding: 0;
}
#wrapper {
margin-right: auto;
margin-left: auto;
max-width: 1200px;
padding: 15px;
}
#ac-header {
max-width: 100%;
background-color: #2d3737;
height: 63px;
border-bottom: 6px solid #ffde00;
padding-top: 10px;
padding-bottom: 10px;
}
#ac-logo {
object-fit: contain;
width: 285px;
height: 43px;
padding-left: 41px;
margin-top: 10px;
position: absolute;
}
#nav {
position: relative;
display: inline-block;
bottom: 15px;
left: 500px;
top: 15px;
}
#nav > span {
color: #fff;
font-size: 15px;
text-align: center;
display: inline-block;
padding: 8px 8px 8px 8px;
margin-left: 30px;
border-radius: 3px;
cursor: pointer;
}
#nav > span:hover {
color: #ffde00;
background-color: #282e2e;
border-radius: 4px 4px 4px 4px;
-moz-border-radius: 4px 4px 4px 4px;
-webkit-border-radius: 4px 4px 4px 4px;
border: 0px solid #000000;
}
#nav-Dropdown {
display: none;
position: absolute;
background-color: #fff;
width: 590px;
top: 100px;
padding: 12px 16px;
border-radius: 4px 4px 4px 4px;
-moz-border-radius: 4px 4px 4px 4px;
-webkit-border-radius: 4px 4px 4px 4px;
border: 0px solid #000000;
}
#nav #nav-Dropdown ul {
list-style-type: none;
padding: 0;
font-size: 14px;
}
#nav > #nav-Dropdown > ul > li {
display: inline;
padding: 10px;
right: 10px;
}
#nav > #nav-Dropdown > ul > li > a {
text-decoration: none;
color: #000;
}
#nav:hover #nav-Dropdown {
background-color: #fff;
display: block;
top: 58px;
}
<div id="ac-header">
<a href="#"><img id="ac-logo" src="ac.png" alt="Arnold Clark logo" /></a>
<div id="nav">
<span>Find a car</span>
<div id="nav-Dropdown">
<p>
<div style="font-weight: bold; font-size: 14px;">We sell</div>
</p>
<ul class="subDropdown">
<li><a href="#">Used cars</a></li>
<li><a href="#">New cars</a></li>
<li><a href="#">Nearly new cars</a></li>
<li><a href="#">Car finance</a></li>
<li><a href="#">Vans</a></li>
<li><a href="#">Motability</a></li>
</ul>
</div>
</div>
<div id="nav">
<span class="pointer">Find a van</span>
</div>
<div id="nav">
<span class="pointer">Find a dealer</span>
</div>
<div id="nav">
<span class="pointer">Rental</span>
</div>
<div id="nav">
<span class="pointer">Servicing</span>
</div>
<div id="nav">
<span class="pointer">Other</span>
<div id="nav-Dropdown">
<ul>
<div style="font-weight: bold;">
<li>About Arnold Clark</li>
</div>
</ul>
</div>
</div>
</div>
<div id="wrapper">
Content goes in here.
</div>
所以,你要做的是,一个li元素有一个子元素,它的链接包在里面,为它的
var dropdown = document.querySelector('.dropdownParent');
dropdown.onclick = function() {
var target = document.getElementById(this.dataset.droptarget)
target.classList.toggle('active');
}
ul {
display: flex;
list-style: none;
justify-content: space-around;
}
.dropdown {
position: relative;
}
.dropdown #dropdownOne {
position: absolute;
left: 0;
top: 30px;
display: none;
opacity: 0;
transition: all 1s;
}
.dropdown #dropdownOne.active {
opacity: 1;
display: block;
}
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li class="dropdown">
<a class="dropdownParent" href="#" data-droptarget="dropdownOne">Item 3</a>
<div id="dropdownOne">
<a href="#">Dropdown Item</a>
<a href="#">Dropdown Item</a>
<a href="#">Dropdown Item</a>
</div>
</li>
<li>Item 4</li>
</ul>
下拉菜单。见代码。