JS+CSS绘制棋盘格的示例代码
短信预约 -IT技能 免费直播动态提醒
在这篇文章中,我将展示如何使用 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棋盘格内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341