简体   繁体   English

在 html 网站中添加一个按钮以更改为暗模式

[英]Add a button to change to dark mode in html website

I have added a button on my site which let's the users change to dark or light mode whenever they want.我在我的网站上添加了一个按钮,让用户可以随时更改为暗模式或亮模式。 I added the button with a moon icon on it, but the problem is that I want that the moon icon changes to sun icon when the user is in dark mode.我添加了带有月亮图标的按钮,但问题是我希望当用户处于黑暗模式时月亮图标变为太阳图标。 And changes to moon icon when user is in light mode.并在用户处于光照模式时更改为月亮图标。

 function myfunction(e) { console.log("you clicked"); document.documentElement.classList.toggle("dark-mode"); document.querySelectorAll(".inverted").forEach((result) => { result.classList.toggle("invert"); }); } const btn = document.querySelector('.btn') btn.addEventListener('click', myfunction);
 .dark-mode { filter: invert(1) hue-rotate(180deg); } .invert { filter: invert(1) hue-rotate(180deg); }
 <button class="btn"><img src='moon.png'></img></button>

The .inverted class in js is because I don't want the images to invert their colors.. so I gave all the images a class='inverted' js 中的 .inverted 类是因为我不希望图像反转它们的颜色..所以我给所有图像一个 class='inverted'

So, this is what I've done and someone please let me know how I should change the icon to moon and sun depending on the current mode (light or dark)所以,这就是我所做的,有人请告诉我如何根据当前模式(亮或暗)将图标更改为月亮和太阳

Thanks!谢谢!

You could add the sun as another image to the button and change the visibility of the two images via your .dark-mode css class.您可以将太阳作为另一个图像添加到按钮,并通过您的.dark-mode css 类更改两个图像的可见性。

So whenever the .dark-mode class is present the moon gets hidden and the sun gets shown.因此,每当.dark-mode类出现时,月亮就会被隐藏起来,而太阳就会被显示出来。

 function myfunction(e) { console.log("you clicked"); document.documentElement.classList.toggle("dark-mode"); document.querySelectorAll(".inverted").forEach((result) => { result.classList.toggle("invert"); }); } const btn = document.querySelector('.btn') btn.addEventListener('click', myfunction);
 .dark-mode { filter: invert(1) hue-rotate(180deg); } .invert { filter: invert(1) hue-rotate(180deg); } /* button handling */ .moon { display: block; } .sun { display: none; } .dark-mode .moon { display: none; } .dark-mode .sun { display: block; }
 <button class="btn"> <img class="moon" src="moon.png" alt="moon"></img> <img class="sun" src="sun.png" alt="sun"></img> </button>

You could go with the CSS approach as in @Fabian's answer.您可以采用@Fabian 的回答中的 CSS 方法。 If you would like to go with JS, you could simply use a flag to indicate whether or not the user switched to dark mode, and dynamically set the icon based on it.如果你想用 JS,你可以简单地使用一个标志来指示用户是否切换到暗模式,并根据它动态设置图标。

let isDarkMode = document.documentElement.classList.contains("dark-mode");
function myfunction(e) {
    document.documentElement.classList.toggle("dark-mode");
    document.querySelectorAll(".inverted").forEach((result) => {
        result.classList.toggle("invert");
    });
    e.currentTarget.querySelector("img").src = isDarkMode ? "sun.png" : "moon.png";
 }

 function myfunction(e) { const doc = document.documentElement doc.classList.toggle("dark-mode"); document.querySelectorAll(".inverted").forEach((result) => { result.classList.toggle("invert"); }); const img = e.currentTarget.querySelector('img') if (doc.classList.contains('dark-mode')) { img.src = 'sun.png' } else { img.src = 'moon.png' } } const btn = document.querySelector('.btn') btn.addEventListener('click', myfunction);
 .dark-mode { filter: invert(1) hue-rotate(180deg); } .invert { filter: invert(1) hue-rotate(180deg); } '
 <button class="btn"><img src='moon.png' /> Toggle Dark mode </button>

You can use the below reference for the toggle button from light mode to dark mode and dark mode to light mode.您可以将以下参考用于从亮模式到暗模式以及从暗模式到亮模式的切换按钮。

 body { height: 100vh; display: flex; align-items: center; justify-content: center; } .toggle-checkbox { position: absolute; opacity: 0; cursor: pointer; height: 0; width: 0; } .toggle-slot { position: relative; height: 10em; width: 20em; border: 5px solid #e4e7ec; border-radius: 10em; background-color: white; box-shadow: 0px 10px 25px #e4e7ec; transition: background-color 250ms; } .toggle-checkbox:checked ~ .toggle-slot { background-color: #374151; } .toggle-button { transform: translate(11.75em, 1.75em); position: absolute; height: 6.5em; width: 6.5em; border-radius: 50%; background-color: #ffeccf; box-shadow: inset 0px 0px 0px 0.75em #ffbb52; transition: background-color 250ms, border-color 250ms, transform 500ms cubic-bezier(.26,2,.46,.71); } .toggle-checkbox:checked ~ .toggle-slot .toggle-button { background-color: #485367; box-shadow: inset 0px 0px 0px 0.75em white; transform: translate(1.75em, 1.75em); } .sun-icon { position: absolute; height: 6em; width: 6em; color: #ffbb52; } .sun-icon-wrapper { position: absolute; height: 6em; width: 6em; opacity: 1; transform: translate(2em, 2em) rotate(15deg); transform-origin: 50% 50%; transition: opacity 150ms, transform 500ms cubic-bezier(.26,2,.46,.71); } .toggle-checkbox:checked ~ .toggle-slot .sun-icon-wrapper { opacity: 0; transform: translate(3em, 2em) rotate(0deg); } .moon-icon { position: absolute; height: 6em; width: 6em; color: white; } .moon-icon-wrapper { position: absolute; height: 6em; width: 6em; opacity: 0; transform: translate(11em, 2em) rotate(0deg); transform-origin: 50% 50%; transition: opacity 150ms, transform 500ms cubic-bezier(.26,2.5,.46,.71); } .toggle-checkbox:checked ~ .toggle-slot .moon-icon-wrapper { opacity: 1; transform: translate(12em, 2em) rotate(-15deg); }
 <head> <script src="https://code.iconify.design/1/1.0.4/iconify.min.js"> </script> </head> <label> <input class='toggle-checkbox' type='checkbox'> <div class='toggle-slot'> <div class='sun-icon-wrapper'> <div class="iconify sun-icon" data-icon="feather-sun" data-inline="false"></div> </div> <div class='toggle-button'></div> <div class='moon-icon-wrapper'> <div class="iconify moon-icon" data-icon="feather-moon" data-inline="false"></div> </div> </div> </label>

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

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