“cara memeriksa elemen ada di viewport” Kode Jawaban

Javascript di Viewport

var isInViewport = function (elem) {
    var bounding = elem.getBoundingClientRect();
    return (
        bounding.top >= 0 &&
        bounding.left >= 0 &&
        bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
};
Fancy Flamingo

cara memeriksa elemen ada di viewport

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

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

cara memeriksa apakah elemen ada di 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)
Miss Skooter

Jawaban yang mirip dengan “cara memeriksa elemen ada di viewport”

Pertanyaan yang mirip dengan “cara memeriksa elemen ada di viewport”

Lebih banyak jawaban terkait untuk “cara memeriksa elemen ada di viewport” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya