I am using the Intersection Observer API to help me with a parallax effect. So far everything work well except for one scenario:
When the image is already visible and in the middle of the page, as long as the image it not intersecting with the edge of the viewport, there is not callback trigger. So no way for me to update the position of the parallax translation with the boundingClientRect received from the callback.
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);
}
}
}
Is there a way with the Intersection Observer API to have a callback even though the image is fully visible and not intersecting with anything?