Jquery Scroll To Specific Element

Scroll To Specific Element Using Jquery scrollTop function

September 14, 2020

With Jquery scrollTop function we can scroll to any element.

    <section id="sec_scrollto">Scroll here</section>
    <section id="sec_scrolltoWithAnimation">Scroll here With Animation</section>
 
    <a href="javascript:void" id="a_scrolltoTop">Scroll To Top</a>
    <a href="javascript:void" id="a_scrolltoElement">Scroll To Element</a>
    <a href="javascript:void" id="a_scrolltoElementWithAnimation">Scroll To Element With Animation</a>
 
    <script>
 
        // SCROLL TO SPECIFIC ELEMENT (SECTION WITH ID "sec_scrollto")
        $(document).on("click""#a_scrolltoElement"function () {
            $(window).scrollTop($("#sec_scrollto").position().top);
        });
 
        // SCROLL TO SPECIFIC ELEMENT WITH ANIMATION (SECTION WITH ID "sec_scrollto")
        $(document).on("click"'#a_scrolltoElementWithAnimation'function () {
            $('html, body').animate({
                scrollTop: $("#sec_scrolltoWithAnimation").offset().top
            }, 1000);
        });
 
        // SCROLL TO TOP
        $(document).on("click"'#a_scrolltoTop'function () {
            $('html, body').animate({
                scrollTop: 0
            }, 800);
            return false;
        });
 
    </script>

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...