Skip to content

Commit 9c8bd1a

Browse files
authored
Correctly update medium-zoom background when switching theme (alshedivat#332)
1 parent 495c4aa commit 9c8bd1a

File tree

4 files changed

+47
-41
lines changed

4 files changed

+47
-41
lines changed

_posts/2015-05-15-images.md

-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,3 @@ The rest of the images in this post are all zoomable, arranged into different mi
4343
<img class="img-fluid rounded z-depth-1" src="{{ site.baseurl }}/assets/img/7.jpg" data-zoomable>
4444
</div>
4545
</div>
46-

assets/js/dark_mode.js

+1-28
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,6 @@ $(document).ready(function() {
22
const mode_toggle = document.getElementById("light-toggle");
33

44
mode_toggle.addEventListener("click", function() {
5-
const temp = localStorage.getItem("theme");
6-
toggleTheme(temp);
5+
toggleTheme(localStorage.getItem("theme"));
76
});
8-
9-
let toggleTheme = (theme) => {
10-
if (theme == "dark") {
11-
setTheme("light");
12-
} else {
13-
setTheme("dark");
14-
}
15-
}
16-
17-
let setTheme = (theme) => {
18-
trans();
19-
if (theme) {
20-
document.documentElement.setAttribute("data-theme", theme)
21-
}
22-
else {
23-
document.documentElement.removeAttribute("data-theme");
24-
}
25-
localStorage.setItem("theme", theme);
26-
};
27-
28-
let trans = () => {
29-
document.documentElement.classList.add("transition");
30-
window.setTimeout(() => {
31-
document.documentElement.classList.remove("transition")
32-
}, 500)
33-
}
347
});

assets/js/theme.js

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
11
// Has to be in the head tag, otherwise a flicker effect will occur.
2+
3+
let toggleTheme = (theme) => {
4+
if (theme == "dark") {
5+
setTheme("light");
6+
} else {
7+
setTheme("dark");
8+
}
9+
}
10+
11+
12+
let setTheme = (theme) => {
13+
transTheme();
14+
if (theme) {
15+
document.documentElement.setAttribute("data-theme", theme);
16+
}
17+
else {
18+
document.documentElement.removeAttribute("data-theme");
19+
}
20+
localStorage.setItem("theme", theme);
21+
22+
// Updates the background of medium-zoom overlay.
23+
if (typeof medium_zoom !== 'undefined') {
24+
medium_zoom.update({
25+
background: getComputedStyle(document.documentElement)
26+
.getPropertyValue('--global-bg-color') + 'ee', // + 'ee' for trasparency.
27+
})
28+
}
29+
};
30+
31+
32+
let transTheme = () => {
33+
document.documentElement.classList.add("transition");
34+
window.setTimeout(() => {
35+
document.documentElement.classList.remove("transition");
36+
}, 500)
37+
}
38+
39+
240
let initTheme = (theme) => {
341
if (theme == null) {
442
const userPref = window.matchMedia;
543
if (userPref && userPref('(prefers-color-scheme: dark)').matches) {
644
theme = 'dark';
745
}
846
}
9-
10-
if (theme) {
11-
document.documentElement.setAttribute('data-theme', theme)
12-
}
13-
14-
localStorage.setItem("theme", theme);
47+
setTheme(theme);
1548
}
1649

50+
1751
initTheme(localStorage.getItem("theme"));

assets/js/zoom.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
1+
// Initialize medium zoom.
22
$(document).ready(function() {
3-
mediumZoom('[data-zoomable]', {
4-
margin: 100,
5-
background: getComputedStyle(document.documentElement)
6-
.getPropertyValue('--global-bg-color') + 'ee',
7-
})
3+
medium_zoom = mediumZoom('[data-zoomable]', {
4+
margin: 100,
5+
background: getComputedStyle(document.documentElement)
6+
.getPropertyValue('--global-bg-color') + 'ee', // + 'ee' for trasparency.
7+
})
88
});

0 commit comments

Comments
 (0)