This is related to Intersection observer does not work with target with position: fixed
but my issue is that interection observer don't fire on element with position: absolute. I have jQuery UI dialog and when it show up observer don't fire.
Here is my code:
var self = $('<div/>').appendTo('body').dialog({
autoOpen: false
})
var first = true;
function visibility_checker(x) {
if (first) {
first = false; // ignore initial call
} else {
console.log(x);
}
}
setTimeout(function() {
self.dialog('open');
}, 4000);
if (window.IntersectionObserver) {
var visibility_observer = new IntersectionObserver(visibility_checker, {
root: document.body
});
visibility_observer.observe(self[0]);
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
Without jQuery UI
var self = $('<div class="x"/>').hide().appendTo('body');
var first = true;
function visibility_checker(x) {
if (first) {
first = false; // ignore initial call
} else {
console.log(x);
}
}
setTimeout(function() {
self.show();
}, 4000);
if (window.IntersectionObserver) {
var visibility_observer = new IntersectionObserver(visibility_checker, {
root: document.body
});
visibility_observer.observe(self[0]);
}
.x {
width: 100px;
height: 100px;
background: red;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
Anyone know what the issue is and why it don't fire?
Found the issue the problem is position: absolute, don't know why but setting:
body {
position: relative;
}
fixed the issue, maybe absolute need to have reference. Also according to MDN you can use null as root. That also works.