Skip to content

Instantly share code, notes, and snippets.

View bibek-magar's full-sized avatar
♠️
Learning

Bibek Magar bibek-magar

♠️
Learning
View GitHub Profile
@bibek-magar
bibek-magar / smooth-scroll.js
Created April 4, 2024 04:08
Smooth scrolling to section in Vanilla JS
function clickHandler(e) {
e.preventDefault()
const href = this.getAttribute('href')
const offsetTop = document.querySelector(href).offsetTop
scroll({
top: offsetTop - 90,
behavior: 'smooth',
})
}
@bibek-magar
bibek-magar / highlight-navbar.js
Last active April 4, 2024 04:13
Vanilla JS code to highlight the navigation.
/*
To check whether the given element in view port by visibility percentage
Tweak the number (20) as you need for the visibility percentage
*/
function isInViewport(element) {
const rect = element.getBoundingClientRect()
const viewportWidth =
window.innerWidth || document.documentElement.clientWidth
const viewportHeight =
window.innerHeight || document.documentElement.clientHeight