我如何在window.resize之后获取当前高度的值?
$(window).resize(function(){
currHeight = $('#window-fixed').height();
});
console.log( currHeight );
//Uncaught ReferenceError: currHeight is not defined
$('a.stp').click(function(e){
e.preventDefault();
var href = $(this).data('id');
$('html, body').animate({
scrollTop: ( $(href).offset().top ) - currHeight
}, 1200);
});
谢谢
最佳答案
var $winFix = $('#window-fixed'), // Cache your elements!
currHeight = $winFix.height(); // Say hello to your variable!
$(window).resize(function(){
currHeight = $winFix.height(); // modify your variable
});
console.log( currHeight ); // nnn
$('a.stp').click(function(e){
e.preventDefault();
var href = $(this).data('id');
$('html, body').animate({
scrollTop: ( $(href).offset().top ) - currHeight // Reuse it
}, 1200);
});
您遇到ReferenceError的原因是您的变量范围未在父函数中定义,因此并非所有内部函数都可以访问。
关于javascript - 我怎样才能在window.resize之外获得实时值(value)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19972421/