Check if Element is in view

Question: Check if element is in view and return boolean.

Answer:

function isInViewport(element) {

var rect = element.getBoundingClientRect();
var html = document.documentElement;

return (

rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || html.clientHeight) &&
rect.right <= (window.innerWidth || html.clientWidth)

);

}

var element = document.getElementById('hello');
alert(isInViewport(element));

Try it yourself: