Skip to content

Commit b30b35b

Browse files
committed
feat: Implementing Smooth Scrolling
1 parent 2ba018d commit b30b35b

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

starter/07-Omnifood-Desktop/css/general.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Medium 11px
6464
html {
6565
font-size: 62.5%;
6666
overflow-x: hidden;
67+
/* Does NOT work on Safari */
68+
/* scroll-behavior: smooth; */
6769
}
6870

6971
body {

starter/07-Omnifood-Desktop/index.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<li>
5555
<a
5656
class="main-nav-link nav-cta"
57-
href="#try-for-free"
57+
href="#cta"
5858
alt="Main Nav Section Item"
5959
>Try for free</a
6060
>
@@ -80,10 +80,12 @@ <h1 class="heading-primary">
8080
eat healthy again. Tailored to your personal tastes and
8181
nutritional needs.
8282
</p>
83-
<a href="#" class="btn btn--full margin-right-sm"
83+
<a href="#cta" class="btn btn--full margin-right-sm"
8484
>Start eating well</a
8585
>
86-
<a href="#" class="btn btn--outline">Learn more &darr;</a>
86+
<a href="#how-it-works" class="btn btn--outline"
87+
>Learn more &darr;</a
88+
>
8789
<div class="delivered-meals">
8890
<div class="delivered-img">
8991
<img src="img/customers/customer-1.jpg" alt="Customer photo" />
@@ -595,7 +597,7 @@ <h2 class="heading-secondary">
595597
</div>
596598
</section>
597599

598-
<section class="section-cta" id="try-for-free">
600+
<section class="section-cta" id="cta">
599601
<div class="container">
600602
<div class="cta">
601603
<div class="cta-text-box">
@@ -733,6 +735,10 @@ <h2 class="heading-secondary">Get your first meal for free!</h2>
733735
nomodule
734736
src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"
735737
></script>
738+
<script
739+
defer
740+
src="https://unpkg.com/smoothscroll-polyfill@0.4.4/dist/smoothscroll.min.js"
741+
></script>
736742
<script defer src="js/script.js"></script>
737743
</body>
738744
</html>

starter/07-Omnifood-Desktop/js/script.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ btnMobileNavEl.addEventListener("click", function () {
3030
headerEl.classList.toggle("nav-open");
3131
});
3232

33+
/////////////////////////////////////////////////////////////////////////////
34+
// Smooth scrolling animation
35+
const allLinks = document.querySelectorAll("a:link");
36+
allLinks.forEach((link) => {
37+
link.addEventListener("click", (e) => {
38+
e.preventDefault();
39+
const href = link.getAttribute("href");
40+
41+
// Scroll back to top
42+
if (href === "#") {
43+
window.scrollTo({
44+
top: 0,
45+
behavior: "smooth",
46+
});
47+
}
48+
49+
// Scroll to other links
50+
if (href !== "#" && href.startsWith("#")) {
51+
const section = document.querySelector(href);
52+
section.scrollIntoView({ behavior: "smooth" });
53+
}
54+
55+
// Close mobile navigation
56+
if (link.classList.contains("main-nav-link")) {
57+
headerEl.classList.toggle("nav-open");
58+
}
59+
});
60+
});
61+
3362
// https://unpkg.com/smoothscroll-polyfill@0.4.4/dist/smoothscroll.min.js
3463

3564
/*

0 commit comments

Comments
 (0)