当我按下body时,如何关闭动画,而不是使动画出现/消失的元素?
$('#btt').click(function() {
$('.box').slideToggle('slow');
});
.box {
width: 200px;
height: 300px;
background-color: #00000025;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btt">PressIt</button>
<div class="box"></div>
答案 0 :(得分:0)
您需要做两件事:
slideUp()
时使用body
.box
元素时,停止传播click事件,以便使用body
模拟e.stopPropagation();
点击
$('#btt').click(function(e) {
$('.box').slideToggle('slow');
e.stopPropagation();
});
$('body').click(function(){
$('.box').slideUp();
});
.box {
width: 200px;
height: 300px;
background-color: #00000025;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btt">PressIt</button>
<div class="box"></div>