如何获取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 之外获取实时值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19972421/