“smooth-scroll.js” Kode Jawaban

JavaScript Gulir halus ke elemen jangkar

//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>
Grepper

cara menghaluskan gulir di javascript

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

smooth-scroll.js

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/smooth-scroll/16.1.0/smooth-scroll.min.js"></script>
Super Skunk

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya