|
3 | 3 | ========================================================================== */
|
4 | 4 |
|
5 | 5 | $(document).ready(function () {
|
6 |
| - // Set the theme on page load |
| 6 | + // detect OS/browser preference |
| 7 | + const browserPref = window.matchMedia('(prefers-color-scheme: dark)').matches |
| 8 | + ? 'dark' |
| 9 | + : 'light'; |
| 10 | + |
| 11 | + // Set the theme on page load or when explicitly called |
7 | 12 | var setTheme = function (theme) {
|
8 |
| - const use_theme = theme || localStorage.getItem("theme") || $("html").attr("data-theme"); |
| 13 | + const use_theme = |
| 14 | + theme || |
| 15 | + localStorage.getItem("theme") || |
| 16 | + $("html").attr("data-theme") || |
| 17 | + browserPref; |
| 18 | + |
9 | 19 | if (use_theme === "dark") {
|
10 | 20 | $("html").attr("data-theme", "dark");
|
11 | 21 | $("#theme-icon").removeClass("fa-sun").addClass("fa-moon");
|
12 | 22 | } else if (use_theme === "light") {
|
13 | 23 | $("html").removeAttr("data-theme");
|
14 | 24 | $("#theme-icon").removeClass("fa-moon").addClass("fa-sun");
|
15 | 25 | }
|
16 |
| - } |
| 26 | + }; |
| 27 | + |
17 | 28 | setTheme();
|
18 | 29 |
|
19 |
| - // Toggle the theme |
| 30 | + // if user hasn't chosen a theme, follow OS changes |
| 31 | + window |
| 32 | + .matchMedia('(prefers-color-scheme: dark)') |
| 33 | + .addEventListener("change", (e) => { |
| 34 | + if (!localStorage.getItem("theme")) { |
| 35 | + setTheme(e.matches ? "dark" : "light"); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + // Toggle the theme manually |
20 | 40 | var toggleTheme = function () {
|
21 | 41 | const current_theme = $("html").attr("data-theme");
|
22 | 42 | const new_theme = current_theme === "dark" ? "light" : "dark";
|
23 | 43 | localStorage.setItem("theme", new_theme);
|
24 | 44 | setTheme(new_theme);
|
25 |
| - } |
26 |
| - $('#theme-toggle').on('click', function () { |
27 |
| - toggleTheme(); |
28 |
| - }); |
| 45 | + }; |
| 46 | + |
| 47 | + $('#theme-toggle').on('click', toggleTheme); |
29 | 48 |
|
30 | 49 | // These should be the same as the settings in _variables.scss
|
31 | 50 | const scssLarge = 925; // pixels
|
@@ -65,10 +84,35 @@ $(document).ready(function () {
|
65 | 84 | });
|
66 | 85 |
|
67 | 86 | // init smooth scroll, this needs to be slightly more than then fixed masthead height
|
68 |
| - $("a").smoothScroll({ offset: -65 }); |
| 87 | + $("a").smoothScroll({ |
| 88 | + offset: -75, // needs to match $masthead-height |
| 89 | + preventDefault: false, |
| 90 | + }); |
69 | 91 |
|
70 | 92 | // add lightbox class to all image links
|
71 |
| - $("a[href$='.jpg'],a[href$='.jpeg'],a[href$='.JPG'],a[href$='.png'],a[href$='.gif']").addClass("image-popup"); |
| 93 | + // Add "image-popup" to links ending in image extensions, |
| 94 | + // but skip any <a> that already contains an <img> |
| 95 | + $("a[href$='.jpg'],\ |
| 96 | + a[href$='.jpeg'],\ |
| 97 | + a[href$='.JPG'],\ |
| 98 | + a[href$='.png'],\ |
| 99 | + a[href$='.gif'],\ |
| 100 | + a[href$='.webp']") |
| 101 | + .not(':has(img)') |
| 102 | + .addClass("image-popup"); |
| 103 | + |
| 104 | + // 1) Wrap every <p><img> (except emoji images) in an <a> pointing at the image, and give it the lightbox class |
| 105 | + $('p > img').not('.emoji').each(function() { |
| 106 | + var $img = $(this); |
| 107 | + // skip if it’s already wrapped in an <a.image-popup> |
| 108 | + if ( ! $img.parent().is('a.image-popup') ) { |
| 109 | + $('<a>') |
| 110 | + .addClass('image-popup') |
| 111 | + .attr('href', $img.attr('src')) |
| 112 | + .insertBefore($img) // place the <a> right before the <img> |
| 113 | + .append($img); // move the <img> into the <a> |
| 114 | + } |
| 115 | + }); |
72 | 116 |
|
73 | 117 | // Magnific-Popup options
|
74 | 118 | $(".image-popup").magnificPopup({
|
|
0 commit comments