Intersection Observer
Intersection Observer
const main = document.querySelectorAll("h1"); <<< [It will select all h1]
const observer = new IntersectionObserver((entries) =>{ [all h1 are in the form of events in entries as array]
console.log(entries); [if you have 3 h1 you will see 3 intersection observer as items inside array]
entries.forEach((entry) =>{ [inside that array each intersection obeserver as selected using foreach loop]
if(entry.isIntersecting){ [ entry refers to single intersection observer from 3 of them (3 ota h1 maddhe 1 ota)]
console.log("THIS IS entry"); [ isIntersecting means whether it is on screen or not ]
console.log(entry); [ if any of entry (i.e. h1) is being displayed under screen then this if statement runs ]
entry.target.classList.add("show"); [ when h1 is being displaying on screen then another class is added to h1 which content CSS property ]
}
})
},{
threshold: 1, [It means 100% of h1 should be seen, then only if statement will run]
rootMargin: "-250px" [It will reduce that screen size, so that on exact time data can be displayed +ve increases screen size]
});
main.forEach((events) =>{ [This means we want to run intersection observer for all h1, each and every h1 are executed, if there is single element no need to use for loop]
observer.observe(events);
})
Code without any notices :
const main = document.querySelectorAll("h1");
const observer = new IntersectionObserver((entries) =>{
console.log(entries);
entries.forEach((entry) =>{
if(entry.isIntersecting){
console.log("THIS IS entry");
console.log(entry);
entry.target.classList.add("show");
}
})
},{
threshold: 1,
rootMargin: "-250px"
});
main.forEach((events) =>{
observer.observe(events);
})
Comments
Post a Comment