Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to check if element is visible after scrolling?

I'm loading elements by means of AJAX. Some of them are just noticeable in the event that you look down the page. Is there any way I can know whether an element is present in the noticeable piece of the page?
by

2 Answers

aashaykumar

function isScrolledIntoView(el) {
var rect = el.getBoundingClientRect();
var elemTop = rect.top;
var elemBottom = rect.bottom;

// Only completely visible elements return true:
var isVisible = (elemTop >= 0) && (elemBottom <= window.innerHeight);
// Partially visible elements return true:
//isVisible = elemTop < window.innerHeight && elemBottom >= 0;
return isVisible;
}
kshitijrana14
The best method I have found so far is the jQuery appear plugin. Works like a charm.
Mimics a custom "appear" event, which fires when an element scrolls into view or otherwise becomes visible to the user.
$('#foo').appear(function() {
$(this).text('Hello world');
});

This plugin can be used to prevent unnecessary requests for content that's hidden or outside the viewable area.

Login / Signup to Answer the Question.