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

标签 javascript jquery css flexbox

我有 10 个随机显示的 div。

我怎样才能每次都将最后显示的 div 设置在顶部(排名第一的位置)而不是按照 div 的 html 顺序?

这是我的代码:

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>

这是我的 fiddle :https://jsfiddle.net/gkq21ppt/3/

编辑:解决方案的想法

一个解决方案可能是,包装 div 并将它们设置为反转列。然后添加一个 JS 代码,将序列号设置为 flex 订单号到每个新的淡入 div 中。 但我不知道如何做到这一点,因为我的 JS 技能不高。

所以循环看起来像:

  1. 创建以 1 开头的数字作为变量
  2. 创建类宽度名称,例如“订单-[编号]”
  3. 在“.order-[number]”类中将 css 属性“order”设置为 [number]
  4. 在淡入之前将此类添加到循环中
  5. 淡出后移除这个类

或者?

最佳答案

您可以尝试使用 .prependTo()

这将做什么(我认为)是删除事件通知并将其添加回容器中的第一个位置。由于这种行为,flexbox 应该不是必需的。

请注意,这会更改 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>

关于javascript - 在第一个位置显示最后一个 fadeIn div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48300788/

相关文章:

javascript - 创建 DOM 元素并将其附加到另一个元素的内容

javascript - 需要将长文本变量从 JavaScript 传递到 PHP

jquery - 使用 "overflow: scroll"时,翻页卡背面的内容在 Chrome 中显示在前面,而不是其他浏览器

html - 定位方案会影响子元素

javascript - 如何在 html5 数据列表中使用 angularjs ng-click

javascript - 在 jquery 函数中使用 PHP 脚本

javascript - 使用 jquery 创建图像 slider 时出现问题

jquery - Bootstrap 3 数据切换 Accordion

php - IE8 上@font-face 中的像素化字体问题

Html5 amp 故事转换为 mo4 视频