Determines if a given element is currently visible in the viewport.
A CSS selector string or an HTMLElement.
true if the element is in the viewport, false otherwise.
true
false
Useful for lazy loading, animations on scroll, or tracking visible content.
//ES Moduleimport { isElementInViewport } from '@bnidev/js-utils'// CommonJSconst { isElementInViewport } = require('@bnidev/js-utils') Copy
//ES Moduleimport { isElementInViewport } from '@bnidev/js-utils'// CommonJSconst { isElementInViewport } = require('@bnidev/js-utils')
// With a CSS selectorif (isElementInViewport('#my-element')) { console.log('Visible')}// With an elementconst el = document.getElementById('my-element')if (el && isElementInViewport(el)) { console.log('Also visible')} Copy
// With a CSS selectorif (isElementInViewport('#my-element')) { console.log('Visible')}// With an elementconst el = document.getElementById('my-element')if (el && isElementInViewport(el)) { console.log('Also visible')}
Determines if a given element is currently visible in the viewport.