Skip to content

Commit 6fedd61

Browse files
committed
Modify drawer autohide. Remove unnecessary file.
1 parent f1afe48 commit 6fedd61

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

assets/js/navbar.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ class Navbar {
66
this.navbarHeader = document.getElementById("navbar-header")
77
this.pageContainer = document.getElementsByClassName("page-container")[0]
88
this.navbar = document.getElementById("navbar")
9-
this.lastY = window.pageYOffset;
9+
this.y = window.scrollY;
10+
this.velocityY = 0;
11+
this.accelerationY = 0;
1012

1113
window.addEventListener("scroll", () => {
1214
this.onScroll()
@@ -15,7 +17,7 @@ class Navbar {
1517
document.addEventListener("click", (e) => {
1618

1719
if (this.navbarHeader.contains(e.target)) {
18-
20+
1921
if (this.isOpened) {
2022
this.closeDrawer()
2123
}
@@ -34,49 +36,49 @@ class Navbar {
3436

3537
showNavbar() {
3638
this.navbar.classList.remove("hide")
37-
this.navbarShown = !!! this.navbarShown
39+
this.navbarShown = !!!this.navbarShown
3840
}
3941

4042
hideNavbar() {
4143
this.navbar.classList.add("hide")
42-
this.navbarShown = !!! this.navbarShown
44+
this.navbarShown = !!!this.navbarShown
4345
}
4446

45-
onScroll() {
46-
const currentY = window.pageYOffset
47-
48-
if (this.lastY > currentY) {
47+
onScroll(threshold = 1) {
48+
const currentY = window.scrollY
49+
const velocityY = currentY - this.y
50+
const accelerationY = velocityY - this.velocityY
51+
52+
if (this.accelerationY > threshold) {
4953
this.showNavbar()
5054
}
5155
else if (!this.isOpened && document.body.scrollHeight > document.body.clientHeight) {
5256
this.hideNavbar()
5357
}
54-
this.lastY = currentY
58+
this.y = currentY
59+
this.velocityY = velocityY
60+
this.accelerationY = accelerationY
5561
}
5662

5763
openDrawer() {
5864
this.pageContainer.classList.add("navbar-open")
5965
this.navbar.classList.add("open")
6066
this.navbarDrawer.classList.add("open")
6167
this.navbarHeader.classList.add("opened")
62-
this.isOpened = !!! this.isOpened
68+
this.isOpened = !!!this.isOpened
6369
}
6470

6571
closeDrawer() {
6672
this.pageContainer.classList.remove("navbar-open")
6773
this.navbar.classList.remove("open")
6874
this.navbarDrawer.classList.remove("open")
6975
this.navbarHeader.classList.remove("opened")
70-
this.isOpened = !!! this.isOpened
76+
this.isOpened = !!!this.isOpened
7177
}
7278
}
7379

7480
let navbar;
7581

76-
const test = (e) => {
77-
e.preventDefault()
78-
}
79-
8082
window.addEventListener("load", () => {
8183
navbar = new Navbar()
8284
})

page.js

-8
This file was deleted.

0 commit comments

Comments
 (0)