javascript trigger function when element is in viewport

Solutions on MaxInterview for javascript trigger function when element is in viewport by the best coders in the world

showing results for - "javascript trigger function when element is in viewport"
Salvatore
23 Jan 2020
1// Determine if an element is in the visible viewport
2function isInViewport(element) {
3  var rect = element.getBoundingClientRect();
4  var html = document.documentElement;
5  return (
6    rect.top >= 0 &&
7    rect.left >= 0 &&
8    rect.bottom <= (window.innerHeight || html.clientHeight) &&
9    rect.right <= (window.innerWidth || html.clientWidth)
10  );
11}
12// The above function could be used by adding a “scroll” event listener to the window and then calling isInViewport().