欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS+CSS繪制棋盤格的示例代碼

 更新時間:2022年05月06日 16:27:32   作者:海擁  
在這篇文章中,將為大家展示如何使用css Flex-box和一些JavaScript來設(shè)計棋盤。文中的示例代碼講解詳細,感興趣的可以了解一下

在這篇文章中,我將展示如何使用 css 和一些 JavaScript 來設(shè)計棋盤。

為此,你需要對 CSS Flex-box 和 nth-child() 屬性有基本的了解。

所以讓我們開始吧......

實現(xiàn)思路

我們將為每個偶數(shù)行添加 containerEven 類作為父容器,為每個奇數(shù)行添加 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>

現(xiàn)在剩下的就是使用 javascript 在相應(yīng)的 id 的幫助下在這些部分標簽內(nèi)動態(tài)附加元素。

 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 個數(shù)組來存儲每一行??數(shù)據(jù)。當我們得到數(shù)據(jù)再將其附加到相應(yīng)的容器 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>

效果展示

到此這篇關(guān)于JS+CSS繪制棋盤格的示例代碼的文章就介紹到這了,更多相關(guān)JS棋盤格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論