1 回答

TA貢獻1877條經驗 獲得超1個贊
只有第一個發生了變化,因為這就是您通過僅解構第一個數組元素來更改顏色函數的目標:
const [entry] = entries;
但是,InteractionObserver回調不是針對每個條目調用的,而是針對同時觸發的所有條目調用的;因此該entries數組包含所有被觀察的項目,您需要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
});
};
添加回答
舉報