这与Intersection observer does not work with target with position: fixed
有关但是我的问题是,交互观察器不会对位置为绝对的元素进行触发。我有jQuery UI对话框,当它显示观察者时不要触发。
这是我的代码:
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"/>
没有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"/>
任何人都知道问题是什么,为什么不触发?
答案 0 :(得分:1)