兄弟选择器 ~ 的妙用

兄弟选择器 ~ 用来选择同级的元素,但将它和伪类选择器 :first-child :nth-last-child 结合起来用时,可以计算当前同级的元素的个数

<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style type="text/css">
            .btns {
                text-align: center;
            }
            ul {
                list-style: none;
                padding: 0;
                width: 300px;
                height: 300px;
                outline: 1px solid #ddd;
                margin: auto;
                font-size: 0;
                counter-reset: count;
            }
            li {
                counter-increment: count;
                background: red;
                outline: 1px solid blue;
                display: inline-block;
                position: relative;
                outline-offset: -1px;
            }
            li::before {
                content: counter(count);
                font-size: 16px;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
            }

            /* 一个元素 */
            li:only-child {
                width: 100%;
                height: 100%;
            }

            /* 二个元素 */
            li:first-child:nth-last-child(2),
            li:first-child:nth-last-child(2) ~ li {
                width: 50%;
                height: 50%;
            }
            li:first-child:nth-last-child(2) + li {
                margin-bottom: -50%;
            }

            /* 三个元素 */
            li:first-child:nth-last-child(3),
            li:first-child:nth-last-child(3) ~ li {
                width: 50%;
                height: 50%;
            }
            li:first-child:nth-last-child(3){
                margin-left: 25%;
            }

            /* 四个元素 */
            li:first-child:nth-last-child(4),
            li:first-child:nth-last-child(4) ~ li {
                width: 50%;
                height: 50%;
            }

            /* 五个元素 */
            li:first-child:nth-last-child(5),
            li:first-child:nth-last-child(5) ~ li {
                width: 100px;
                height: 100px;
                top: 50px;
            }
            li:first-child:nth-last-child(5) {
                margin-left: 50px;
            }

            /* 六个元素 */
            li:first-child:nth-last-child(6),
            li:first-child:nth-last-child(6) ~ li {
                width: 100px;
                height: 100px;
                top: 50px;
            }

            /* 七个元素 */
            li:first-child:nth-last-child(7),
            li:first-child:nth-last-child(7) ~ li {
                width: 100px;
                height: 100px;
            }
            li:first-child:nth-last-child(7) {
                margin-left: 100px;
                margin-right: 100px;
            }

            /* 八个元素 */
            li:first-child:nth-last-child(8),
            li:first-child:nth-last-child(8) ~ li {
                width: 100px;
                height: 100px;
            }
            li:first-child:nth-last-child(8) {
                margin-left: 50px;
            }

            /* 九个元素 */
            li:first-child:nth-last-child(9),
            li:first-child:nth-last-child(9) ~ li {
                width: 100px;
                height: 100px;
            }
        </style>
    </head>
    <body>
        <ul id="photo-group">
            <li></li>
        </ul>
        <div class="btns">
            <button id="add">增加</button>
            <button id="reduce">减少</button>
        </div>
        <script type="text/javascript">
            let group = document.getElementById('photo-group');
            let add = document.getElementById('add');
            let reduce = document.getElementById('reduce');
            let count = 1;
            add.addEventListener('click', () => {
                if (count >= 9) {
                    return;
                }
                let li = document.createElement('li');
                group.appendChild(li);
                count++;
            });
            reduce.addEventListener('click', () => {
                if (count <= 1) {
                    return;
                }
                let last = group.lastElementChild;
                group.removeChild(last);
                count--;
            });
        </script>
    </body>
</html>
Last Updated:
Contributors: af