gpt4 book ai didi

javascript - 将下拉内容与下拉按钮对齐

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:27 25 4
gpt4 key购买 nike

这几天我一直在努力解决这个问题。我有一个下拉按钮和一些下拉内容。它工作得很好。我遇到的问题是下拉内容与按钮不对齐。它位于按钮以外的任何位置,具体取决于页面大小的调整方式。

我想我做错了什么,aa 到目前为止我尝试的一切都没有正常工作。

这是我的代码:

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('#dropdown')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.navigator {
font-family: sans-serif;
background-color: #f4f4f4;
height: 80px;
width: 100%;
position: fixed;
}

.navigator #navigator-contents {
width: 85%;
text-align: center;
margin: 0 auto;
}

.navigator #navigator-contents .logo {
background-color: #f4f4f4;
float: left;
height: 80px;
line-height: 82px;
}

.navigator #navigator-contents .logo img {
vertical-align: middle;
}

.navigator #navigator-contents .logo h2 {
font-size: 1.40em;
display: inline;
vertical-align: middle;
margin: 0;
}

.navigator #navigator-contents .clock {
float: right;
height: 80px;
width: 100px;
line-height: 80px;
}

.navigator #navigator-contents .clock img {
vertical-align: middle;
}

.navigator #navigator-contents .clock h4 {
display: inline;
vertical-align: middle;
margin: 0;
margin-left: -1px;
}

.navigator #navigator-contents .dropdown {
width: 100px;
height: 80px;
display: inline-block;
line-height: 82px;
position: relative;
cursor: pointer;
outline: none;
float: right;
}

.navigator #navigator-contents .dropdown:hover {
background-color: #e5e5e5;
color: #000;
}

.navigator #navigator-contents .dropdown:focus {
background-color: #1a5dad;
color: #fff;
}

.navigator #navigator-contents .dropdown h4 {
display: inline;
vertical-align: middle;
}

.navigator #navigator-contents .dropdown-content {
border-bottom-right-radius: 7px;
border-bottom-left-radius: 7px;
background-color: #f4f4f4;
display: none;
position: absolute;
z-index: 1;
margin-top: 80px;
}

.navigator #navigator-contents .dropdown-content .drop-content {
display: inline-block;
padding: 30px;
float: left;
}

.navigator #navigator-contents .dropdown-content .drop-content span {
font-size: 16px;
display: inline;
vertical-align: middle;
margin: 0;
margin-left: -1px;
right: 5px;
}

.navigator #navigator-contents .dropdown-content .drop-content i {
font-size: 1.10em;
}

.navigator #navigator-contents .dropdown-content .drop-content:hover {
transition: all .2s;
background-color: #1a5dad;
color: #fff;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
}

.navigator #navigator-contents .show {
border-top: 2px solid #1a5dad;
display: block;
}
<div class="navigator">
<div id="navigator-contents">
<div class="clock">
<a href="#">
<h4>Time: </h4>
<h4>13:34</h4>
</a>
</div>
<div class="dropdown" id="dropdown" onclick="myFunction()">
<h4 id="dropdown">Home <i class="fa fa-caret-down" aria-hidden="true"></i></h4>
</div>
<div class="dropdown-content" id="myDropdown">
<a href="#">
<div class="drop-content">
<span>This</span>
</div>
</a>
<a href="#">
<div class="drop-content">
<span>Is</span>
</div>
</a>
<a href="#">
<div class="drop-content">
<span>Dropdown</span>
</div>
</a>
<a href="#">
<div class="drop-content">
<span>Content</span>
</div>
</a>
</div>
</div>
</div>

我想要实现的图片:Image

所以,我的问题是 - 如何将下拉内容与菜单按钮对齐并使其保持原样,即使它已调整大小?

最佳答案

position:relative 添加到.navigator #navigator-contents。并为 .navigator #navigator-contents 添加 right100px(.navigator #navigator-contents .clock 的宽度)。下拉内容

function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('#dropdown')) {

var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.navigator {
font-family: sans-serif;
background-color: #f4f4f4;
height: 80px;
width: 100%;
position: fixed;
}

.navigator #navigator-contents {
width: 85%;
text-align: center;
margin: 0 auto; position:relative
}

.navigator #navigator-contents .logo {
background-color: #f4f4f4;
float: left;
height: 80px;
line-height: 82px;
}

.navigator #navigator-contents .logo img {
vertical-align: middle;
}

.navigator #navigator-contents .logo h2 {
font-size: 1.40em;
display: inline;
vertical-align: middle;
margin: 0;
}

.navigator #navigator-contents .clock {
float: right;
height: 80px;
width: 100px;
line-height: 80px;
}

.navigator #navigator-contents .clock img {
vertical-align: middle;
}

.navigator #navigator-contents .clock h4 {
display: inline;
vertical-align: middle;
margin: 0;
margin-left: -1px;
}

.navigator #navigator-contents .dropdown {
width: 100px;
height: 80px;
display: inline-block;
line-height: 82px;
position: relative;
cursor: pointer;
outline: none;
float: right;
}

.navigator #navigator-contents .dropdown:hover {
background-color: #e5e5e5;
color: #000;
}

.navigator #navigator-contents .dropdown:focus {
background-color: #1a5dad;
color: #fff;
}

.navigator #navigator-contents .dropdown h4 {
display: inline;
vertical-align: middle;
}

.navigator #navigator-contents .dropdown-content {
border-bottom-right-radius: 7px;
border-bottom-left-radius: 7px;
background-color: #f4f4f4;
display: none;
position: absolute;
z-index: 1;
margin-top: 80px; right: 100px;
}

.navigator #navigator-contents .dropdown-content .drop-content {
display: inline-block;
padding: 30px;
float: left;
}

.navigator #navigator-contents .dropdown-content .drop-content span {
font-size: 16px;
display: inline;
vertical-align: middle;
margin: 0;
margin-left: -1px;
right: 5px;
}

.navigator #navigator-contents .dropdown-content .drop-content i {
font-size: 1.10em;
}

.navigator #navigator-contents .dropdown-content .drop-content:hover {
transition: all .2s;
background-color: #1a5dad;
color: #fff;
border-bottom-right-radius: 5px;
border-bottom-left-radius: 5px;
}

.navigator #navigator-contents .show {
border-top: 2px solid #1a5dad;
display: block;
}
<div class="navigator">
<div id="navigator-contents">
<div class="clock">
<a href="#">
<h4>Time: </h4>
<h4>13:34</h4>
</a>
</div>
<div class="dropdown" id="dropdown" onclick="myFunction()">
<h4 id="dropdown">Home <i class="fa fa-caret-down" aria-hidden="true"></i></h4>
</div>
<div class="dropdown-content" id="myDropdown">
<a href="#">
<div class="drop-content">
<span>This</span>
</div>
</a>
<a href="#">
<div class="drop-content">
<span>Is</span>
</div>
</a>
<a href="#">
<div class="drop-content">
<span>Dropdown</span>
</div>
</a>
<a href="#">
<div class="drop-content">
<span>Content</span>
</div>
</a>
</div>
</div>
</div>

关于javascript - 将下拉内容与下拉按钮对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45936952/

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