目录实现思路完整代码效果展示在这篇文章中,我将展示如何使用 CSS 和一些 javascript 来设计棋盘。 为此,你需要对 CSS Flex-box 和 nth-child()
在这篇文章中,我将展示如何使用 CSS 和一些 javascript 来设计棋盘。
为此,你需要对 CSS Flex-box 和 nth-child() 属性有基本的了解。
所以让我们开始吧......
我们将为每个偶数行添加 containerEven 类作为父容器,为每个奇数行添加 containerOdd,
下面显示了相同的 CSS
.containerEven>div:nth-child(odd) {
background-color: white;
}
.containerEven>div:nth-child(even) {
background-color: black;
}
.containerOdd>div:nth-child(odd) {
background-color: black;
}
.containerOdd>div:nth-child(even) {
background-color: white;
}
这是html部分
<div class="parent-class">
<section class="containerEven" id='container1'></section>
<section class="containerOdd" id='container2'></section>
<section class="containerEven" id='container3'></section>
<section class="containerOdd" id='container4'></section>
<section class="containerEven" id='container5'></section>
<section class="containerOdd" id='container6'></section>
<section class="containerEven" id='container7'></section>
<section class="containerOdd" id='container8'></section>
</div>
现在剩下的就是使用 javascript 在相应的 id 的帮助下在这些部分标签内动态附加元素。
var res1 = [], res2 = [], res3 = [], res4 = [], res5 = [], res6 = [], res7 = [], res8 = [];
for (i = 1; i <= 8; i++) {
res1 += `<div class="item"></div>`
res2 += `<div class="item"></div>`
res3 += `<div class="item"></div>`
res4 += `<div class="item"></div>`
res5 += `<div class="item"></div>`
res6 += `<div class="item"></div>`
res7 += `<div class="item"></div>`
res8 += `<div class="item"></div>`
}
document.getElementById(`container1`).innerHTML = res1;
document.getElementById(`container2`).innerHTML = res2;
document.getElementById(`container3`).innerHTML = res3;
document.getElementById(`container4`).innerHTML = res4;
document.getElementById(`container5`).innerHTML = res5;
document.getElementById(`container6`).innerHTML = res6;
document.getElementById(`container7`).innerHTML = res7;
document.getElementById(`container8`).innerHTML = res8;
在这里我们所做的是使用 8 个数组来存储每一行数据。当我们得到数据再将其附加到相应的容器 id,
下面是带有输出的完整代码
<style>
.parent-class {
border: 5px chocolate groove;
}
.containerEven,
.containerOdd {
display: flex;
background-color: dodgerblue;
}
.item {
background-color: #f1f1f1;
padding: 20px;
font-size: 30px;
flex: 1;
height: 50px;
text-align: center;
}
.containerEven>div:nth-child(odd) {
background-color: white;
}
.containerEven>div:nth-child(even) {
background-color: black;
}
.containerOdd>div:nth-child(odd) {
background-color: black;
}
.containerOdd>div:nth-child(even) {
background-color: white;
}
</style>
<div class="parent-class">
<section class="containerEven" id='container1'></section>
<section class="containerOdd" id='container2'></section>
<section class="containerEven" id='container3'></section>
<section class="containerOdd" id='container4'></section>
<section class="containerEven" id='container5'></section>
<section class="containerOdd" id='container6'></section>
<section class="containerEven" id='container7'></section>
<section class="containerOdd" id='container8'></section>
</div>
<script>
var res1 = [], res2 = [], res3 = [], res4 = [], res5 = [], res6 = [], res7 = [], res8 = [];
for (i = 1; i <= 8; i++) {
res1 += `<div class="item"></div>`
res2 += `<div class="item"></div>`
res3 += `<div class="item"></div>`
res4 += `<div class="item"></div>`
res5 += `<div class="item"></div>`
res6 += `<div class="item"></div>`
res7 += `<div class="item"></div>`
res8 += `<div class="item"></div>`
}
document.getElementById(`container1`).innerHTML = res1;
document.getElementById(`container2`).innerHTML = res2;
document.getElementById(`container3`).innerHTML = res3;
document.getElementById(`container4`).innerHTML = res4;
document.getElementById(`container5`).innerHTML = res5;
document.getElementById(`container6`).innerHTML = res6;
document.getElementById(`container7`).innerHTML = res7;
document.getElementById(`container8`).innerHTML = res8;
</script>
到此这篇关于js+CSS绘制棋盘格的示例代码的文章就介绍到这了,更多相关JS棋盘格内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: JS+CSS绘制棋盘格的示例代码
本文链接: https://lsjlt.com/news/148110.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0