Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to check element is in viewport

function isVisible (ele) {
  const { top, bottom } = ele.getBoundingClientRect();
  const vHeight = (window.innerHeight || document.documentElement.clientHeight);

  return (
    (top > 0 || bottom > 0) &&
    top < vHeight
  );
}
Comment

how to check if element is in viewport

function isInViewport(element) {
    const rect = element.getBoundingClientRect();
    return (
        rect.top >= 0 &&
        rect.left >= 0 &&
        rect.bottom <= ((window.innerHeight + rect.height) || document.documentElement.clientHeight) &&
        rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
}

//optimized from the stackOverflow answer to account 
//for element heights and widths (in vertical/horizontal scrolling)
Comment

how to check if element is in viewport javascript

function elementInViewport(el) {
  let top = el.offsetTop;
  let left = el.offsetLeft;
  let width = el.offsetWidth;
  let height = el.offsetHeight;

  while(el.offsetParent) {
    el = el.offsetParent;
    top += el.offsetTop;
    left += el.offsetLeft;
  }

  return (
    top >= window.pageYOffset &&
    left >= window.pageXOffset &&
    (top + height) <= (window.pageYOffset + window.innerHeight) &&
    (left + width) <= (window.pageXOffset + window.innerWidth)
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery scroll width 
Javascript :: array loop js 
Javascript :: increase font size in jsx 
Javascript :: get offset of element relative to parent 
Javascript :: how to move a channel to a category discord js 
Javascript :: Delete spaces in text in javascript 
Javascript :: nodejs fs directory exists 
Javascript :: javascript change data attribute value 
Javascript :: get unique numbers of an array in javascript using for loop 
Javascript :: style display block js 
Javascript :: node get unix timestamp 
Javascript :: nodemon compile typescript and execute js file 
Javascript :: president zelensky 
Javascript :: npm i react query 
Javascript :: print a specific div in javascript 
Javascript :: get width of a dom element js 
Javascript :: jquery loop over elements 
Javascript :: find object length in javascript 
Javascript :: mouseevent clientx javascript 
Javascript :: url.parse deprecated 
Javascript :: react chrome get language 
Javascript :: uuid use in express 
Javascript :: getype js 
Javascript :: how to read a firebase txt file 
Javascript :: create react app in current folder 
Javascript :: create react app with tailwind 
Javascript :: js remove specific css property 
Javascript :: start peerjs server 
Javascript :: javascript recorrer json 
Javascript :: html form clear 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =