Use jQuery to scroll an element into view if it’s off the top of the page

Scrolling an element that is off the top of the page (but not the bottom) into view:

var tS = $(".element-i-want-in-view").offset().top - 150; // 150 is an offset in case you have a fixed header or something
if ($(document).scrollTop() > tS) { // only if the element is above the top of the window
    $('html, body').animate({
        scrollTop: tS
    }, 1000);
}

Share this Story

Related Posts

In the market for PHP (especially WordPress) hosting? You owe it to yourself to check out SiteGround. The reliability and performance is unmatched for the price and their support really is top notch.
There aren't a lot of web hosting providers that I would recommend, but SiteGround is at the top of the list.