gpt4 book ai didi

javascript - 在 codepen 中,Intersection 观察者的行为与浏览器窗口不同

转载 作者:行者123 更新时间:2023-12-01 16:10:14 24 4
gpt4 key购买 nike

如果我写 this codes在单独的 HTML、CSS 和 Javascript 文件中并用浏览器打开它,当目标在视口(viewport)高度的中间观察时会出现粘性共享栏,但在 codepen 中当目标在视口(viewport)高度的底部观察时会出现。是什么原因?

{
class StickyShareBar {
constructor(element) {
this.element = element;
this.contentTarget = document.getElementsByClassName('js-sticky-sharebar-target');
this.showClass = 'sticky-sharebar--on-target';
this.threshold = '50%';
this.initShareBar();
}

initShareBar() {
if(this.contentTarget.length < 1) {
this.element.addClass( this.showClass);
return;
}
if(intersectionObserverSupported) {
this.initObserver();
} else {
this.element.addClass(this.showClass);
}
}

initObserver() {
const self = this;
var observer = new IntersectionObserver(
function(entries, observer) {
self.element.classList.toggle( self.showClass, entries[0].isIntersecting);
},
{
rootMargin: "0px 0px -"+this.threshold+" 0px"}
);
observer.observe(this.contentTarget[0]);
}
}

const stickyShareBar = document.getElementsByClassName('js-sticky-sharebar'),
intersectionObserverSupported = ('IntersectionObserver' in window && 'IntersectionObserverEntry' in window && 'intersectionRatio' in window.IntersectionObserverEntry.prototype);

new StickyShareBar(stickyShareBar[0]);
}

最佳答案

关于javascript - 在 codepen 中,Intersection 观察者的行为与浏览器窗口不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61911268/

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