但我的问题是,交互观察者不会在具有位置的元素上触发:绝对。我有 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"/>
任何人都知道问题是什么以及为什么它不触发?