@@ -6,7 +6,9 @@ class Navbar {
6
6
this . navbarHeader = document . getElementById ( "navbar-header" )
7
7
this . pageContainer = document . getElementsByClassName ( "page-container" ) [ 0 ]
8
8
this . navbar = document . getElementById ( "navbar" )
9
- this . lastY = window . pageYOffset ;
9
+ this . y = window . scrollY ;
10
+ this . velocityY = 0 ;
11
+ this . accelerationY = 0 ;
10
12
11
13
window . addEventListener ( "scroll" , ( ) => {
12
14
this . onScroll ( )
@@ -15,7 +17,7 @@ class Navbar {
15
17
document . addEventListener ( "click" , ( e ) => {
16
18
17
19
if ( this . navbarHeader . contains ( e . target ) ) {
18
-
20
+
19
21
if ( this . isOpened ) {
20
22
this . closeDrawer ( )
21
23
}
@@ -34,49 +36,49 @@ class Navbar {
34
36
35
37
showNavbar ( ) {
36
38
this . navbar . classList . remove ( "hide" )
37
- this . navbarShown = ! ! ! this . navbarShown
39
+ this . navbarShown = ! ! ! this . navbarShown
38
40
}
39
41
40
42
hideNavbar ( ) {
41
43
this . navbar . classList . add ( "hide" )
42
- this . navbarShown = ! ! ! this . navbarShown
44
+ this . navbarShown = ! ! ! this . navbarShown
43
45
}
44
46
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 ) {
49
53
this . showNavbar ( )
50
54
}
51
55
else if ( ! this . isOpened && document . body . scrollHeight > document . body . clientHeight ) {
52
56
this . hideNavbar ( )
53
57
}
54
- this . lastY = currentY
58
+ this . y = currentY
59
+ this . velocityY = velocityY
60
+ this . accelerationY = accelerationY
55
61
}
56
62
57
63
openDrawer ( ) {
58
64
this . pageContainer . classList . add ( "navbar-open" )
59
65
this . navbar . classList . add ( "open" )
60
66
this . navbarDrawer . classList . add ( "open" )
61
67
this . navbarHeader . classList . add ( "opened" )
62
- this . isOpened = ! ! ! this . isOpened
68
+ this . isOpened = ! ! ! this . isOpened
63
69
}
64
70
65
71
closeDrawer ( ) {
66
72
this . pageContainer . classList . remove ( "navbar-open" )
67
73
this . navbar . classList . remove ( "open" )
68
74
this . navbarDrawer . classList . remove ( "open" )
69
75
this . navbarHeader . classList . remove ( "opened" )
70
- this . isOpened = ! ! ! this . isOpened
76
+ this . isOpened = ! ! ! this . isOpened
71
77
}
72
78
}
73
79
74
80
let navbar ;
75
81
76
- const test = ( e ) => {
77
- e . preventDefault ( )
78
- }
79
-
80
82
window . addEventListener ( "load" , ( ) => {
81
83
navbar = new Navbar ( )
82
84
} )
0 commit comments