44//offsetTop - A Number, representing the top position of the element, in pixels
55
66// ********** set date ************
7-
7+ const date = document . getElementById ( 'date' ) ;
8+ date . innerHTML = new Date ( ) . getFullYear ( ) ;
89// ********** close links ************
10+ const navToggle = document . querySelector ( ".nav-toggle" ) ;
11+ const linksContainer = document . querySelector ( ".links-container" ) ;
12+ const links = document . querySelector ( ".links" ) ;
13+
14+ navToggle . addEventListener ( 'click' , function ( ) {
15+ //linksContainer.classList.toggle("show-links");
16+ const containerHeight = linksContainer . getBoundingClientRect ( ) . height ;
17+ const linksHeight = links . getBoundingClientRect ( ) . height ;
918
19+ if ( containerHeight === 0 ) {
20+ linksContainer . style . height = `${ linksHeight } px` ;
21+ } else {
22+ linksContainer . style . height = 0 ;
23+ }
24+ } )
1025// ********** fixed navbar ************
26+ const navbar = document . getElementById ( "nav" ) ;
27+ const topLink = document . querySelector ( ".top-link" ) ;
28+ window . addEventListener ( "scroll" , function ( ) {
29+ const scrollHeight = window . pageYOffset ;
30+ const navHeight = navbar . getBoundingClientRect ( ) . height ;
31+ if ( scrollHeight > navHeight ) {
32+ navbar . classList . add ( "fixed-nav" ) ;
33+ } else {
34+ navbar . classList . remove ( "fixed-nav" ) ;
35+ }
36+ if ( scrollHeight > 500 ) {
37+ topLink . classList . add ( "show-link" ) ;
38+ } else {
39+ topLink . classList . remove ( "show-link" ) ;
40+ }
1141
42+ } ) ;
1243// ********** smooth scroll ************
1344// select links
45+ const scrollLinks = document . querySelectorAll ( '.scroll-link' ) ;
46+
47+ scrollLinks . forEach ( function ( link ) {
48+ link . addEventListener ( "click" , function ( e ) {
49+ e . preventDefault ( ) ;
50+ const id = e . currentTarget . getAttribute ( 'href' ) . slice ( 1 ) ;
51+ const element = document . getElementById ( id ) ;
52+ //calculate the heights
53+ const navHeight = navbar . getBoundingClientRect ( ) . height ;
54+ const containerHeight = linksContainer . getBoundingClientRect ( ) . height ;
55+ const fixedNav = navbar . classList . contains ( 'fixed-nav' ) ;
56+ let position = element . offsetTop - navHeight ;
57+
58+ if ( ! fixedNav ) {
59+ position = position - navHeight ;
60+ }
61+ if ( navHeight > 82 ) {
62+ position = position + containerHeight ;
63+ }
64+ window . scrollTo ( {
65+ left : 0 ,
66+ top : position ,
67+ } ) ;
68+ linksContainer . style . height = 0 ;
69+ } ) ;
70+ } ) ;
0 commit comments