我不理解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