开发者

jQuery — Update a function's initial parameters via a variable which changes according to user activity?

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').seria开发者_JAVA百科lScroll({
    target:'body',
    items: nextClass, 
    next:'#nextImage',
    axis:'y',
    duration:700,
    force:true, 
});


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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜