Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56f0dd9

Browse files
committed
Sticky header
1 parent 2c65b9f commit 56f0dd9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

24 - Sticky Nav/index-START.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ <h1>A story about getting lost.</h1>
5555

5656
<script>
5757

58+
const nav = document.querySelector('#main');
59+
const navTop = nav.offsetTop;
60+
61+
function isFixed() {
62+
if (navTop >= window.scrollY) {
63+
document.body.classList.remove('fixed');
64+
document.body.style.paddingTop = 0;
65+
} else {
66+
document.body.classList.add('fixed');
67+
document.body.style.paddingTop = nav.offsetHeight + 'px';
68+
}
69+
}
70+
71+
document.addEventListener('scroll', isFixed);
72+
5873
</script>
5974

6075
</body>

24 - Sticky Nav/style-START.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ nav {
4646
transition:all 0.5s;
4747
position: relative;
4848
z-index: 1;
49+
50+
}
51+
.fixed nav {
52+
position: fixed;
4953
}
5054

5155
nav ul {
@@ -72,6 +76,10 @@ li.logo {
7276
font-size: 30px;
7377
}
7478

79+
.fixed .logo {
80+
max-width: 500px;
81+
}
82+
7583
li.logo a {
7684
color:black;
7785
}

0 commit comments

Comments
 (0)