gpt4 book ai didi

javascript - Intersection Observer 不适用于 jQuery UI 对话框

转载 作者:行者123 更新时间:2023-11-30 19:35:07 25 4
gpt4 key购买 nike

这与 Intersection observer does not work with target with position: fixed 有关

但我的问题是,interection observer 不会在具有 position: absolute 的元素上触发。我有 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"/>

有人知道问题是什么以及为什么它没有触发吗?

最佳答案

发现问题是position: absolute, 不知道为什么但是setting:

body {
position: relative;
}

解决了这个问题,也许绝对需要引用。也根据MDN您可以使用 null 作为根。这也有效。

关于javascript - Intersection Observer 不适用于 jQuery UI 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56021611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com