来自IntersectionObserver的Observer函数调用返回为undefined

时间:2018-05-25 19:44:40

标签: javascript intersection-observer

我正在尝试确定slot元素是否有滚动条。如果是的话,我想添加一个节目全部/显示插槽旁边的更少链接,这将相应地扩展或收缩。

我正在使用IntersectionObserver,因为它似乎是针对这个用例而设计的。该插槽包含一个ul,它本身具有可变数量的li子节点。直到添加show more / show链接的函数被触发后才会填充ul - 因此使用IntersectionObserver。我写的函数应该返回一个布尔值:

_isOverflowing( rootElem, childElem ) {
    // rootElem is the slot; childElem is the ul
    let observer;

    const options = {
        root: rootElem,
        threshold: 1.0
    };

    function handleIntersect(entries, observer) {
        entries.forEach(entry => {
            return entry.intersectionRatio >= options.threshold
        });
    }

    observer = new IntersectionObserver(handleIntersect, options);
    return observer.observe( childElem );
}

我所看到的是最后的功能

observer.observe( childElem )

返回undefined。我做错了什么?

0 个答案:

没有答案