2015-04-07 98 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(); 
}