I want to activate dark mode auto at night and will be off at day or morning.
I have a code of dark mode toggle of my website. It enables dark mode and light mode toggle with localstorage. How can I set it like that, if it's night, it will be dark by default for any users and at night it will be light by default.
Normally website with dark mode option, day mode is auto selected or default selected. If any user want to activate dark mode, he can toggle to dark mode. After he can change.
I want something like that with advance option or features. From Evening to Dawn, it will be black or dark by default. And user can change it easily. By the same way, it will be light from Morning to evening.
Here's my code
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js' type='text/javascript'></script>
<style>
:root{
--mmm-bg-color: #EADE5A;
--body: #fff;
}
html.is-dark{
--mmm-bg-color:#0057AF;
--body: #000;
--text: #fff;
}
body{
background-color: var(--body);
color: var(--text);
}
.mmmcard {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
padding: 16px;
text-align: center;
background-color: var(--mmm-bg-color);
}
</style>
<a class='darkmode-toggle' cond='data:skin.vars.darkmode == "0px" and (data:skin.vars.userdarkmode == "1px")' href='javascript:;' name='a' role='button'> DARK / LIGHT </a>
<div class="mmmcard">
<h3>Card 1</h3></div>
<p style="font-size:15px; margin-top: 25px;">
This is an example of Dark Mode. I want to take it next level.
</p>
<script type='text/javascript'>
$("html").each(function() {
var e = $(this);
darkMode = "undefined" != typeof darkMode && darkMode, userDarkMode = "undefined" == typeof userDarkMode || userDarkMode, 1 != darkMode && 0 != userDarkMode && ("dark" == localStorage.themeColor && e.addClass("is-dark"), $(".darkmode-toggle").on("click", function() {
"dark" != localStorage.themeColor ? (e.addClass("is-dark"), localStorage.themeColor = "dark") : (e.removeClass("is-dark"), localStorage.themeColor = "light")
}))
});
</script>
How can I do this using only JavaScript or jQuery?