Scrollto element with an id selector using jQuery

For example, if I have a button with an id “btn” and a form with the id “form” and I want to scrollto the form when I click on the button

HTML:

<p><a href="" id="btn">go to form</a></p>


<form id="form">

</form>

 

 

jQuery:

$(document).ready(function(){
	$("#btn").click(function(e){
            e.preventDefault();
            $('html, body').animate({
                scrollTop: $("#form").offset().top
            }, 1000);
        })
})

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s