简体   繁体   English

如何将切换更改为图标单击(用于切换到暗模式)

[英]How to change toggle to icon click (for switching to dark mode)

I've dark mode enabled on the website.我在网站上启用了暗模式。 It currently has a toggle switch, which changes the layout from light to dark and vice versa.它目前有一个拨动开关,可以将布局从浅色更改为深色,反之亦然。

It is using the following code:它使用以下代码:

 const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]'); const currentTheme = localStorage.getItem('theme'); if (currentTheme) { document.documentElement.setAttribute('data-theme', currentTheme); if (currentTheme === 'dark') { toggleSwitch.checked = true; } } function switchTheme(e) { if (e.target.checked) { document.documentElement.setAttribute('data-theme', 'dark'); localStorage.setItem('theme', 'dark'); } else { document.documentElement.setAttribute('data-theme', 'light'); localStorage.setItem('theme', 'light'); } } toggleSwitch.addEventListener('change', switchTheme, false);
 <div class="theme-switch-wrapper"> <label class="theme-switch" for="checkbox"> <input type="checkbox" id="checkbox"/> <div class="slider round"></div> </label> <em>DARK</em> <strong>MODE</strong> </div>

Now I want to upgrade to an icon click.现在我想升级到一个图标点击。 For example, if there is light mode enabled, the icon for dark mode should be seen upon clicking it, the user will get dark mode on.例如,如果启用了浅色模式,点击后应该会看到深色模式的图标,用户将打开深色模式。 Same, if dark mode is enabled, the icon for the light mode to be displayed, and if the user clicks it, the light mode is activated.同样,如果启用了暗模式,则会显示亮模式的图标,如果用户单击它,则会激活亮模式。

Thanks for any help or suggestions.感谢您的任何帮助或建议。

Change the toggle switch to a button element with an icon inside.将切换开关更改为内部带有图标的button元素。

Then inside your click-handling code, you can switch out the src of the icon svg/image to show the other image.然后在您的点击处理代码中,您可以切换图标 svg/image 的src以显示另一个图像。

 const darkmodeIcon = "https://uxwing.com/wp-content/themes/uxwing/download/01-user_interface/dark-mode.png" const lightmodeIcon = "https://uxwing.com/wp-content/themes/uxwing/download/01-user_interface/light-mode.png" const icon = document.getElementById("darkmode-icon"); icon.src = lightmodeIcon function handleDarkModeToggle() { if (icon.src == darkmodeIcon) { icon.src = lightmodeIcon console.log("Toggled to Light Mode") } else { icon.src = darkmodeIcon console.log("Toggled to Dark Mode") } }
 button { background: white; border: none; } img { height: 50px; }
 <button onclick="handleDarkModeToggle()"> <img id="darkmode-icon"/> </button>

in the HTMl make a new image as a lable:在 HTMl 中制作一个新图像作为标签:

<label class="theme-switch" for="checkbox">
        <img width="30" id="switcher" src="">
        <input type="checkbox" id="checkbox" />
        <div class="slider round"></div>
</label>

then you can use the set attribute to switch the source那么就可以使用set属性来切换源了

<script>
    const switcher = document.querySelector("#switcher");
    switcher.setAttribute('src', 'https://vectorified.com/images/switch-button-icon-19.png');

    const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]');
    const currentTheme = localStorage.getItem('theme');
    if (currentTheme) {
        document.documentElement.setAttribute('data-theme', currentTheme);
        if (currentTheme === 'dark') {
            toggleSwitch.checked = true;
        }
    }

    function switchTheme(e) {
        if (e.target.checked) {
            document.documentElement.setAttribute('data-theme', 'dark');
            localStorage.setItem('theme', 'dark');
            switcher.setAttribute('src', 'https://vectorified.com/images/switch-button-icon-19.png');
        } else {
            document.documentElement.setAttribute('data-theme', 'light');
            localStorage.setItem('theme', 'light');
            switcher.setAttribute('src', 'https://image.flaticon.com/icons/png/512/37/37474.png');
        }
    }
    toggleSwitch.addEventListener('change', switchTheme, false);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM