📌  相关文章
📜  元素在视口中时的 javascript 触发函数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:38.533000             🧑  作者: Mango

代码示例1
// Determine if an element is in the visible viewport
function isInViewport(element) {
  var rect = element.getBoundingClientRect();
  var html = document.documentElement;
  return (
    rect.top >= 0 &&
    rect.left >= 0 &&
    rect.bottom <= (window.innerHeight || html.clientHeight) &&
    rect.right <= (window.innerWidth || html.clientWidth)
  );
}
// The above function could be used by adding a “scroll” event listener to the window and then calling isInViewport().