I wrote the code below and placed it just before the </body>
tag on each page of my site. It works great in that it turns the background of each page black and the text to white and remembers that setting until the user changes it back to "Day Mode".
<script>
var body = $("body");
var day = $("#day");
var night = $("#night");
var dayLi = $("#day-li");
var nightLi = $("#night-li");
var panel = $(".panel");
if(localStorage.night == "on"){
body.addClass("night");
panel.addClass("night");
dayLi.show();
nightLi.hide();
}else{
body.removeClass("night");
panel.removeClass("night");
nightLi.show();
dayLi.hide();
}
night.click(function(){
body.addClass("night");
panel.addClass("night");
localStorage.setItem("night","on");
dayLi.show();
nightLi.hide();
});
day.click(function(){
body.removeClass("night");
panel.removeClass("night");
localStorage.night = "off";
nightLi.show();
dayLi.hide();
});
</script>
The .night
class is as you might expect:
.night{
background-color: black;
color: white;
}
The problem I'm having with it is that when in night mode, it takes about a second for night mode to take effect, meaning there is a white flash on each page load.
I experimented with placing the above script higher up on the page, but still got the flash of white first on each page load. To be clear, the flash of white is very short or almost non-existent on pages that aren't loading as much data from my database. But on the pages where a lot of data is getting loaded from my db the flash lasts for about a second.
Is this something that can be fixed? Thanks in advance for any pointers.