【问题标题】:Removing code for mobile screen menu删除移动屏幕菜单的代码
【发布时间】: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)。

【问题讨论】:

  • 只需在窗口调整大小时添加类;)

标签: html css


【解决方案1】:

我已经创建了一个简短的示例来说明需要完成的事情。添加了一些 jquery 代码以使其按您想要的方式工作。

第 1 步 - 在窗口宽度

上添加类hide-nav
 /* logic For window width */
  if ($(window).width() > 768) {
    $('.mobile-nav').addClass('hide-nav');
  } else {
    $('.mobile-nav').removeClass('hide-nav');
  }

第 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>

【讨论】:

  • 确实如此!但是有一个请求,我有一个动作在它关闭时有一个删除类动作(以及在它打开时有一个动作)当窗口调整到更大的屏幕时是否可以让它像关闭一样“行为”?现在菜单消失了
  • 那么当浏览器在 768px 以上的情况下调整大小时,菜单应该会出现什么情况?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 2017-03-21
  • 2018-06-28
  • 2019-05-20
  • 1970-01-01
  • 2015-11-21
相关资源
最近更新 更多