Can anyone help me optimize the speed at which dark mode is applied on my website? Currently, there seems to be a delay between page load and the dark mode taking effect. Below is the jQuery.js code I am using:
onload = function () {
if (localStorage.getItem("darkMode") === "true") {
var mode = localStorage.getItem("darkMode");
enableDarkMode();
}
}
function enableDarkMode() {
$("body").addClass("dark");
$("nav").removeClass("navbar navbar-expand-md navbar-light");
$("nav").addClass("navbar navbar-expand-md navbar-dark");
$('.inner-switch').text("ON");
var mode = localStorage.setItem("darkMode", "true");
}
function disableDarkMode() {
$("body").removeClass("dark");
$("nav").removeClass("navbar navbar-expand-md navbar-dark");
$("nav").addClass("navbar navbar-expand-md navbar-light");
$('.inner-switch').text("OFF");
var mode = localStorage.setItem("darkMode", "false");
}
$('.inner-switch').on("click", function () {
if ($("body").hasClass("dark")) {
disableDarkMode();
} else {
enableDarkMode();
}
});