Skip to content

Commit 9e14048

Browse files
committedFeb 6, 2025·
Try this.
1 parent 72d02e7 commit 9e14048

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed
 

‎assets/js/navbar.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Navbar {
99
this.y = window.scrollY;
1010
this.velocityY = 0;
1111
this.accelerationY = 0;
12+
this.jerkY = 0;
1213

1314
window.addEventListener("scroll", () => {
1415
this.onScroll()
@@ -44,20 +45,24 @@ class Navbar {
4445
this.navbarShown = !!!this.navbarShown
4546
}
4647

47-
onScroll(threshold = 1) {
48-
const currentY = window.scrollY
49-
const velocityY = currentY - this.y
48+
onScroll(threshold = 30) {
49+
const y = window.scrollY
50+
const velocityY = y - this.y
5051
const accelerationY = velocityY - this.velocityY
52+
const jerkY = accelerationY - this.accelerationY
5153

52-
if ((-accelerationY) > threshold && velocityY < 0) {
54+
55+
if ((-jerkY) > threshold && velocityY < 0) {
56+
console.log({ accelerationY, velocityY, y, threshold, jerkY })
5357
this.showNavbar()
5458
}
55-
else if (!this.isOpened && document.body.scrollHeight > document.body.clientHeight && velocityY > 0 && currentY > 0) {
59+
else if (!this.isOpened && document.body.scrollHeight > document.body.clientHeight && velocityY > 0 && y > 0) {
5660
this.hideNavbar()
5761
}
58-
this.y = currentY
62+
this.y = y
5963
this.velocityY = velocityY
6064
this.accelerationY = accelerationY
65+
this.jerkY = jerkY
6166
}
6267

6368
openDrawer() {

0 commit comments

Comments
 (0)
Please sign in to comment.