2015-04-07 25 views
1

目前,我想要做一個頭,一旦窗口寬度自動響應是波紋管這樣通行證全局變量來對Click事件

$(window).resize(function() { 
    responsiveNav() 
}); 

function responsiveNav() { 

    var navWidth = $("#navigation").innerWidth(); 
    var windowWidth = $(window).width(); 

    if(windowWidth <= navWidth) { 
     // Make header responsive 
    } 

} 

導航寬度的事情是,導航寬度不上窗口中更改調整。但它在其他一些事件上確實發生了變化。我如何更新某些點擊事件的「navWidth」變量並將其全局傳遞,以便我可以在responsiveNav函數內使用它,而不是始終在窗口調整大小時獲取navWidth?

+0

請參見[56,「問題包括‘標籤’,在他們的頭銜?」(HTTP://meta.stackexchange這些問題的共識是「不,他們不應該」! –

回答

1

你可以聲明變量在全局範圍和更新它在其他功能,如

$(window).resize(function() { 
    responsiveNav() 
}); 

//declare the variable in a shared scope(like the global scope) 
var navWidth; 

function responsiveNav() { 

    var windowWidth = $(window).width(); 

    if (windowWidth <= navWidth) { 
     // Make header responsive 
    } 
} 
//this could be an event handler or some other function 
function someotherfunction() { 
    navWidth = $("#navigation").innerWidth(); 
}