AFlyingLemon
AFlyingLemon

Reputation: 19

Scroll to certain position from top of window on click

Anyone know how I can scroll to a certain position on the page when clicking a link using JS? For instance, on click, scroll to a position 500px from the top of the window.

I am using the ScrollMagic plugin and my website content is being activated by scroll position so it is not possible for me to just use anchor links. Also it cannot be offset from the current position as this would not work either.

Any ideas?

Upvotes: 1

Views: 4783

Answers (3)

Stefan Stefko
Stefan Stefko

Reputation: 390

I reccomand doing it by jQuery :) Here is working on every device version

$(document).ready(function()  //When the page is ready, load function
{
    $("#some_id").click(function()  // When arrow is clicked
    {
        $("body,html").animate(
        {
            scrollTop : 500                       // Scroll 500px from top of body
        }, 400);  //how fast the scrolling animation will be in miliseconds
    });
});

Upvotes: 1

Sunny Patel
Sunny Patel

Reputation: 8077

Would something like this work? It'll give you a smooth scroll to that location for whatever link you attach it to.

$('a[href*=#]').click(function() {
    $('html, body').animate({scrollTop: 500}, 500);
}

Upvotes: 1

Justin Herter
Justin Herter

Reputation: 590

This should do the trick in pure js:

document.body.scrollTop = 500;

Upvotes: 4

Related Questions