3000 explained code solutions for 75 technologies


javascriptCHeck if DOM element is visible in current viewport


var el = document.querySelector('div');
var rect = el.getBoundingClientRect();
var is_visible = rect.top >= 0 && rect.left >= 0 &&
                 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
                 rect.right <= (window.innerWidth || document.documentElement.clientWidth);ctrl + c
document.querySelector('div')

select needed DOM element

el.getBoundingClientRect()

gets element coordinates relative to viewport

is_visible

will contain true if element is visible in the current viewport