[英]How can I know If a webcomponent is on screen in litelement?
我正在用 litelement 做一個小項目,但是我想在顯示 webcomponent 時觸發一個事件,不僅是第一次,而且每次顯示組件時,我該怎么做?
提前致謝
Intersection Observer API將幫助您查找元素是否在屏幕上。 有關更多信息, 請訪問 MDN 文檔。
// 1. Observer options
let options = {
// The scroll area - mostly body or document
root: document.querySelector('#parent'),
rootMargin: '0px',
threshold: 1.0
};
let callback = () => { /* Do something */ };
// Observe Element
let observer = new IntersectionObserver(callback, options);
// Start observing
const targetElm = document.querySelector('#target');
observer.observe(targetElm);
除了 IE 之外的所有瀏覽器都支持 API,但已經提供了一個 polyfill: https : //github.com/w3c/IntersectionObserver
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.