我正在使用 Intersection Observer API 来帮助我实现视差效果。到目前为止,除了一种情况外,一切都运行良好:
当图像已经可见且位于页面中间时,只要图像不与视口边缘相交,就不会触发回调。因此,我无法使用从回调接收到的 boundingClientRect 更新视差平移的位置。
buildThresholdList(element) {
let thresholds = [];
let elementHeight = element.getBoundingClientRect().height;
for (var i=1.0; i<=elementHeight; i++) {
var ratio = i/elementHeight;
thresholds.push(ratio);
}
return thresholds;
}
createIntersectionObserver(element) {
const options = {
root: null,
threshold: this.buildThresholdList(element),
}
this.observer = new IntersectionObserver(this.intersectionObserverCallback, options);
}
intersectionObserverCallback(entries) {
entries.forEach(entry => {
let target = entry.target;
if (entry.isIntersecting) {
//calculate the translated value
this.getTranslateValue(entry);
}
}
}
即使图像完全可见并且不与任何东西相交,Intersection Observer API 是否有办法进行回调?