I know a variation on this has been asked several times; I\'ve been browsing SO for a while now but either I\'m doing something wrong or I haven\'t found what I need.
<
All modern Browsers support this. Visit: http://caniuse.com/#search=scrollIntoView
function scrollIntoViewIfNeeded(target) {
if (target.getBoundingClientRect().bottom > window.innerHeight) {
target.scrollIntoView(false);
}
if (target.getBoundingClientRect().top < 0) {
target.scrollIntoView();
}
}
Update
The target has to be an Element. If you use jQuery call the function like this:
scrollIntoViewIfNeeded($(".target")[0]);