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

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

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

在這篇文章中,我將展示如何使用 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 的幫助下在這些部分標(biāo)簽內(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ù)。當(dāng)我們得到數(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)文章

  • 微信小程序仿知乎實現(xiàn)評論留言功能

    微信小程序仿知乎實現(xiàn)評論留言功能

    這篇文章主要為大家詳細(xì)介紹了微信小程序仿知乎實現(xiàn)評論留言功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • JS模式之單例模式基本用法

    JS模式之單例模式基本用法

    這篇文章主要介紹了JS模式之單例模式基本用法,實例分析了javascript單例模式的基本實現(xiàn)方法,需要的朋友可以參考下
    2015-06-06
  • bootstrap 日期控件 datepicker被彈出框dialog覆蓋的解決辦法

    bootstrap 日期控件 datepicker被彈出框dialog覆蓋的解決辦法

    這篇文章主要介紹了bootstrap 日期控件 datepicker被彈出框dialog覆蓋的解決辦法 ,本文給大家分享幾種解決方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • JS控制彈出新頁面窗口位置和大小的方法

    JS控制彈出新頁面窗口位置和大小的方法

    這篇文章主要介紹了JS控制彈出新頁面窗口位置和大小的方法,實例分析了open方法彈出窗口的使用技巧,需要的朋友可以參考下
    2015-03-03
  • 微信小程序分享功能之按鈕button 邊框隱藏和點擊隱藏

    微信小程序分享功能之按鈕button 邊框隱藏和點擊隱藏

    這篇文章主要介紹了微信小程序分享功能之按鈕button 邊框隱藏和點擊隱藏,需要的朋友可以參考下
    2018-06-06
  • layui select獲取自定義屬性方法

    layui select獲取自定義屬性方法

    今天小編就為大家分享一篇layui select獲取自定義屬性方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • BootStrap table刪除指定行的注意事項(筆記整理)

    BootStrap table刪除指定行的注意事項(筆記整理)

    在前端開發(fā)中遇到這樣的問題,對于table指定行的數(shù)據(jù)進行刪除,花了好長時間才解決,今天小編抽時間給大家介紹BootStrap table刪除指定行的注意事項,需要的朋友參考下吧
    2017-02-02
  • 談?wù)凧avaScript類型系統(tǒng)之Math

    談?wù)凧avaScript類型系統(tǒng)之Math

    Math 對象并不像 Date 和 String 那樣是對象的類,因此沒有構(gòu)造函數(shù) Math(),像 Math.sin() 這樣的函數(shù)只是函數(shù),不是某個對象的方法。您無需創(chuàng)建它,通過把 Math 作為對象使用就可以調(diào)用其所有屬性和方法
    2016-01-01
  • JS實現(xiàn)監(jiān)控微信小程序的原理

    JS實現(xiàn)監(jiān)控微信小程序的原理

    這篇文章主要介紹了JS實現(xiàn)監(jiān)控微信小程序的原理,本文通過實例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2018-06-06
  • js兩個數(shù)組合并去重的方法大全

    js兩個數(shù)組合并去重的方法大全

    這篇文章主要給大家介紹了關(guān)于js兩個數(shù)組合并去重的相關(guān)資料,日常開發(fā)中經(jīng)常會用到的JS數(shù)組去重,文中將每種方法都給出了代碼示例,需要的朋友可以參考下
    2023-09-09

最新評論