我有一个响应式的 wordpress 主题。当屏幕尺寸低于 740 时,菜单被编码为隐藏。但是它只在主页上正确执行此操作。如果我转到任何其他页面,菜单会折叠但它无法隐藏并且我收到此错误:“未捕获的 TypeError:无法读取属性 'clientWidth' of null”
这是代码,我在主题的 header.php 文件中调用了它:
var ww = document.body.clientWidth;
$(document).ready(function() {
adjustMenu();
$(".cat").click(function(e) { // cat class
e.preventDefault();
$(this).toggleClass("active");
$(".sf-menu").toggle();
});
});
function adjustMenu() {
if (ww <= 740) { //change this to your breakpoint
$('.sf-menu').hide();
$(".cat").show();
if (!$(".cat").hasClass("active")) {
$(".sf-menu").hide();
} else {
$(".sf-menu").show();
}
} else {
$('.sf-menu').show();
$(".cat").hide();
}
}
$(window).bind('resize orientationchange', function() {
ww = document.body.clientWidth;
adjustMenu();
});