I'm trying to increase only the top value of the rootMargin by 50px. The code I'm using for the option is rootMargin: '50px 0px 0px 0px'
. And that isn't working.
Although using rootMargin: '50px 0px'
can work for my case, I have no desire to increase the bottom value.
The full test code is listed below. I did not create a demo on jsFiddle because jsFiddle wraps the result in an iframe, which would be the root
. I cannot set that iframe as the root on jsFiddle so the demo would not work.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>rootMargin test</title>
<style>
* {
margin: 0;
padding: 0;
}
#full-height {
margin-bottom: 100px;
height: 100vh;
background-color: lightgreen;
}
#observee {
background-color: lightblue;
}
</style>
</head>
<body>
<div id="full-height"></div>
<p id="observee">I'm being observed.</p>
<script>
var observer = new IntersectionObserver(
function(entries) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
console.log('Intersected.');
}
});
},
{
rootMargin: '50px 0px 0px 0px'
}
);
observer.observe(document.querySelector('#observee'));
</script>
</body>
</html>