HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

Scroll to an element with jQuery

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
jqueryscrollelementwith

Problem

I have this input element:



Then I have some other elements, like other tag's & ` tag's, etc...

When the user clicks on the
, the page should scroll to the page's last element, and it should do so with a nice animation (It should be a scroll to bottom and not to top).

The last item of the page is a
submit button with #submit`:



The animation should not be too fast and should be fluid.

I am running the latest jQuery version. I prefer to not install any plugin but to use the default jQuery features to achieve this.

Solution

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.






$(document).ready(function (){
$("#click").click(function (){
$('html, body').animate({
scrollTop: $("#div1").offset().top
}, 2000);
});
});


Test




Test 2

Click me

Code Snippets

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

Context

Stack Overflow Q#6677035, score: 4557

Revisions (0)

No revisions yet.