Css Smooth Scroll To Anchor Code Example


Example 1: javascript smooth scroll to anchor element


//add smooth scrolling when clicking any anchor link
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
//<a href="#someOtherElementID"> Go to Other Element Smoothly </a>

Example 2: smooth scroll css


html {
scroll-behavior: smooth;
}

/* No support in IE, or Safari
You can use this JS polyfill for those */

http://iamdustan.com/smoothscroll/

Example 3: how to smooth scroll in javascript


window.scrollTo({ top: 900, behavior: 'smooth' })

Example 4: smooth scroll


// Scroll to specific values
// scrollTo is the same
window.scroll({
top: 2500,
left: 0,
behavior: 'smooth'
});

// Scroll certain amounts from current position
window.scrollBy({
top: 100, // could be negative value
left: 0,
behavior: 'smooth'
});

// Scroll to a certain element
document.querySelector('.hello').scrollIntoView({
behavior: 'smooth'
});

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable