Javascript 如何获取window.resize之外的活动值?

Javascript 如何获取window.resize之外的活动值?,javascript,jquery,Javascript,Jquery,如何在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();

如何在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);        

});
谢谢


您遇到了一个引用错误,原因是您的变量范围未在父函数中定义,因此无法访问所有内部函数。

currHeight
声明为全局变量,如:

var currHeight = 0;

$(window).resize(function(){
    currHeight = $('#window-fixed').height();        
}).resize();    //  Calling resize() method on page load only

console.log( currHeight );  // You will get the `window-fixed` height here now

当然你们可以得到它,但这里重要的问题是你们想在哪里使用它,对不起。我会更新这个问题。谢谢:)“Roko C.Buljan”回答了。谢谢你,Roko!:)做得好!
var currHeight = 0;

$(window).resize(function(){
    currHeight = $('#window-fixed').height();        
}).resize();    //  Calling resize() method on page load only

console.log( currHeight );  // You will get the `window-fixed` height here now