您必须首先在结果div中设置
.notification
要隐藏
display:none
,然后在将每个选项添加到结果div后,只需将display设置为flex,然后将fade、delay和fadeOut链接起来。
请参见以下工作片段:
var arr = $(".notification");
function display(){
let rand = Math.floor(Math.random() * arr.length)
let notfi = arr.eq(rand);
$("#result").append(notfi);
notfi.css("display","flex").fadeIn(400).delay(3000).fadeOut(400);
arr = arr.not(":eq("+rand+")")
if(arr.length>0) createRandomInterval();
}
function createRandomInterval() {
setTimeout(display, 500 + Math.random() * 4000);
}
createRandomInterval()
.notification {
background-color: red;
display: flex;
justify-content: center;
align-items: center;
height: 50px;
width: 200px;
margin-bottom: 10px;
}
.hidden {
display: none;
}
#result .notification {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hidden">
<div class="notification">object 1</div>
<div class="notification">object 2</div>
<div class="notification">object 3</div>
<div class="notification">object 4</div>
</div>
<div id="result"></div>