11// Element.getBoundingClientRect() method returns the size of an element and its position relative to the viewport.
2-
32// pageYOffset is a read - only window property that returns the number of pixels the document has been scrolled vertically.
43// slice extracts a section of a string without modifying original string
54//offsetTop - A Number, representing the top position of the element, in pixels
@@ -33,8 +32,9 @@ const navbar = document.getElementById("nav");
3332const topLink = document . querySelector ( ".top-link" ) ;
3433
3534window . addEventListener ( "scroll" , function ( ) {
36- const height = window . pageYOffset ;
37- if ( height > 96 ) {
35+ const scrollHeight = window . pageYOffset ;
36+ const navHeight = navbar . getBoundingClientRect ( ) . height ;
37+ if ( scrollHeight > navHeight ) {
3838 navbar . classList . add ( "fixed-nav" ) ;
3939 } else {
4040 navbar . classList . remove ( "fixed-nav" ) ;
@@ -57,7 +57,7 @@ scrollLinks.forEach((link) => {
5757 // navigate to specific spot
5858 const id = e . currentTarget . getAttribute ( "href" ) . slice ( 1 ) ;
5959 const element = document . getElementById ( id ) ;
60- // calculate heights
60+
6161 const navHeight = navbar . getBoundingClientRect ( ) . height ;
6262 const containerHeight = linksContainer . getBoundingClientRect ( ) . height ;
6363 const fixedNav = navbar . classList . contains ( "fixed-nav" ) ;
@@ -69,6 +69,7 @@ scrollLinks.forEach((link) => {
6969 if ( navHeight > 82 ) {
7070 position = position + containerHeight ;
7171 }
72+
7273 window . scrollTo ( {
7374 left : 0 ,
7475 top : position ,
@@ -77,3 +78,4 @@ scrollLinks.forEach((link) => {
7778 linksContainer . style . height = 0 ;
7879 } ) ;
7980} ) ;
81+ // calculate heights
0 commit comments