[英]Show a random div of from a container one time
I want to do something like notification messages. 我想做一些通知消息。
First I implemented an infinite loop, but this is not working perfect, so I just want to show every div of the container one time in a random order. 首先,我实现了一个无限循环,但这并不是完美的,因此我只想以随机顺序一次显示容器的每个div。 When all divs of the container where shown, it should simply stop.
当显示容器的所有div时,都应停止。
But I don't know, how the set something like a list, which should be process randomly and then stop. 但是我不知道,如何设置类似列表的内容,应该随机处理然后停止。
Here is my CODE: 这是我的代码:
var myVar; function showDiv() { var random = Math.floor(Math.random() * $('.notification').length); $('.notification').eq(random).prependTo('.container').animate({ opacity: 1, // animate slideUp marginLeft: '30px' }, '100', 'swing').delay(3000).fadeOut(400); createRandomInterval(); } function createRandomInterval() { setTimeout(showDiv, 500 + Math.random() * 4000); } $(document).ready(function() { createRandomInterval(); });
.notification { width: 200px; height: 50px; background-color: yellow; border: 1px solid rgba(0, 0, 0, 0.2); margin-bottom: 5px; text-align: center; padding-top: 20px; opacity: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="notification">1</div> <div class="notification">2</div> <div class="notification">3</div> <div class="notification">4</div> <div class="notification">5</div> <div class="notification">6</div> <div class="notification">7</div> <div class="notification">8</div> <div class="notification">9</div> <div class="notification">10</div> </div>
Here is my fiddle: https://jsfiddle.net/brapbg1h/6/ 这是我的小提琴: https : //jsfiddle.net/brapbg1h/6/
Thank you so much! 非常感谢!
I think this is what You're looking for: 我认为这是您要寻找的:
Simply store your elements in an array and delete them from it once you've appended them 只需将元素存储在数组中,然后将其从数组中删除即可
var arr = $(".notification"); function display(){ let rand = Math.floor(Math.random() * arr.length) arr.eq(rand).prependTo("#result").animate({ opacity: 1, // animate slideUp marginLeft: '30px' }, '100', 'swing').delay(2000).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; opacity: 0 } .hidden { 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>
edit changed the snippet as requested by op 编辑根据op的要求更改了代码段
Maintain an array and then look for visited node 维护一个数组,然后寻找访问的节点
for ( i in $('.notification') ) {
if (arr.length === $('.notification').length) {
console.log("all are shown")
} else {
setInterval(showDiv, 500 + Math.random() * 4000);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.