jQuery —通过根据用户活动而变化的变量来更新函数的初始参数?
我不明白如何在设置函数的初始参数后更新它们。
我正在使用一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里发生的事情是,当 .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.
JavaScript 对象是动态的,您始终可以向它们添加方法。由于serialScroll是动态添加到它所调用的元素中的一堆函数,因此有两种方法可以完成您所需要的:
如果您只有一个实例,那么后者更容易并且应该证明更具前向兼容性:
要完成第一个选项,您需要侵入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:
If you only have one instance, then the latter is easier and should prove more forward-compatible:
To accomplish the first option, you would need to hack into the serialScroll code itself.