gpt4 book ai didi

javascript - 从不同行和列中的 Javascript 数组和 Bootstrap 生成动态卡片 HTML 卡片

转载 作者:行者123 更新时间:2023-11-27 23:53:07 24 4
gpt4 key购买 nike

我想创建我的团队部分,其中包含不止一行并且一行中有 5 张卡片。我想创建所有团队成员的数组并按照上述格式对齐。但我的问题是,当一行被 5 张卡片填充时,我无法让卡片在不同的行中对齐。

我通过为 3 行手动创建 3 个不同的数组来做到这一点。但我想使用单数组和单循环来完成。

const teamDataOne = [
{
img: "img/team/man.png",
name: "Bimal Timilsina",
position: "Web Designer",
facebook: "https://facebook.com/",
email: "mailto:someone@gmail.com"
},
{
img: "img/team/man.png",
name: "Bimal Timilsina",
position: "Web Designer",
facebook: "https://facebook.com/",
email: "mailto:someone@gmail.com"
},
{
img: "img/team/man.png",
name: "Bimal Timilsina",
position: "Web Designer",
facebook: "https://facebook.com/",
email: "mailto:someone@gmail.com"
},
{
img: "img/team/man.png",
name: "Bimal Timilsina",
position: "Web Designer",
facebook: "https://facebook.com/",
email: "mailto:someone@gmail.com"
},
{
img: "img/team/man.png",
name: "Bimal Timilsina",
position: "Web Designer",
facebook: "https://facebook.com/",
email: "mailto:someone@gmail.com"
}
];

const container = document.getElementById("teamRowOne");

teamDataOne.forEach(result => {
// Construct card content
const content = `
<div class="col-md-2 shadow p-3 mb-5 bg-white rounded ">
<img src="${result.img}" height="150" width="150" alt="name">
<h4>${result.name}</h4>
<h6>${result.position}</h6>
<div class="border-bottom"></div>
<a href="${
result.facebook
}" target="_blank" class="btn-social btn-facebook"><i class="fa fa-facebook"></i></a>
<a href="${
result.email
}" target="_blank" class="btn-social btn-email"><i class="fa fa-envelope"></i></a>
</div>
`;

// Append newyly created card element to the container
container.innerHTML += content;
});

这是我的 HTML 代码:

<div class="row justify-content-around wow zoomIn" id="teamRowOne">
</div>

我希望每行只对齐五张卡片。

最佳答案

有数千种方法可以做到这一点,我只想举一个使用模运算符的例子。你可以像这样在一个循环中完成:

// dont use row as a container anymore, take the parent element
var container = document.getElementById("row-container");
var content="";
teamData.forEach(function(result,i){
if(i == 0){
//add start row
content+= '<div class="row">'
}
// add content
content += '<div class="col....'

if(i!=0 && i%5 == 0){

// add end of row ,and start new row on every 5 elements
content += '</div><div class="row">'
}

});
// after looping dont forget to close the last row
content += '</div>'
container.innerHTML += content;

(它是可调整的伪代码,让您了解)

关于javascript - 从不同行和列中的 Javascript 数组和 Bootstrap 生成动态卡片 HTML 卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56413874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com