【发布时间】:2018-02-21 05:49:34
【问题描述】:
总的来说,我是编程和网络开发的新手。我正在制作个人网站。
我想将框放在一个有 4 列的网格中,类似于您在此处看到的:https://devpost.com/software/search?query=is%3Afeatured
这些框中的每一个都代表一个对象,我希望能够在框内显示一些数据,而当您单击框时,将其余数据显示在弹出对话框中。
我一直在玩 CSS 网格布局,现在它变得很流行(强烈推荐这个视频:https://www.youtube.com/watch?v=7kVeCqQCxlk)。
当我在包装器 div 中对一堆 div 元素进行硬编码时,具有 4 列的东西会起作用。但是,每当我在包含我的数据数组的包装器上使用 *ngFor 并将每次迭代中的数据输入到内部 div 元素时,网格布局都会被破坏,将所有内容放入一列中。
当我使用多个 div 元素(此处为 item2)手动输入数据时,它会按需要工作:
.wrapper {
margin-left: 1em;
margin-right: 1em;
display: grid;
grid-template-rows: auto;
grid-template-columns: repeat(4, 1fr);
grid-row-gap: 1em;
grid-column-gap: 1em;
}
<div class="wrapper">
<div class="item2" style="background-color: deepskyblue;">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
</div>
<div class="item2" style="background-color: deepskyblue;">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
</div>
<div class="item2" style="background-color: deepskyblue;">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
</div>
<div class="item2" style="background-color: deepskyblue;">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
</div>
<div class="item2" style="background-color: deepskyblue;">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
</div>
</div>
使用 *ngFor... 它运行数组“stickyNotes”的长度,但仅在每行下方堆叠框。
<div class="wrapper" *ngFor="let s of stickyNotes; let i = index" >
<div class="item2" style="background-color: deepskyblue;">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
<p>{{s.title}}</p>
</div>
</div>
我对此的破解是...在 *ngIf(i+1、i+2 等)上将这个 div 元素添加 4 次,其中 i 增加 1。这里的问题是,当新行开始时,CSS 中的 grid-row-gap 会被忽略。
<div class="item2" style="background-color: deepskyblue;" *ngIf="i < stickyNotes.length && i%4 === 0">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
<p>{{stickyNotes[i].title}}</p>
</div>
<div class="item2" style="background-color: deepskyblue;" *ngIf="i+1 < stickyNotes.length && i%4 === 0">
<img class="img-responsive img-rounded" src="assets/Logo/square_filler.png" alt="pic-test" >
<p>more text here.</p>
<p>{{stickyNotes[i+1].title}}</p>
</div>
<!--2 more times until i+3-->
我想知道是否有一种方法可以创建自定义可迭代指令,而不会破坏网格属性,也无需将 item2 类编码 4 次(或任何其他方法)。
我尝试过引导行和列属性,但是有 4 列,响应性不如网格布局。
任何帮助或建议将不胜感激!
【问题讨论】:
标签: html css angular typescript css-grid