From dc778836f13ab2167f3522d858195883be21a98e Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Mon, 21 Jul 2025 16:33:47 +0800 Subject: [PATCH] feat: hide image in a11y mode --- assets/js/background-blur.js | 26 ++++++++-- layouts/partials/hero/background.html | 25 +++++----- layouts/partials/hero/thumbAndBackground.html | 49 ++++++++++--------- layouts/partials/home/background.html | 19 ++++--- 4 files changed, 73 insertions(+), 46 deletions(-) diff --git a/assets/js/background-blur.js b/assets/js/background-blur.js index 310b9d95..dc8d491b 100644 --- a/assets/js/background-blur.js +++ b/assets/js/background-blur.js @@ -10,7 +10,7 @@ return scrollY / scrollDivisor; }; - const applyBlurState = (blurElement, scrollDivisor, targetId) => { + const applyBlurState = (blurElement, scrollDivisor, targetId, imageElement, imageUrl) => { if (!blurElement) return; const isMenuBlur = targetId === "menu-blur"; @@ -23,22 +23,38 @@ blurElement.style.display = ""; blurElement.style.opacity = getScrollOpacity(scrollDivisor); } + // Hide image in a11y mode + if (imageElement) { + imageElement.style.display = "none"; + } } else { blurElement.style.display = ""; blurElement.style.opacity = getScrollOpacity(scrollDivisor); blurElement.removeAttribute("aria-hidden"); + // Show image in normal mode + if (imageElement) { + imageElement.style.display = ""; + // Re-apply image source if it was removed + if (imageUrl && !imageElement.getAttribute('src')) { + imageElement.setAttribute('src', imageUrl); + } + } } }; scripts.forEach(script => { const targetId = script.getAttribute("data-target-id"); const scrollDivisor = Number(script.getAttribute("data-scroll-divisor") || 300); + const imageId = script.getAttribute("data-image-id"); + const imageUrl = script.getAttribute("data-image-url"); // Get image URL from data attribute + const blurElement = document.getElementById(targetId); + const imageElement = imageId ? document.getElementById(imageId) : null; if (blurElement) { blurElement.setAttribute("role", "presentation"); blurElement.setAttribute("tabindex", "-1"); - applyBlurState(blurElement, scrollDivisor, targetId); + applyBlurState(blurElement, scrollDivisor, targetId, imageElement, imageUrl); window.addEventListener("scroll", () => { if (!isA11yMode() || targetId === "menu-blur") { @@ -53,8 +69,12 @@ scripts.forEach(script => { const targetId = script.getAttribute("data-target-id"); const scrollDivisor = Number(script.getAttribute("data-scroll-divisor") || 300); + const imageId = script.getAttribute("data-image-id"); + const imageUrl = script.getAttribute("data-image-url"); // Get image URL from data attribute + const blurElement = document.getElementById(targetId); - applyBlurState(blurElement, scrollDivisor, targetId); + const imageElement = imageId ? document.getElementById(imageId) : null; + applyBlurState(blurElement, scrollDivisor, targetId, imageElement, imageUrl); }); }; })(); diff --git a/layouts/partials/hero/background.html b/layouts/partials/hero/background.html index c3adf2e0..a2c83d7a 100644 --- a/layouts/partials/hero/background.html +++ b/layouts/partials/hero/background.html @@ -35,17 +35,16 @@
{{ end }} -{{ if or $disableImageOptimization (strings.HasSuffix $featured ".svg")}} - {{ with . }} -
+
+ {{ if or $disableImageOptimization (strings.HasSuffix $featured ".svg")}} + {{ with . }} + Background Image + {{ end }} + {{ else }} + {{ with .Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} + Background Image + {{ end }} {{ end }} -{{ else }} - {{ with .Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} -
- {{ end }} -{{ end }}
@@ -57,6 +56,10 @@
{{ $backgroundBlur := resources.Get "js/background-blur.js" }} {{ $backgroundBlur = $backgroundBlur | resources.Minify | resources.Fingerprint ($.Site.Params.fingerprintAlgorithm | default "sha512") }} - + {{ end }} {{- end -}} diff --git a/layouts/partials/hero/thumbAndBackground.html b/layouts/partials/hero/thumbAndBackground.html index 2a8448cb..6e480deb 100644 --- a/layouts/partials/hero/thumbAndBackground.html +++ b/layouts/partials/hero/thumbAndBackground.html @@ -58,30 +58,29 @@ {{- with $background -}} - {{ if or $disableImageOptimization (strings.HasSuffix . ".svg") }} - {{ with . }} -
-
-
-
+
+ {{ if or $disableImageOptimization (strings.HasSuffix . ".svg") }} + {{ with . }} + Background Image + {{ end }} + {{ else }} + {{ with .Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} + Background Image + {{ end }} {{ end }} - {{ else }} - {{ with .Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} -
-
-
-
- {{ end }} - {{ end }} - +
+
+
{{- end -}} {{ if $shouldBlur | default false }} @@ -94,5 +93,7 @@ type="text/javascript" src="{{ $backgroundBlur.RelPermalink }}" integrity="{{ $backgroundBlur.Data.Integrity }}" - data-target-id="background-blur"> + data-target-id="background-blur" + data-image-id="background-image-main" + {{ with $background }}data-image-url="{{ .RelPermalink }}"{{ end }}> {{ end }} diff --git a/layouts/partials/home/background.html b/layouts/partials/home/background.html index f2c06872..4e3c7534 100644 --- a/layouts/partials/home/background.html +++ b/layouts/partials/home/background.html @@ -1,5 +1,5 @@ {{ $disableImageOptimization := .Site.Params.disableImageOptimization | default false }} -
+
@@ -22,16 +22,17 @@ {{ end }} {{ if $homepageImage }}
+ class="from-neutral absolute inset-0 h-[1000px] bg-gradient-to-t to-transparent mix-blend-normal dark:from-neutral-800">
+ class="from-neutral absolute inset-0 h-[1000px] bg-gradient-to-t to-neutral-100 opacity-60 mix-blend-normal dark:from-neutral-800 dark:to-neutral-800">
{{ end }}
-
+
{{ with .Site.Params.Author.image }} {{ $authorImage := "" }} {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} @@ -44,7 +45,7 @@ {{ $authorImage = $authorImage.Fill (print "288x288 q" ( $.Site.Params.Author.imagequality | default "96" )) }} {{ end }} {{ $.Site.Params.Author.name | default + data-target-id="background-blur" + data-image-id="homepage-background-image" + {{ if $homepageImage }}data-image-url="{{ $homepageImage.RelPermalink }}"{{ end }}> {{ end }}