我的问题是,我想在点击事件上使用 fadein
来显示一个 div
并在一定时间后自动使用 fadeout
隐藏该 div
。
我已经尝试了一些代码,但是 fadeout
函数没有正常运行。以下是我的代码片段:
.alert-box {
position: absolute;
left: 0;
right: 0;
bottom: 0;
width: 100%;
display: none;
padding: 20px 0;
background-color: red;
color: #fff;
}
.close {
position: absolute;
right: 10px;
top: 10px;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>click me</button>
<div class="alert-box">
<span class="close">×</span>
<div class="content">
sample content
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('button').click(function() {
$('.alert-box').fadeIn('fast');
});
if ($('.alert-box').css('display') == 'block') {
$('.alert-box').delay(1000).fadeOut('fast');
}
});
</script>