JavaScript How To Scroll To Specific Element with Animation

Reading Time: 5 minutes
131 Views

Inside this article we will see the concept i.e JavaScript How To Scroll To Specific Element with Animation Tutorial. Article contains the classified information about How To Scroll To a Specific Position of a Webpage in JavaScript / jQuery.

If you are looking for a solution i.e jQuery Smooth Animated Scroll to any specific location of a webpage Using jQuery / Javascript then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Read More: CodeIgniter 4 How To Find Data Using Multiple Ids Tutorial

Let’s get started.

Application Programming

Create a folder say smooth-scroll in your localhost directory. Create a file index.html into it.

index.html

Concept

Specify Element Selector

First you will specify the selector element where you need to scroll. Here, in this example h3 tag is specified with an ID value.

<h3 id="scroll_to_me">Scroll To Me</h3>

Javascript logic to scroll to element

Read More: How To Use Codeigniter 4 in Web Development

This code triggers when page loads. You can bind with any other javascript events.

$(document).ready(function() {
    $([document.documentElement, document.body]).animate({
        scrollTop: $("#scroll_to_me").offset().top
    }, 2000);
});

animate() is a jquery plugin method which scrolls you down with an animation effect.

Application Testing

Open browser and type your application URL:

URL: http://localhost/smooth-scroll/index.html

Whenever you load the page, automatically it will scrolls you down with the animation effect.

We hope this article helped you to learn JavaScript How To Scroll To Specific Element with Animation Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.