【发布时间】:2017-08-27 20:00:07
【问题描述】:
我有以下代码在我的导航中添加/删除 .show-nav 和 .hide-nav 类。这适用于带有 .toggle-nav 按钮的 .mobile-nav div。
functions.php
$(function() {
// Bind a click event to anything with the class "toggle-nav"
$('.toggle-nav').click(function() {
if ($('.mobile-nav').hasClass('show-nav')) {
$('.mobile-nav').removeClass('show-nav').addClass('hide-nav');
setTimeout(function() {
$('.mobile-nav').removeClass('hide-nav');
}, 500);
} else {
$('.mobile-nav').removeClass('hide-nav').addClass('show-nav');
}
return false;
});
});
.mobile-nav 是一个全屏覆盖菜单,我只想在移动设备上使用它,所以我删除了屏幕尺寸 (>768px) 上的 .toggle-nav 按钮。在单击 .toggle-nav 之前,.mobile-nav 保持不可见。
css
.toggle-nav { display: none; }
@media screen and (max-width: 768px) {
.toggle-nav { display: inline-block; }
}
问题是如果移动导航“打开”并且用户将屏幕变大,则切换导航按钮被隐藏但菜单保持打开状态。
基本上,如果屏幕大于 768 像素,我希望应用 .hide-nav 类(或删除 .show-nav)。
【问题讨论】:
-
只需在窗口调整大小时添加类;)