feat: Torna o header fixo no scroll

This commit is contained in:
Saulo Klein Nery 2022-12-08 14:47:11 -03:00
parent 85c28ba5f0
commit 7f9d01a0a0
3 changed files with 24 additions and 35 deletions

View File

@ -1,45 +1,25 @@
import { isSmallerThen768 } from "Helpers/MediasMatch"; import { isSmallerThen991 } from "Helpers/MediasMatch";
export default class FixedHeader { export default class FixedHeader {
constructor() { constructor() {
if (!isSmallerThen768) { if (!isSmallerThen991) {
this.fixedHeader(); this.selectors();
this.events();
} }
} }
fixedHeader() { selectors() {
var lastScroll = 0; this.pageHeader = $(".page-header");
var timer, scrollTop, headerHeight; }
var element = $("header.header");
$(document).scroll(function (e) { events() {
headerHeight = element.height(); $(document).scroll(this.fixHeader.bind(this));
scrollTop = $(document).scrollTop(); }
if (scrollTop > 1) { fixHeader() {
$("body").css("padding-top", headerHeight); const topScroll = $(document).scrollTop();
element.addClass("fixed");
if (timer) { if (topScroll > 200) this.pageHeader.addClass("is-fixed");
window.clearTimeout(timer); else this.pageHeader.removeClass("is-fixed");
}
timer = window.setTimeout(function () {
if (
scrollTop > lastScroll &&
scrollTop > element.height() + 20
) {
element.addClass("fixed-hide");
} else {
element.removeClass("fixed-hide");
}
lastScroll = $(document).scrollTop();
}, 50);
} else {
element.removeClass("fixed");
$("body").css("padding-top", 0);
}
});
} }
} }

View File

@ -7,6 +7,15 @@
padding: 18px 0 0; padding: 18px 0 0;
} }
@include mq(lg, min) {
position: sticky;
top: 0;
}
&.is-fixed {
background: rgba($black-500, 0.9);
}
&__wrapper { &__wrapper {
display: grid; display: grid;
grid-template-columns: 2fr 1fr 2fr; grid-template-columns: 2fr 1fr 2fr;

View File

@ -22,7 +22,7 @@
<body> <body>
<vtex:template id="sauloklein-header" /> <vtex:template id="sauloklein-header" />
<main class="home"></main> <main style="height: 600px" class="home"></main>
<footer> <footer>
<vtex:template id="sauloklein-footer-newsletter" /> <vtex:template id="sauloklein-footer-newsletter" />