Scroll to an element with jQuery Scroll to an element with jQuery jquery jquery

Scroll to an element with jQuery


Assuming you have a button with the id button, try this example:

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

I got the code from the article Smoothly scroll to an element without a jQuery plugin. And I have tested it on the example below.

<html>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>    <script>        $(document).ready(function (){            $("#click").click(function (){                $('html, body').animate({                    scrollTop: $("#div1").offset().top                }, 2000);            });        });    </script>    <div id="div1" style="height: 1000px; width 100px">        Test    </div>    <br/>    <div id="div2" style="height: 1000px; width 100px">        Test 2    </div>    <button id="click">Click me</button></html>