create new css link to smoothly transition between themes

Previously, directly editing the <link> tag with the new file would
cause the page to have no stylesheet for a moment while the new file is
downloaded. A new element is now appended below the original instead,
which smoothens out the transition.
This commit is contained in:
Harvey Tindall
2020-08-19 14:31:41 +01:00
parent ec7609ed8c
commit 56478e96c9
2 changed files with 26 additions and 4 deletions
+13 -2
View File
@@ -19,14 +19,25 @@ var transitionEndEvent = whichTransitionEvent();
// Toggles between light and dark themes
function toggleCSS() {
let cssEl = document.querySelectorAll('link[rel="stylesheet"][type="text/css"]')[0];
let Els = document.querySelectorAll('link[rel="stylesheet"][type="text/css"]');
let cssEl = Els[0]
let remove = false;
if (Els.length != 1) {
cssEl = Els[1]
remove = true
}
let href = "bs" + bsVersion;
if (cssEl.href.includes(href + "-jf")) {
href += ".css";
} else {
href += "-jf.css";
}
cssEl.href = href
let newEl = cssEl.cloneNode(true);
newEl.href = href
cssEl.parentNode.insertBefore(newEl, cssEl.nextSibling);
if (remove) {
Els[0].remove()
}
document.cookie = "css=" + href;
}