jQuery —通过根据用户活动而变化的变量来更新函数的初始参数?

发布于 2024-11-02 06:38:02 字数 961 浏览 6 评论 0原文

我不明白如何在设置函数的初始参数后更新它们。

我正在使用一个名为 serialScroll 的插件。该插件滚动到初始 $(elem).serialScroll({//...}) 设置中指示的项目。

我想要做的是在用户与页面交互时重新定义该项目的名称。 我有另一个变量,当用户滚动时它可以成功获取信息,我就这么多了。

我想做的就是让serialScroll({)}使用该变量相应地更新它的“item:_”字段。

这是串行滚动代码:

    var nextClass = ".fig4"; 

    // this is being changed to .figN depending on the users 
    // scrolling position in the browser/viewport (successfully). 
    // For proof of communication purposes I forced nextClass 
    // here to equal .fig4 to see if the two are at lest talking to 
    // each other, which they are... but now how do i update "items:" 
    // below to reflect the change in the "nextClass" variable?

$('html').serialScroll({
    target:'body',
    items: nextClass, 
    next:'#nextImage',
    axis:'y',
    duration:700,
    force:true, 
});

I don't understand how to update a fucntion's initial parameters once they've been set.

I'm using a plugin called serialScroll. The plugin scrolls to items that are indicated in the initial $(elem).serialScroll({//...}) setup.

What i want to do is redefine the name of that item as the user interacts with the page.
I have another variable which is successfully picking up information as the user scrolls around, that much i have.

All i want to do is make serialScroll({)} update it's "item:_" field accordingly with that variable.

Here is the serialScroll code:

    var nextClass = ".fig4"; 

    // this is being changed to .figN depending on the users 
    // scrolling position in the browser/viewport (successfully). 
    // For proof of communication purposes I forced nextClass 
    // here to equal .fig4 to see if the two are at lest talking to 
    // each other, which they are... but now how do i update "items:" 
    // below to reflect the change in the "nextClass" variable?

$('html').serialScroll({
    target:'body',
    items: nextClass, 
    next:'#nextImage',
    axis:'y',
    duration:700,
    force:true, 
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

非要怀念 2024-11-09 06:38:02

这里发生的事情是,当 .serialScroll 初始化时,它会获取一次 nextClass 的值,这就是它使用的值。

需要发生两件事之一。要么修改插件以使用全局变量(不是非常理想,但它有效),要么每次 nextClass 更改时,您需要重新初始化 .serialScroll。

What is happening here is that when .serialScroll is initialized, it gets the value of nextClass once, and that is the value it uses.

One of two things would need to happen. Either you modify the plugin to use a global variable (not terrificly ideal, but it works), or every time nextClass changes, you need to reinitialize .serialScroll.

无尽的现实 2024-11-09 06:38:02

JavaScript 对象是动态的,您始终可以向它们添加方法。由于serialScroll是动态添加到它所调用的元素中的一堆函数,因此有两种方法可以完成您所需要的:

  1. 向插件代码中添加一个setter - 这将使其在所有安装中工作
  2. 猴子修补serialScroll的每个实例,初始化后

如果您只有一个实例,那么后者更容易并且应该证明更具前向兼容性:

$('html').serialScroll({...});
$('html').serialScroll.setItems = function(i){
  items = i;
}
// when nextClass changes
$('html').serialScroll.setItems(nextClass);

要完成第一个选项,您需要侵入serialScroll代码本身。

JavaScript objects are dynamic and you can always add methods to them. As serialScroll is bunch of functions added dynamically to the element it is called upon, there are two ways to accomplish what you need:

  1. Add a setter to the plugin code - this would make it work across all installations
  2. monkey-patch every instance of serialScroll, after initializing

If you only have one instance, then the latter is easier and should prove more forward-compatible:

$('html').serialScroll({...});
$('html').serialScroll.setItems = function(i){
  items = i;
}
// when nextClass changes
$('html').serialScroll.setItems(nextClass);

To accomplish the first option, you would need to hack into the serialScroll code itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文