我想达到这样的效果:

enter image description here

为了实现这一点,我用一些测试数据制作了这个array.从这个数组中,我动态地创建了div,它包含了这个数组中的元素的数量.

我得到的不是什么.我遗漏了什么?这就是我得到的:

enter image description here

const items = [
  { picturePath: '../example.png', desc: 'Beschreibung 1', title: 'Titel 1' },
  { picturePath: '../example.png', desc: 'Beschreibung 1', title: 'Titel 2' },
  { picturePath: '../example.png', desc: 'Beschreibung 1', title: 'Titel 3' },
];

$(document).ready(function() {
  var $DivList = $('#div-list-Items');
  var zIndexCounter = 1000; // Startwert für den z-index

  $.each(items, function(index, item) {
    var $itemDiv = $('<div></div>').addClass('item');
    $itemDiv.css('z-index', zIndexCounter - index);

    if (index === 0) {
      $itemDiv.css('top', '50%');
    } else {
      var overlap = 10; // Die Pixelanzahl, um die jedes Element überlappen soll
      var previousItemTop = parseInt($DivList.children('.item').last().css('top'));
      var previousItemHeight = $DivList.children('.item').last().outerHeight();
      var newTopValue = previousItemTop + previousItemHeight - overlap;
      $itemDiv.css('top', newTopValue + 'px');
    }

    $DivList.append($itemDiv);
  });
});
#div-list-Items {
  position: relative;
  width: 100%;
  height: 600px;
}

.item {
  position: absolute;
  box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;
  border-radius: 8px;
  width: 80%;
  height: 100%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: none;
}

.section {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<section id="div-list" class="section center">
  <div id="div-list-Items"></div>
</section>

推荐答案

要执行所需的操作,可以使用item的索引在每个连续的.item div上添加一些margin-top.

在下面的工作示例中,请注意使用map()的‘index’参数递增地调整在循环中创建的元素的z-indexmargin-top.

const items = [
  { picturePath: '../example.png', desc: 'Beschreibung 1', title: 'Titel 1' },
  { picturePath: '../example.png', desc: 'Beschreibung 1', title: 'Titel 2' },
  { picturePath: '../example.png', desc: 'Beschreibung 1', title: 'Titel 3' }
];
const $container = $('#div-list-items');

jQuery($ => {
  const $items = items.map((item, i) => `<div class="item" style="z-index: ${1000 - i}; margin-top: ${50 * i}px;"></div>`);
  $container.append($items);
});
#div-list-items {
  position: relative;
  width: 100%;
  height: 600px;
}

.item {
  position: absolute;
  box-shadow: rgba(0, 0, 0, 0.19) 0px 10px 20px, rgba(0, 0, 0, 0.23) 0px 6px 6px;
  border-radius: 8px;
  width: 80%;
  height: 100%;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: none;
  background-color: #FFF;
}

.section {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<section id="div-list" class="section center">
  <div id="div-list-items"></div>
</section>

Javascript相关问答推荐

RxJS setTimeout操作符等效

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

提交表格后保留Web表格中的收件箱值?

fs. writeFile()vs fs.writeFile()vs fs.appendFile()

使用JavaScript重新排序行

Exceljs:我们在file.xlsx(...)&#中发现了一个问题'"" 39人;

你怎么看啦啦队的回应?

实现JS代码更改CSS元素

还原器未正确更新状态

在执行异步导入之前判断模块是否已导入()

如何修复我的数据表,以使stateSave正常工作?

如何将未排序的元素追加到数组的末尾?

同一类的所有div';S的模式窗口

处理app.param()中的多个参数

react -原生向量-图标笔划宽度

Clip-Path在网页浏览器(Mozilla、Edge、Chrome)上不能正常工作,但在预览版Visual Code Studio HTML、CSS、JS上却能很好地工作

Pevent触发material 用户界面数据网格中的自动保存

使用Java脚本替换字符串中的小文本格式hashtag

如果我将高度设置为其内容的100%,则在Java脚本中拖动以调整面板大小时会冻结

在JS/TS中构造RSA公钥