有没有办法将其他参数传递给 scrollTarget 回调函数

Is there a way to pass other parameters to the scrollTarget callback function?

本文关键字:scrollTarget 回调 函数 参数传递 其他 有没有      更新时间:2023-09-26

我正在查看ScrollMagic的文档。我看到我可以使用回调函数来更改滚动行为,如下所示:

controller.scrollTo(function (newScrollPos) {
    $("body").animate({scrollTop: newScrollPos});
});

有没有办法向这个函数传递更多参数?对于某些特定的链接/锚点,我想使用偏移量。我一直无法弄清楚如何传递更多参数,或者如何从回调中获取 ScrollTarget。

此函数仅用于接收一个数字,因为它应该做的只是创建一个移动到该数字的自定义滚动行为。

但是,您可以使用 this 在回调中引用控制器。

将元素或场景位置

转换为滚动位置的功能(如果向controller.scrollTo提供元素或场景)甚至在调用自定义回调之前处理。

因此,要让它在特定情况下使用某些偏移量,您需要在实际调用函数之前处理它。
例如,在您的点击处理程序中。

下面是一些伪代码:

anchor.on(click, function() {
    if (this is an exception) {
         var customPos = targetElement.offset().top + offset;
         controller.scrollTo(customPos);
    } else {
         controller.scrollTo(targetElement);
    }
});