I've been diving into Intersection Observer by following a video tutorial closely and even copied the script code exactly as shown. Surprisingly, I encountered an error while testing the code. I stumbled upon someone else who faced the same issue but managed to solve it by switching from querySelectorAll to querySelector. Despite replicating their solution and making the change to querySelector in my code, the problem persisted. Below is the code snippet that I'm working on using VS Code live server.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel='stylesheet' href='style.css'>
<script src='script.js'></script>
</head>
<body>
<section class = 'section1'></section>
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
</body>
</html>
const sectionOne = document.querySelector('.section1');
const options = {};
const observer = new IntersectionObserver(function
(entries, observer){
entries.forEach(entry => {
console.log(entry);
});
}, options);
observer.observe(sectionOne);
body{
padding: 0;
margin: 0;
}
section{
height: 100vh;
width:100%;
}
section:nth-child(odd){
background: lightcoral
}