1 回答

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
只有第一個(gè)發(fā)生了變化,因?yàn)檫@就是您通過僅解構(gòu)第一個(gè)數(shù)組元素來更改顏色函數(shù)的目標(biāo):
const [entry] = entries;
但是,InteractionObserver回調(diào)不是針對(duì)每個(gè)條目調(diào)用的,而是針對(duì)同時(shí)觸發(fā)的所有條目調(diào)用的;因此該entries數(shù)組包含所有被觀察的項(xiàng)目,您需要isIntersecting像這樣檢查屬性:
const changeColor = function(entries) {
entries.forEach(entry => {
if(entry.isIntersecting) {
entry.target.style.background = 'blue';
} else {
entry.target.style.background = 'red';
}
})
}
來自 MDN 文檔
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
});
};
添加回答
舉報(bào)