admin管理员组文章数量:1429993
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>
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>
Share
Improve this question
asked Oct 30, 2019 at 13:58
Ian Y.Ian Y.
2,4777 gold badges40 silver badges57 bronze badges
1
-
1
I have the exact same issue and can't explain why.
rootMargin
is ignored when I set 4 values, but it works when I set only 2 values. – Max Commented Feb 13, 2021 at 1:09
1 Answer
Reset to default 6While this is an older question, I think the following may help users with similar issues.
You can run the demo in JSFiddle - just change the root you want to use as the trigger. See the demo below to highlight this.
I think you may be confusing the rootMargin properties. From you question, it sounds like you want the observed element to trigger 50px before it es into view. If that is the case, then you want to increase the root bottom margin:
0px 0px 50px 0px
if you want to trigger the element only when it reaches 50px inside the root, then use:0px 0px -50px 0px
. You can leave off the last value like:0px 0px 50px
which would betop left/right bottom
See the demo below:
JSFiddle
本文标签: javascriptIntersection Observer cannot increase only the top rootMarginStack Overflow
版权声明:本文标题:javascript - Intersection Observer cannot increase only the top rootMargin - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745538436a2662379.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论