我不理解JavaScript中的变量。我正在尝试在localScroll函数出现之前更改/计算“偏移量”(使用变量“theOffset”),或者更优选地调整窗口大小。下面的实例都没有工作,接受“//初始化偏移量”。JavaScript变量不起作用。为什么?
如何获取“$ .localScroll”中的变量“theOffset”进行更改?
jQuery(function($){
//initialize offset
var windowWidth = $(window).width();
if (windowWidth < 900) {
theOffset = 0;
} else {
theOffset = ($(window).width() - 900)/-2;
}
$(window).resize(function() {
//calculate offset
var windowWidth = $(window).width();
if (windowWidth < 900) {
theOffset = 0;
} else {
theOffset = ($(window).width() - 900)/-2;
}
});
$.localScroll({
target: '#content',
queue:true,
duration:1500,
hash:true,
stop:true,
onBefore:function(e, anchor, $target){
//calculate offset
var windowWidth = $(window).width();
if (windowWidth < 900) {
theOffset = 0;
} else {
theOffset = ($(window).width() - 900)/-2;
}
},
offset: {top:0, left: theOffset,
onAfter:function(anchor, settings){
if (windowWidth < 900) {
theOffset = 0;
} else {
theOffset = ($(window).width() - 900)/-2;
}
}
});
});
如果需要知道,我中心一个div容器在一个奇特的侧滚动网站偏移的窗口;)
你在http://www.jslint.com上得到3个错误 – 2010-11-18 21:27:55
即使没有错误,同样的问题仍然存在......'{top:0,left:theOffset}'在执行时被评估,所以没有任何变量作用域的闭包将会修复这个行为并且使其不知道/ [ab ]使用scroll-to API /内部或重新调用localScroll函数。 – 2010-11-18 22:53:29