-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
395b4f5
commit af25c11
Showing
5 changed files
with
729 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Element.getBoundingClientRect() method returns the size of an element and its position relative to the viewport. | ||
// pageYOffset is a read - only window property that returns the number of pixels the document has been scrolled vertically. | ||
// slice extracts a section of a string without modifying original string | ||
//offsetTop - A Number, representing the top position of the element, in pixels | ||
|
||
// ********** set date ************ | ||
// select span | ||
const date = document.getElementById("date"); | ||
date.innerHTML = new Date().getFullYear(); | ||
|
||
// ********** close links ************ | ||
const navToggle = document.querySelector(".nav-toggle"); | ||
const linksContainer = document.querySelector(".links-container"); | ||
const links = document.querySelector(".links"); | ||
|
||
navToggle.addEventListener("click", function () { | ||
// linksContainer.classList.toggle("show-links"); | ||
|
||
const linksHeight = links.getBoundingClientRect().height; | ||
const containerHeight = linksContainer.getBoundingClientRect().height; | ||
if (containerHeight === 0) { | ||
linksContainer.style.height = `${linksHeight}px`; | ||
} else { | ||
linksContainer.style.height = 0; | ||
} | ||
// console.log(linksContainer.getBoundingClientRect()); | ||
}); | ||
|
||
// ********** fixed navbar ************ | ||
|
||
const navbar = document.getElementById("nav"); | ||
const topLink = document.querySelector(".top-link"); | ||
|
||
window.addEventListener("scroll", function () { | ||
const scrollHeight = window.pageYOffset; | ||
const navHeight = navbar.getBoundingClientRect().height; | ||
if (scrollHeight > navHeight) { | ||
navbar.classList.add("fixed-nav"); | ||
} else { | ||
navbar.classList.remove("fixed-nav"); | ||
} | ||
// setup back to top link | ||
|
||
if (scrollHeight > 500) { | ||
topLink.classList.add("show-link"); | ||
} else { | ||
topLink.classList.remove("show-link"); | ||
} | ||
}); | ||
|
||
// ********** smooth scroll ************ | ||
// select links | ||
const scrollLinks = document.querySelectorAll(".scroll-link"); | ||
scrollLinks.forEach((link) => { | ||
link.addEventListener("click", (e) => { | ||
// prevent default | ||
e.preventDefault(); | ||
// navigate to specific spot | ||
const id = e.currentTarget.getAttribute("href").slice(1); | ||
const element = document.getElementById(id); | ||
|
||
const navHeight = navbar.getBoundingClientRect().height; | ||
const containerHeight = linksContainer.getBoundingClientRect().height; | ||
const fixedNav = navbar.classList.contains("fixed-nav"); | ||
let position = element.offsetTop - navHeight; | ||
|
||
if (!fixedNav) { | ||
position = position - navHeight; | ||
} | ||
if (navHeight > 82) { | ||
position = position + containerHeight; | ||
} | ||
|
||
window.scrollTo({ | ||
left: 0, | ||
top: position, | ||
}); | ||
// close | ||
linksContainer.style.height = 0; | ||
}); | ||
}); | ||
// calculate heights |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Scroll</title> | ||
<!-- font-awesome --> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" /> | ||
<!-- styles --> | ||
<link rel="stylesheet" href="styles.css" /> | ||
</head> | ||
|
||
<body> | ||
<header id="home"> | ||
<!-- navbar --> | ||
<nav id="nav"> | ||
<div class="nav-center"> | ||
<!-- nav header --> | ||
<div class="nav-header"> | ||
<img src="./logo.svg" class="logo" alt="logo" /> | ||
<button class="nav-toggle"> | ||
<i class="fas fa-bars"></i> | ||
</button> | ||
</div> | ||
<!-- links --> | ||
<div class="links-container"> | ||
<ul class="links"> | ||
<li> | ||
<a href="#home" class="scroll-link">home</a> | ||
</li> | ||
<li> | ||
<a href="#about" class="scroll-link">about</a> | ||
</li> | ||
<li> | ||
<a href="#services" class="scroll-link">services</a> | ||
</li> | ||
<li> | ||
<a href="#tours" class="scroll-link">tours</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</nav> | ||
<!-- banner --> | ||
<div class="banner"> | ||
<div class="container"> | ||
<h1>scroll project</h1> | ||
<p> | ||
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quas eos | ||
neque sunt in? Id, necessitatibus quos quisquam distinctio | ||
laudantium fugiat? | ||
</p> | ||
<a href="#tours" class="scroll-link btn btn-white">explore tours</a> | ||
</div> | ||
</div> | ||
</header> | ||
<!-- about --> | ||
<section id="about" class="section"> | ||
<div class="title"> | ||
<h2>about <span>us</span></h2> | ||
</div> | ||
</section> | ||
<!-- services --> | ||
<section id="services" class="section"> | ||
<div class="title"> | ||
<h2>our <span>services</span></h2> | ||
</div> | ||
</section> | ||
<!-- contact --> | ||
<section id="tours" class="section"> | ||
<div class="title"> | ||
<h2>featured <span>tours</span></h2> | ||
</div> | ||
</section> | ||
<!-- footer --> | ||
<footer class="section"> | ||
<p> | ||
copyright © backroads travel tours company | ||
<span id="date"></span>. all rights reserved | ||
</p> | ||
</footer> | ||
<a class="scroll-link top-link" href="#home"> | ||
<i class="fas fa-arrow-up"></i> | ||
</a> | ||
<!-- javascript --> | ||
<script src="app.js"></script> | ||
</body> | ||
|
||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.