功能案例 -- 通过开关,白天模式、夜晚模式切换

思路

1. 就是在css设2个全局变量,用来控制文字和背景的颜色。并在css中需要切换黑白天的地方,使用这2个变量。

2. 每次点击开关, 通过 document.documentElement.style.setProperty('全局变量', '样式')

修改css全局变量 : 进而影响全局CSS样式,实现白天 - 黑夜的切换。

效果展示

代码展示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        :root {
            --default-bac-color: #fefefe;
            --default-text-color: #000;
        }

        body {
            background: var(--default-bac-color);
        }

        .box {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            height: 80vh;
        }

        h2 {
            margin-bottom: 100px;
            font-size: 40px;
            color: var(--default-text-color);
        }

        /* 开关 */
        .switch {
            position: relative;
            width: 60px;
            height: 34px;
        }

        .switch input {
            display: none;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            border-radius: 34px;
            background-color: #867b7b85;
            transition: .4s;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 26px;
            width: 26px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            border-radius: 50%;
            transition: .4s;
        }

        input:checked+.slider {
            background-color: rgb(78 84 88);
        }


        input:checked+.slider:before {
            transform: translateX(26px);
        }
    </style>
    <title>改变白天和黑夜</title>
</head>

<body>
    <div class="box">
        <h2>白天</h2>
        <label class="switch">
            <input type="checkbox" checked>
            <div class="slider"></div>
        </label>
    </div>
</body>
<script>

    window.onload = function () {
        addStyleAttrToRules()
    }

    /** 
     *  给带有黑白天相关属性的class样式规则,添加新的css属性
     */
    function addStyleAttrToRules() {
        // 获取样式表对象列表
        let styleSheets = document.styleSheets
        for (let i1 = 0; i1 < styleSheets.length; i1++) {
            let rules = styleSheets[i1].cssRules || styleSheets[i1].rules
            // 遍历样式表中的规则,例:a { width:100px}
            for (let i2 = 0; i2 < rules.length; i2++) {
                let { cssText } = rules[i2]
                if (cssText.includes('var(--default-bac-color)') || cssText.includes('var(--default-text-color)')) {
                    // 为规则添加新的css属性
                    rules[i2].style.setProperty('transition', 'all 2s')
                    console.log(rules[i2].style);
                }
            }

        }
    }


    let box_but = document.querySelector('.box-but')
    let h2 = document.querySelector('h2')
    let switch_ele = document.querySelector('.switch input[type="checkbox"]')
    let day = {
        daytime: '白天',
        night: "黑夜",
        textColor:{
            daytime:"#000",
            night:"#fff"
        },
        bacColor:{
            daytime:"#fefefe",
            night:"#1a1a1a"
        }
    }
    switch_ele.addEventListener('click', () => {
        if (switch_ele.checked) {
            // 更改css全局变量
            // 白天
            document.documentElement.style.setProperty('--default-bac-color', day.bacColor.daytime)
            document.documentElement.style.setProperty('--default-text-color', 
day.textColor.daytime)
            h2.innerText = day.daytime


        } else {
            // 黑夜
            document.documentElement.style.setProperty('--default-bac-color', 
day.bacColor.night)
            document.documentElement.style.setProperty('--default-text-color', 
day.textColor.night)
            h2.innerText = day.night
        }
    })




</script>

</html>

PS: 以上的过渡效果,我嫌麻烦,用js加的,但是一旦选择器过多了,执行速率变慢,。。。,所以还是采用css样式加。嗯。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

本郡主是喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值