gpt4 book ai didi

javascript - 如何将下拉菜单与按钮对齐

转载 作者:行者123 更新时间:2023-12-05 04:29:48 24 4
gpt4 key购买 nike

我有一个带有 html、css 和一些 js 的下拉菜单。它工作正常,我还添加了一些动画。现在我想在点击我按钮下对齐它!我一直在堆栈上环顾四周,尝试使用 float 和 align,但我做不到。抱歉,我是新手,有人可以帮助我吗?

感谢任何回复,谢谢。

function myFunction() {
var x = document.getElementById("Demo");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className += " w3-hide";
setTimeout(function(){
x.className = x.className.replace(" w3-show", "");
x.className = x.className.replace(" w3-hide", "");

},500)
}
}
/*Items menu*/
.user_menu_item {
display: flex;
align-items: center;
}

.user_menu_item:hover {
background: whitesmoke;
border-left: 4px solid blue;
transition: 0.3s;
}

.user_menu_item:not(:hover) {
transition: 0.3s;
}

.user_menu_item > a {
display: flex;
align-items: center;
padding: 12px 10px 12px 10px;
width: 100%;
font-size: 14px;
color: #75777D;
}

.w3-container {
position: absolute;
top: 15%;
}

.w3-dropdown-click {
display: flex;
position: relative;
}

.w3-dropdown-content {
display: none;
background-color: whitesmoke;
min-width: 160px;
overflow: hidden;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
position:relative;

animation:animateFromBottom 0.6s
}

@keyframes animateFromBottom {
from{bottom:-50px;opacity:0} to{bottom:0;opacity:1}
}

@keyframes animateToBottom {
from{bottom:0;opacity:1} to{bottom:-50px;opacity:0}
}

.w3-bar-block .w3-bar-item {
width:100%;
display:block;
padding:8px 16px;
text-align:left;
border:none;
white-space:normal;
float:none;
outline:0
}

.w3-show-block,.w3-show {
display:block!important
}

.w3-dropdown-content.w3-hide {
animation:animateToBottom 0.6s
}

.w3-button {
float: right;
}
<button onclick="myFunction()" class="w3-button">Click Me!</button> 

<div class="w3-container">
<div class="w3-dropdown-click">


<div id="Demo" class="w3-dropdown-content w3-bar-block w3-border">
<div class="user_menu_item">
<a href="/account">
<span class="link_text">Dashboard</span>
</a>
</div>

<div class="user_menu_item">
<a href="ordini">
<span class="link_text">I miei ordini</span>
</a>
</div>

<div class="user_menu_item">
<a href="libreria">
<span class="link_text">Downloads</span>
</a>
</div>

<div class="user_menu_item">
<a href="impostazioni">
<span class="link_text">Impostazioni</span>
</a>
</div>

<div class="user_menu_item">
<a href="wp-login.php?action=logout">
<span class="link_text">Logout</span>
</a>
</div>
</div>

</div>
</div>

最佳答案

您可以创建一个容器 div 以将菜单和按钮本身放入其中,然后像您对菜单所做的那样为该容器提供绝对定位属性。你必须调整你的菜单位置,所以当它出现时它不会与按钮重叠,但我在代码中处理了这一点。此外,如果您希望按钮和菜单位于绝对右侧而不是偏移和溢出屏幕右侧,则需要修改菜单本身的位置。

参见:https://developer.mozilla.org/pt-BR/docs/Web/CSS/position

function myFunction() {
var x = document.getElementById("Demo");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className += " w3-hide";
setTimeout(function() {
x.className = x.className.replace(" w3-show", "");
x.className = x.className.replace(" w3-hide", "");

}, 500)
}
}
.container {
display: block;
margin: 0 0;
position: absolute;
top: 10px;
right: 0px;
}

/*Items menu*/
.user_menu_item {
display: flex;
align-items: center;
}

.user_menu_item:hover {
background: whitesmoke;
border-left: 4px solid blue;
transition: 0.3s;
}

.user_menu_item:not(:hover) {
transition: 0.3s;
}

.user_menu_item>a {
display: flex;
align-items: center;
padding: 12px 10px 12px 10px;
width: 100%;
font-size: 14px;
color: #75777D;
}

.w3-container {
position: absolute;
top: 25px;
right: 0px;
}

.w3-dropdown-click {
display: flex;
position: relative;
}

.w3-dropdown-content {
display: none;
background-color: whitesmoke;
min-width: 160px;
overflow: hidden;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
position: relative;

animation: animateFromBottom 0.6s
}

@keyframes animateFromBottom {
from {
bottom: -50px;
opacity: 0
}

to {
bottom: 0;
opacity: 1
}
}

@keyframes animateToBottom {
from {
bottom: 0;
opacity: 1
}

to {
bottom: -50px;
opacity: 0
}
}

.w3-bar-block .w3-bar-item {
width: 100%;
display: block;
padding: 8px 16px;
text-align: left;
border: none;
white-space: normal;
float: none;
outline: 0;
}

.w3-show-block,
.w3-show {
display: block !important
}

.w3-dropdown-content.w3-hide {
animation: animateToBottom 0.6s
}

.w3-button {
display: block;
}
<div class="container">
<button onclick="myFunction()" class="w3-button">Click Me!</button>

<div class="w3-container">
<div class="w3-dropdown-click">


<div id="Demo" class="w3-dropdown-content w3-bar-block w3-border">
<div class="user_menu_item">
<a href="/account">
<span class="link_text">Dashboard</span>
</a>
</div>

<div class="user_menu_item">
<a href="ordini">
<span class="link_text">I miei ordini</span>
</a>
</div>

<div class="user_menu_item">
<a href="libreria">
<span class="link_text">Downloads</span>
</a>
</div>

<div class="user_menu_item">
<a href="impostazioni">
<span class="link_text">Impostazioni</span>
</a>
</div>

<div class="user_menu_item">
<a href="wp-login.php?action=logout">
<span class="link_text">Logout</span>
</a>
</div>
</div>

</div>
</div>
</div>

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

24 4 0