Skip to content

Commit dd8a5dc

Browse files
author
Kyle Bradshaw
committed
24 sticky nav --kb
1 parent 345e81e commit dd8a5dc

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

24 - Sticky Nav/index-START.html renamed to 24 - Sticky Nav/index.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Sticky Nav</title>
6-
<link rel="stylesheet" href="style-START.css">
6+
<link rel="stylesheet" href="style.css">
77
</head>
88
<body>
99

@@ -55,7 +55,24 @@ <h1>A story about getting lost.</h1>
5555

5656
<script>
5757

58+
const nav = document.querySelector('#main');
59+
const topOfNav = nav.offsetTop;
60+
61+
function fixNav() {
62+
console.log(topOfNav, window.scrollY, nav.offsetHeight);
63+
if (window.scrollY >= topOfNav) {
64+
document.body.style.paddingTop = `${nav.offsetHeight}px`;
65+
document.body.classList.add('fixed-nav');
66+
//causes it to be janky (no nav in dom) offset amt on body
67+
} else {
68+
document.body.style.paddingTop = 0;
69+
document.body.classList.remove('fixed-nav');
70+
71+
}
72+
}
73+
74+
window.addEventListener('scroll', fixNav);
5875
</script>
5976

6077
</body>
61-
</html>
78+
</html>

24 - Sticky Nav/style-START.css renamed to 24 - Sticky Nav/style.css

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ body {
2323
transition: transform 0.5s;
2424
}
2525

26+
.fixed-nav .site-wrap {
27+
transform: scale(1);
28+
}
29+
2630
header {
2731
text-align: center;
2832
height:50vh;
@@ -47,6 +51,10 @@ nav {
4751
position: relative;
4852
z-index: 1;
4953
}
54+
.fixed-nav nav {
55+
position: fixed;
56+
box-shadow: 0 5px rgba(0,0,0,0.1);
57+
}
5058

5159
nav ul {
5260
margin: 0;
@@ -64,14 +72,18 @@ nav li {
6472
}
6573

6674
li.logo {
67-
max-width:0;
75+
max-width:0;/*because transitions*/
6876
overflow: hidden;
6977
background: white;
7078
transition: all .5s;
7179
font-weight: 600;
7280
font-size: 30px;
7381
}
7482

83+
.fixed-nav li.logo {
84+
max-width: 500px; /*because transitions*/
85+
}
86+
7587
li.logo a {
7688
color:black;
7789
}

0 commit comments

Comments
 (0)