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 :: how to execute javascript after c# function execute 
Javascript :: jquery check if checkbox is not checked 
Javascript :: change label value jquery 
Javascript :: rxjs cdn 
Javascript :: lodash combine permissions 
Javascript :: angular 8 how to iterate json object in view 
Javascript :: twitter icon in next js 
Javascript :: js input text set value 
Javascript :: react split array into chunks 
Javascript :: fs file already exists 
Javascript :: neffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 
Javascript :: how send to another page by router in vuejs 
Javascript :: loop through object and get a certain values 
Javascript :: check object has value 
Javascript :: random string generator node js 
Javascript :: get x characters from string javascript 
Javascript :: how to copy text on clipboard in react 
Javascript :: fs write stream append 
Javascript :: javascript get width of a div 
Javascript :: discord js user has role 
Javascript :: how to print to console javascript 
Javascript :: datatable processing 
Javascript :: loop over javascript using foreach 
Javascript :: javascript remove space from string 
Javascript :: codewars js Find the first non-consecutive number 
Javascript :: how to position View absolute react native 
Javascript :: get the last option from select jquery 
Javascript :: javascript loop thrown object 
Javascript :: javascript how to sort nodes from dom 
Javascript :: javascript disable copy paste 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =