Skip to content

Commit

Permalink
#47 Adds spacing to FAQ accordion when sticky header is true
Browse files Browse the repository at this point in the history
Signed-off-by: James Hunt <10615884+thetwopct@users.noreply.github.com>

Signed-off-by: James Hunt <10615884+thetwopct@users.noreply.github.com>
  • Loading branch information
thetwopct committed May 21, 2024
1 parent 293e026 commit e7fae65
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
21 changes: 16 additions & 5 deletions assets/js/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ const closeAccordion = (accordion) => {
content.style.maxHeight = null;
};

// If sticky_header add offset of 80/100 based on device width.
const getScrollOffset = () => {
if (window.innerWidth < 1000) {
return stickyHeader ? 80 : 0;
} else {
return stickyHeader ? 100 : 0;
}
};

accordions.forEach((accordion) => {
const title = accordion.querySelector(".accordion__title");
const content = accordion.querySelector(".accordion__content");
Expand All @@ -23,13 +32,15 @@ accordions.forEach((accordion) => {
accordions.forEach((accordion) => closeAccordion(accordion));
openAccordion(accordion);

// Use setTimeout to wait a moment before scrolling into view
// Use setTimeout to wait a moment before scrolling into view.
setTimeout(() => {
accordion.scrollIntoView({
behavior: 'smooth',
block: 'start'
const offset = getScrollOffset();
const elementTop = accordion.getBoundingClientRect().top;
window.scrollBy({
top: elementTop - offset,
behavior: 'smooth'
});
}, 500);
}, 250);
}
};
});
3 changes: 3 additions & 0 deletions layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
{{ if and (eq .Type "faq") (eq .Kind "section") }}
{{ $js := resources.Get "js/accordion.js" }}
<script defer src="{{ $js.Permalink }}"></script>
<script>
var stickyHeader = {{ if .Site.Params.sticky_header }}true{{ else }}false{{ end }};
</script>
{{ end }}

{{/* Theme: Scripts */}}
Expand Down

0 comments on commit e7fae65

Please sign in to comment.