2010-11-18 83 views
0

如何让变量“theOffset”在“localScroll”函数内工作。这是我的非工作代码:帮我使用javascript变量的范围

jQuery(function($){ 
    //initialize variable "theOffset" 
    theOffset = 10; // <-- sets variable for inside of "localScroll" function 

     $(window).resize(function() { 
      theOffset = 20; // <-- does NOT reset inside "localScroll" function 
     }); // end of window resize 


    $.localScroll({ 
      target: '#content', 
      duration:1500, 
      hash:true, 
      offset: {top:0, left: theOffset}, // <-- I want to reset this on window resize 
      onBefore:function(e, anchor, $target){ 
      }, 
      onAfter:function(anchor, settings){ 
      } 
    }); 
}); 
+4

应该把'之前的任何变量,你不希望在全球范围内var'。 – mpen 2010-11-18 22:38:22

+0

你可以编辑你现有的问题http://stackoverflow.com/questions/4219478/javascript-variable-not-working-why与新的细节,不需要在同一问题上开始新的问题。 – 2010-11-18 22:40:46

+0

aaahh。我仍然在学习stackoverflow的方法。 – Dave 2010-11-18 22:45:39

回答

1

几个指针:

您创建对象与所有这些{}。虽然它可能看起来像简单的功能,但它们不是。

偏移量为对象的属性被传递到$ .localScroll

一个想法是使(其被传递到$ .localScroll)对象$ .localScroll以外,将其设置为一变量,并引用该变量。

不确定这是否会按原样工作,可能不得不跳过箍或两个仍然。

东西线沿线的:

var theOffset = { 
... 
offset: {top:0, left: whatever}, 
... 
}; 

$(window).resize(function() { 
theOffset.offset = 20; 
}); 

$.localScroll(theOffset);