2

The question is simple.

How to scroll browser to desired element or desired position by javascript?

Thanks a lot for the help!

2 Answers 2

9

To an element :

document.getElementById('id').scrollIntoView();

Has cross browser support and probably the easiest way to do it ....

or to a specific position :

window.scroll(x,y);

Docs for window.scroll() here

Sign up to request clarification or add additional context in comments.

Comments

1
//Finds y value of given object
function findPos(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    return [curtop];
    }
}
//Get object
var SupportDiv = document.getElementById('customer_info');

//Scroll to location of SupportDiv on load
window.scroll(0,findPos(SupportDiv));

1 Comment

Any idea why it's not always working on android mobile devices?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.