简体   繁体   English

JavaScript中屏幕分辨率小于767px时的addClass

[英]addClass when the screen resolution is less than 767px in JavaScript

I created a bootstrap menu, I need to add some classes and attributes when the screen resolution is less than 767 px. 我创建了一个引导菜单,当屏幕分辨率小于767像素时,我需要添加一些类和属性。 My code: 我的代码:

<li class="item-104 deeper parent">
  <a href="#">О министерстве</a>
    <ul class="nav-child unstyled small">
        <li class="item-113"><a href="#">Новости</a>
        </li>
        <li class="item-134"><a href="#">Подразделения Министерства</a>
        </li>
        <li class="item-135 deeper parent">
        <a href="#">Подведомственные учреждение министерства</a>
            <ul class="nav-child unstyled small">
                <li class="item-159"><a href="#">ФОК «ЖАР» МВД Республики Узбекистан</a>
                </li>
            </ul>
        </li>
        <li class="item-136"><a href="#">Прием на службу в органы внутренних дел</a>
        </li>
        <li class="item-177"><a href="#">Тендеры</a>
        </li>
    </ul>
</li>

<script>
    $(document).ready(function() {
      $(window).resize(function(){
        var windowWidth = $(window).width();
        if(windowWidth < 767)$(".navbar-nav ul").addClass("dropdown-menu") && $(".navbar-nav li",".navbar-nav ul").removeClass("parent","nav-child");
      });
    });
</script>

and I want to add the attribute data-toggle="dropdown" to the link which has a subdirectory with ul, ie <li><a href="#" data-toggle="dropdown"><ul><li> etc. 并且我想将属性data-toggle =“ dropdown”添加到具有ul子目录的链接,即<li><a href="#" data-toggle="dropdown"><ul><li>等。

The problem seems to be with && between 问题似乎出在&&之间

$(".navbar-nav ul").addClass("dropdown-menu") and $(".navbar-nav li",".navbar-nav ul").removeClass("parent","nav-child"); $(".navbar-nav ul").addClass("dropdown-menu")$(".navbar-nav li",".navbar-nav ul").removeClass("parent","nav-child");

Instead it should be like this 而是应该像这样

if (windowWidth < 767) {
      $(".navbar-nav ul").addClass("dropdown-menu");
      $(".navbar-nav li", ".navbar-nav ul").removeClass("parent", "nav-child");
    }

Also this same thing can be achieved using CSS media queries 同样,使用CSS媒体查询也可以实现同样的目的

Mediaqueries (as requested as an alternative) 媒体查询 (根据要求提供)

@media screen and (max-width: 800px) {
  body {
    background-color: #FF0;
  }
}

This example changes the background-color of the body to yellow whenever the screen screen is less than 800 pixels. 每当屏幕屏幕小于 800像素时,此示例便将主体的背景色更改为黄色。

How do you apply this in your case? 您如何在您的情况下应用此功能?

Apply all classes you wish to use beforehand, and wrap the markup of the classes that need to invoke in media queries. 事先应用您希望使用的所有类,并包装需要在媒体查询中调用的类的标记。

@media screen and (max-width: 800px) {
  .dropdown-menu {
    /* CSS that invokes the dropdown */
  }
}
@media screen and (min-width: 801px) {
  .nav-child {
    /* CSS that is being used in medium / large screens */
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM