简体   繁体   English

在第一个位置显示最后一个fadeIn div

[英]Show last fadeIn div on first position

I have 10 divs which were displayed in a random time. 我有10个div在随机时间显示。

How can I set the last shown div on top (rank first position) each time and not in the html order of the divs? 如何在每次设置最后显示的div(排名第一的位置)而不是div的html顺序?

Here is my code: 这是我的代码:

 var myVar; function showDiv(){ var random = Math.floor(Math.random() * $('.notification').length); $('.notification').eq(random).fadeIn(200).delay(3000).fadeOut(200); 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; display: none;/* hide initially so that fadIn() fadeOut() will work } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <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> 

Here is my fiddle: https://jsfiddle.net/gkq21ppt/3/ 这是我的小提琴: https//jsfiddle.net/gkq21ppt/3/

EDIT: Idea for a solution 编辑:解决方案的想法

A solution could be, to wrap the divs and set them column-reverse. 一个解决方案可能是,包装div并将它们设置为列反向。 And then add a JS-code which sets a sequential number as flex order number to every new faded in div. 然后添加一个JS代码,它将一个序列号设置为flex命令编号,每个新的faded in div。 But I have no idea how to do this, with my low JS skills. 但我不知道如何做到这一点,我的JS技能很低。

So the loop could look like: 所以循环看起来像:

  1. create number starting by 1 as variable 创建以1开头的数字作为变量
  2. create class width name eg "order-[number]" 创建类宽度名称,例如“order- [number]”
  3. in ".order-[number]" class set css property "order" to [number] 在“.order- [number]”类中将css属性“order”设置为[number]
  4. add this class to the loop before it is faded in 在淡入之前将此类添加到循环中
  5. remove this class after it is faded out 淡出后删除该类

Or? 要么?

You can try using .prependTo() 您可以尝试使用.prependTo()

What this will do (I think) is remove the active notification and add it back to the container, in the first position. 这将做什么(我认为)是删除活动通知并将其添加回容器,在第一个位置。 Because of this behaviour, flexbox shouldn't be necessary. 由于这种行为,不应该使用flexbox。

Note this changes the HTML structure. 请注意,这会更改HTML结构。

updated fiddle 更新小提琴

 var myVar; function showDiv() { var random = Math.floor(Math.random() * $('.notification').length); $('.notification').eq(random).prependTo('.container').fadeIn(200).delay(3000).fadeOut(200); 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; display: none; /* hide initially so that fadIn() fadeOut() will work */ } 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM