问题是当不透明度设置为零时,下拉按钮仍然可以点击。我将不透明度设置为零,因为需要褪色效果。有没有替代方法来获取动画,以及内容,是否被jquery隐藏?
您可以在示例中看到当我们将鼠标悬停在按钮下方时,光标变为指针并且元素是可点击的。
尝试这样做但从未工作
$(".btn").on('click', function(e) {
e.stopPropagation();
var child = $(this).siblings();
if (!child.hasClass("visible")) {
child.animate({
'opacity': 1
}, 1000);
child.addClass("visible");
} else {
child.animate({
'opacity': 0
}, 1000);
child.removeClass("visible");
}
});
$(document).on('click', function(e) {
$(".visible").animate({
'opacity': 0
}, 1000);
$(".visible").removeClass("visible");
});
.btn {
outline: none;
border: none;
padding: 10px 20px;
cursor: pointer;
background-color: #eee;
color: #7b7b7b;
width: 150px;
box-shadow: 4px 4px 6px 0px rgba(0, 0, 0, .16);
font-weight: bold;
margin-bottom: 20px;
}
.dropdown {
position: relative;
display: inline;
}
.btn-dropdown {
padding: 0px;
margin: 0px;
list-style: none;
background-color: #fff;
position: absolute;
left: 0px;
top: 30px;
box-shadow: 4px 4px 6px 0px rgba(0, 0, 0, .16);
min-width: 200px;
border: 1px solid #ddd;
text-align: initial;
max-height: 210px;
overflow: auto;
opacity: 0;
z-index: 100;
}
.btn-dropdown li {
padding: 6px;
margin: 0px;
border-bottom: 1px solid #ddd;
cursor: pointer;
}
.btn-dropdown li:hover {
background-color: #ddd;
}
.btn-dropdown li:last-child {
border-bottom: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dropdown">
<button class="btn first">Select something</button>
<ul class="btn-dropdown">
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
</ul>
</div>
<div class="dropdown">
<button class="btn first">Select something</button>
<ul class="btn-dropdown">
<li>Black</li>
<li>Brown</li>
<li>Red</li>
<li>Orange</li>
</ul>
</div>
答案 0 :(得分:3)
问题是因为当您将opacity
设置为0
时,元素仍然在DOM中,并且仍然可以与之交互,尽管它们无法被看到 - 类似于{{1 }}
要解决此问题,您应该使用visibility: none
。您还可以使用display: none
和fadeToggle()
的组合简化逻辑,如下所示:
fadeOut()
$(".btn").on('click', function(e) {
e.stopPropagation();
var $dropdown = $(this).siblings().fadeToggle(); // toggle this dropdown
$('.dropdown .btn-dropdown').not($dropdown).fadeOut(); // hide other dropdowns
});
$(document).on('click', function(e) {
$('.dropdown .btn-dropdown').fadeOut(); // hide all dropdowns
});
.btn {
outline: none;
border: none;
padding: 10px 20px;
cursor: pointer;
background-color: #eee;
color: #7b7b7b;
width: 150px;
box-shadow: 4px 4px 6px 0px rgba(0, 0, 0, .16);
font-weight: bold;
margin-bottom: 20px;
}
.dropdown {
position: relative;
display: inline;
}
.btn-dropdown {
padding: 0px;
margin: 0px;
list-style: none;
background-color: #fff;
position: absolute;
left: 0px;
top: 30px;
box-shadow: 4px 4px 6px 0px rgba(0, 0, 0, .16);
min-width: 200px;
border: 1px solid #ddd;
text-align: initial;
max-height: 210px;
overflow: auto;
display: none;
z-index: 100;
}
.btn-dropdown li {
padding: 6px;
margin: 0px;
border-bottom: 1px solid #ddd;
cursor: pointer;
}
.btn-dropdown li:hover {
background-color: #ddd;
}
.btn-dropdown li:last-child {
border-bottom: none;
}