欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

JS+CSS绘制棋盘格的示例代码

时间:2022-12-02 11:42:38|栏目:JavaScript代码|点击:

在这篇文章中,我将展示如何使用 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实现超级好看的鼠标小尾巴特效

栏    目:JavaScript代码

下一篇:全选复选框JavaScript编写小结(附代码)

本文标题:JS+CSS绘制棋盘格的示例代码

本文地址:http://www.codeinn.net/misctech/220357.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有