[英]Removing code for mobile screen menu
I have the following code which adds/removes a .show-nav and .hide-nav class to my navigation. 我有以下代码,可在导航中添加/删除.show-nav和.hide-nav类。 This is applied to the .mobile-nav div with the .toggle-nav button.
使用.toggle-nav按钮将其应用于.mobile-nav div。
functions.php 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 is a full screen overlay menu which I only want to use on mobile devices so I remove the .toggle-nav button on screen size ( >768px). .mobile-nav是一个全屏覆盖菜单,我只想在移动设备上使用,因此我删除了屏幕尺寸(> 768px)上的.toggle-nav按钮。 .mobile-nav remains invisible until .toggle-nav is clicked.
.mobile-nav保持不可见,直到单击.toggle-nav为止。
css 的CSS
.toggle-nav { display: none; }
@media screen and (max-width: 768px) {
.toggle-nav { display: inline-block; }
}
Problem is if the mobile-nav is 'opened' and the user makes the screen large, the toggle-nav button is hidden but the menu remains open. 问题是,如果移动导航已“打开”并且用户将屏幕放大,则导航导航按钮将隐藏,但菜单仍保持打开状态。
Basically, I want the class .hide-nav applied (or .show-nav is removed) if the screen goes larger than 768px. 基本上,如果屏幕大于768像素,我希望应用.hide-nav类(或删除.show-nav)。
I have created a short example of things need to be done. 我创建了一个需要完成的简短示例。 Added some jquery codes to make it work the way you want .
添加了一些jquery代码以使其按您希望的方式工作。
Step 1 - Add class
hide-nav
on window width第1步 -在窗口宽度上添加类
hide-nav
/* logic For window width */
if ($(window).width() > 768) {
$('.mobile-nav').addClass('hide-nav');
} else {
$('.mobile-nav').removeClass('hide-nav');
}
Step 2 - Add class
hide-nav
on window resize第2步-在窗口调整大小上添加类
hide-nav
/* logic For Window Resize */
function resize() {
if ($(window).width() > 768) {
$('.mobile-nav').addClass('hide-nav');
}
$('.mobile-nav').addClass('hide-nav');
}
$(window).resize(resize)
.trigger('resize');
$(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; }); /* logic For window width */ if ($(window).width() > 768) { $('.mobile-nav').addClass('hide-nav'); $('.mobile-nav').removeClass('show-nav'); } else { $('.mobile-nav').removeClass('hide-nav'); } /* logic For Window Resize */ function resize() { if ($(window).width() > 768) { $('.mobile-nav').addClass('hide-nav'); $('.mobile-nav').removeClass('show-nav'); } $('.mobile-nav').addClass('hide-nav'); } $(window).resize(resize) .trigger('resize'); });
.mobile-nav { width: 100px; height: 100px; background: red; } .show-nav { display: block; } .hide-nav { display: none } .toggle-nav { display: none; } @media screen and (max-width: 768px) { .toggle-nav { display: inline-block; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="toggle-nav">Toggle Nav</div> <div class="mobile-nav"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.