Skip to content

Commit 5530dc5

Browse files
committed
10 scroll project new setup
1 parent 1a979f1 commit 5530dc5

File tree

3,306 files changed

+196
-111
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,306 files changed

+196
-111
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"git.ignoreLimitWarning": true
3+
}

10-scroll/final/app.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Element.getBoundingClientRect() method returns the size of an element and its position relative to the viewport.
2+
3+
// pageYOffset is a read - only window property that returns the number of pixels the document has been scrolled vertically.
4+
// slice extracts a section of a string without modifying original string
5+
//offsetTop - A Number, representing the top position of the element, in pixels
6+
7+
// ********** set date ************
8+
// select span
9+
const date = document.getElementById("date");
10+
date.innerHTML = new Date().getFullYear();
11+
12+
// ********** close links ************
13+
const navToggle = document.querySelector(".nav-toggle");
14+
const linksContainer = document.querySelector(".links-container");
15+
const links = document.querySelector(".links");
16+
17+
navToggle.addEventListener("click", function () {
18+
// linksContainer.classList.toggle("show-links");
19+
20+
const linksHeight = links.getBoundingClientRect().height;
21+
const containerHeight = linksContainer.getBoundingClientRect().height;
22+
if (containerHeight === 0) {
23+
linksContainer.style.height = `${linksHeight}px`;
24+
} else {
25+
linksContainer.style.height = 0;
26+
}
27+
// console.log(linksContainer.getBoundingClientRect());
28+
});
29+
30+
// ********** fixed navbar ************
31+
32+
const navbar = document.getElementById("nav");
33+
const topLink = document.querySelector(".top-link");
34+
35+
window.addEventListener("scroll", function () {
36+
const height = window.pageYOffset;
37+
if (height > 96) {
38+
navbar.classList.add("fixed-nav");
39+
} else {
40+
navbar.classList.remove("fixed-nav");
41+
}
42+
// setup back to top link
43+
if (height > 500) {
44+
topLink.classList.add("show-link");
45+
} else {
46+
topLink.classList.remove("show-link");
47+
}
48+
});
49+
50+
// ********** smooth scroll ************
51+
// select links
52+
const scrollLinks = document.querySelectorAll(".scroll-link");
53+
scrollLinks.forEach((link) => {
54+
link.addEventListener("click", (e) => {
55+
// prevent default
56+
e.preventDefault();
57+
// navigate to specific spot
58+
const id = e.currentTarget.getAttribute("href").slice(1);
59+
const element = document.getElementById(id);
60+
// calculate heights
61+
const navHeight = navbar.getBoundingClientRect().height;
62+
const containerHeight = linksContainer.getBoundingClientRect().height;
63+
const fixedNav = navbar.classList.contains("fixed-nav");
64+
let position = element.offsetTop - navHeight;
65+
66+
if (!fixedNav) {
67+
position = position - navHeight;
68+
}
69+
if (navHeight > 82) {
70+
position = position + containerHeight;
71+
}
72+
window.scrollTo({
73+
left: 0,
74+
top: position,
75+
});
76+
// close
77+
linksContainer.style.height = 0;
78+
});
79+
});

0 commit comments

Comments
 (0)