我目前正试图在窗口宽度低于这样的导航宽度时自动响应标题
$(window).resize(function() {
responsiveNav()
});
function responsiveNav() {
var navWidth = $("#navigation").innerWidth();
var windowWidth = $(window).width();
if( windowWidth <= navWidth ) {
// Make header responsive
}
}
问题是导航宽度在窗口调整大小时不会改变。但它确实会在其他一些事件上发生变化。如何更新&#34; navWidth&#34;某些点击事件的变量并将其全局传递,以便我可以在responsiveNav函数中使用它,而不是总是在窗口调整大小时使用navWidth?
答案 0 :(得分: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();
}