![](/img/trans.png)
[英]Javascript to detect if a div is contained inside another and when is not anymore
[英]Javascript: move a DIV and detect when it's fully inside another DIV?
您需要使用路口觀察員 API
const options = {
root: document.querySelector('CONTAINER SELECTOR'),
rootMargin: '0px',
threshold: 1.0
}
const targetToWatch = document.querySelector('INSIDE ITEM SELECTOR');
let callback = (entries, observer) => {
entries.forEach(entry => {
// Each entry describes an intersection change for one observed
// target element:
// entry.boundingClientRect
// entry.intersectionRatio
// entry.intersectionRect
// entry.isIntersecting
// entry.rootBounds
// entry.target
// entry.time
});
};
const observer = new IntersectionObserver(callback, options);
observer.observe(targetToWatch);
然后您需要檢查各種entry.
參數來知道元素是否在里面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.