我过去几天一直试图解决这个问题。我有一个下拉按钮和一些下拉内容。它完美地运作。我遇到的问题是Dropdown内容没有与按钮对齐。除了按钮之外,它位于任何位置,具体取决于页面的大小调整方式。
我想我做错了什么,到目前为止我尝试的一切都没有正常工作。
这是我的代码:
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');
}
}
}
}
&#13;
.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;
}
&#13;
<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>
&#13;
我希望实现的目标:Image
所以,我的问题是 - 如何将下拉内容与菜单按钮对齐并使其保持在那里,即使它已调整大小?
答案 0 :(得分:1)
将position:relative
添加到.navigator #navigator-contents
。并为right
100px
值.navigator #navigator-contents .clock
(.navigator #navigator-contents .dropdown-content
的宽度)
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');
}
}
}
}
&#13;
.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;
}
&#13;
<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>
&#13;
答案 1 :(得分:0)
见下文。我让id myDropdown
成为身份dropdown
的孩子,以利用绝对定位。现在myDropdown
绝对位于右侧,值为零。
此外,我将myDropdown
更改为弹性框。
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');
}
}
}
}
&#13;
.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;
}
.navigator #navigator-contents .dropdown-content .drop-content {
padding: 30px;
}
.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: flex;
right: 0;
}
&#13;
<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 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>
</div>
&#13;