-
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
b612a20
commit 9edefcf
Showing
1 changed file
with
27 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,27 @@ | ||
const defaultConfig = { | ||
// Where to align the element | ||
block: 'start', | ||
// Animation method | ||
behavior: 'smooth', | ||
} | ||
|
||
export default function(config = {}) { | ||
// Push defaults into config object | ||
config = { ...defaultConfig, ...config } | ||
// Get the elements marked for smooth scrolling | ||
const elements = document.querySelectorAll('[data-smoothie]') | ||
// For each element get href value | ||
Array.from(elements).forEach((element) => { | ||
// Save the href value from each element | ||
const link = element.getAttribute('href') | ||
// Create the click events | ||
element.addEventListener('click', (event) => { | ||
// Prevent default behavior | ||
event.preventDefault() | ||
// Load proper hash in the url | ||
history.pushState(null, null, link) | ||
// Scroll into view the element | ||
document.querySelector(link).scrollIntoView(config) | ||
}) | ||
}) | ||
} |