Skip to content

Commit

Permalink
style: show the shadow only when scrolling (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang authored Aug 4, 2023
1 parent 781fca1 commit b87bc7d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
17 changes: 15 additions & 2 deletions assets/hb/modules/header/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ import "js/bootstrap/src/offcanvas";
(() => {
document.addEventListener('DOMContentLoaded', () => {
const header = document.querySelector('.hb-header') as HTMLElement
if (!header || window.innerWidth >= 576) {
if (!header) {
return;
}

const shadow = () => {
header.classList.add('shadow-sm')
}

const removeShadow = () => {
header.classList.contains('shadow-sm') && header.classList.remove('shadow-sm')
}

const show = () => {
header.style.removeProperty('opacity');
header.style.removeProperty('z-index');
Expand All @@ -21,7 +29,12 @@ import "js/bootstrap/src/offcanvas";
let h = 0;
const threshold = 20;
window.addEventListener('scroll', () => {
if (Math.abs(document.documentElement.scrollTop - h) > threshold) {
if (document.documentElement.scrollTop === 0) {
removeShadow()
} else {
shadow()
}
if (window.innerWidth < 576 && Math.abs(document.documentElement.scrollTop - h) > threshold) {
document.documentElement.scrollTop > h ? hide() : show();
h = document.documentElement.scrollTop;
}
Expand Down
1 change: 1 addition & 0 deletions assets/hb/modules/header/purgecss.config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
classes = [
"offcanvas-backdrop", # Required by Offcanvas.
"shadow-sm",
]
2 changes: 1 addition & 1 deletion assets/hb/modules/header/scss/_header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $hb-header-margin-bottom: 2rem;

.hb-header {
margin-bottom: $hb-header-margin-bottom;
transition: opacity 0.2s ease-in;
transition: opacity 0.2s ease-in, box-shadow 1s;

@if $hb-header-sticky {
@extend .sticky-top;
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/hb/modules/header/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{- $params := $.Site.Params.hb.header }}
<header class="hb-header shadow-sm">
<header class="hb-header">
<nav class="hb-header-nav navbar navbar-expand-xxl">
<div
class="{{ cond $params.full_width `container-fluid` `container-xxl` }}">
Expand Down

0 comments on commit b87bc7d

Please sign in to comment.