简体   繁体   English

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

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

I was taking a look at ScrollMagic's documentation. 我正在看ScrollMagic的文档。 I see that I can use a callback function to change the scroll behavior as shown below: 我看到我可以使用回调函数来更改滚动行为,如下所示:

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

Is there a way to pass more parameters to this function? 有没有办法将更多参数传递给该函数? For some specific links/anchors, I would like to use an offset. 对于某些特定的链接/锚点,我想使用一个偏移量。 I have not been able to figure out how to pass more parameters, or how to get the ScrollTarget from within the callback. 我还无法弄清楚如何传递更多参数,或者如何从回调中获取ScrollTarget。

this function is only meant to receive a number as all it should do is create a custom scroll behavior moving to that number. 此功能仅用于接收数字,因为它要做的就是创建移动到该数字的自定义滚动行为。

You can however reference the controller inside the callback using this . 但是,您可以使用this在回调内部引用控制器。

The functionality of converting element or scene positions to scroll positions (if you supply an element or a scene to controller.scrollTo ) is handled before the custom callback is even called. 将元素或场景位置转换为滚动位置(如果您向controller.scrollTo提供了元素或场景)的功能在甚至调用自定义回调之前就已处理。

So to get it to work with certain offsets for specific cases, you'll need to handle that before the function is actually called. 因此,要使其在特定情况下与某些偏移量一起使用,您需要在实际调用函数之前进行处理。
For example in your click handler. 例如,在您的点击处理程序中。

Here's some pseudocode: 这是一些伪代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM