【发布时间】:2015-03-27 02:35:08
【问题描述】:
我会尽量保持这个超级简单,只使用我目前拥有的代码大纲。本质上,我有一个命名空间模块,它是一个自调用函数,它在加载时运行并用变量的值填充变量并返回它们以使其他函数可以访问它们:
moduleA = function() {
var a = $(window).height(),
b = $('#imgs').height(),
// other variables that need updating
return {
windowH: a,
imgH: b
}
}();
然后在另一个在滚动时调用的函数中访问这些变量:
function scrollSpeed() {
var start = $('#article-wrap').offset().top,
end = (start + moduleA.imgH) - moduleA.windowH;
//use variables in animations
}
$(window).scroll(function() {
scrollSpeed();
})
上面的问题是当我调整窗口大小时,我需要更新 moduleA 中的变量。最初我在模块中编写了一个函数“update()”并返回它,然后在我的去抖动调整大小函数中调用它,但这不起作用?
moduleA = function() {
var a = $(window).height(),
b = $('#imgs').height(),
// other variables that need updating
function update() {
a = $(window).height(),
b = $('#imgs').height(),
...// other variables that are updated
}
return {
windowH: a,
imgH: b,
update: update
}
}();
var resizeFn = debounce(function() {
moduleA.update();
}, 100);
很明显,我没有在这里包含所有内容,我可以根据要求提供。任何想法我哪里出错了?
【问题讨论】:
标签: javascript jquery namespaces resize