媒体查询不工作最大宽度:900px

时间:2017-05-09 16:35:42

标签: javascript html css

我有一个媒体查询问题,因为它突然根本不起作用。应该发生的是,在低于900px的情况下,导航栏应该会消失并成为一个汉堡包图标,您可以单击该图标以显示菜单。

我会尽量只包含相关代码,所以如果我错过了什么,请告诉我你是否需要它。因此,我删除了所使用的SVG的实际路径。

代码:

function myFunction() {
    var x = document.getElementById("nav_ul");
    if (x.className === "nav_ul") {
        x.className += " responsive";
    } else {
        x.className = "nav_ul";
    }
}
/* Navigation bar for all screen sizes and all pages */

#nav_ul {
  list-style: none;
  margin: 20px;
  padding: 0px;
  position: relative;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  font-family: "Times New Roman", Georgia, Serif;
  font-weight: lighter;
}

#nav_ul li {
  display: inline;
  margin: auto;
}

#nav_ul a {
  text-decoration: none;
  font-size: 50px;
  margin: 0px;
  padding: 0px;
  padding-bottom: 10px;
  color: black;
}

#nav_ul li a:hover {
    color: #BF3038;
}

#nav_logo {
  width: 20vw;
  height: auto;
}

#nav_logo:hover path {
  fill:  #b1c9e5;
}

.nav_seperator {
  font-size: 50px;
  padding: 0;
  margin: 0;
  color: #BF3038;
}

.nav_seperator:hover {
  color: #b1c9e5;
}

#hamburger {
  width: 35px;
  height: 35px;
  color: black;
  display: none;
}

#responsive_logo {
  display: none;
}

/* End of navigation bar for all screen sizes and all pages */

/* Navigation bar hover effect when the bar is not responsive for all pages */

@media (min-width: 900px) {

  #nav_ul li a.not_active,
  #nav_ul li a.not_active:after,
  #nav_ul li a.not_active:before {
    transition: color 300ms;
  }

  #nav_ul li a.not_active:after,
  #nav_ul li a.not_active:before {
    content: '';
    display: block;
    height: 3px;
    visibility: hidden;
    transform: scaleX(0);
    background-color: #BF3038;
    transition: all 300ms ease-in-out;
  }

  #nav_ul li a.not_active:hover::after,
  #nav_ul li a.not_active:hover::before {
    visibility: visible;
    transform: scaleX(1);
  }

  #nav_ul li a.active:after {
    content: '';
    display: block;
    height: 3px;
    background-color: #BF3038;
  }

  #nav_ul li a.active:before {
    content: '';
    display: block;
    height: 3px;
    visibility: hidden;
    background-color: #BF3038;
    transition: all 300ms ease-in-out;
    transform: scaleX(0);
  }

  #nav_ul li a.active:hover::before {
    visibility: visible;
    transform: scaleX(1);
  }

}

/* End of navigation bar hover effect when the bar is not responsive for all pages */\

/* Begining of making the navigation bar responsive */

@media screen and (max-width: 900px) {

  #hamburger {
    display: block;
    position: absolute;
    top: 20px;
    right: 20px;
  }

  #top_nav {
    margin-top: 65px;
  }

  #nav_ul {
    display: block;
  }

  #nav_ul li {
    display: none;
  }

  #top_nav #nav_ul #nav_logo,
  #top_nav #nav_ul .nav_seperator {
    display: none;
  }

  #nav_ul.responsive {
    position: relative;
    margin: -40px auto 0 auto;
    width: 50%;
  }

  #nav_ul.responsive li {
    display: block;

  }

  #nav_ul.responsive li a {
    display: block;
    text-align: center;
    font-size: 20px;
  }

  #nav_ul.responsive #hamburger {
    position: relative;
    top: 0px;
    right: 0px;
  }

  #responsive_logo {
    display: inline-block;
    position: absolute;
    top: 20px;
    left: 20px;
  }

  #responsive_logo svg {
    height: 35px;
    width: 35px;
  }

}

/* End of making the navigation bar responsive */
<div id="responsive_logo">
  <?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
  <svg width="100%" height="100%" viewBox="0 0 161 164" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;"><g id="g4202">
  </svg>
</div>
<div class="icon">
  <a href="javascript:void(0);" style="font-size:15px;" onclick="myFunction()">
    <div id="hamburger">
      <?xml version="1.0" ?>
      <!DOCTYPE svg  PUBLIC '-//W3C//DTD SVG 1.1//EN'  'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
      <svg id="Layer_1" style="enable-background:new 0 0 32 32;" version="1.1" viewBox="0 0 32 32" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      </svg>
    </div>
  </a>
</div>
<nav id="top_nav">
  <ul class="nav_ul" id="nav_ul">
    <li><a class="active" href="#">About</a></li>
    <li class="nav_seperator">|</li>
    <li><a class="not_active" href="facilities.html">Facilities</a></li>
    <li id="nav_logo">
      <div id="nav_logo">
        <?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
        <svg viewBox="0 0 543 158" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">
        </svg>
      </div>
    </li>
    <li><a class="not_active" href="#">Forms</a></li>
    <li class="nav_seperator">|</li>
    <li><a class="not_active" href="#">Contact</a></li>
  </ul>
</nav>

2 个答案:

答案 0 :(得分:1)

你的CSS评论中有拼写错误。 CSS文档中的语法错误通常会破坏您定义的下一个CSS属性,错误就在您的@media查询之前。它最后是迷路\

/* End of navigation bar hover effect when the bar is not responsive for all pages */\

答案 1 :(得分:0)

尝试将@media screen and (min-width:设为901px 以及@media screen and (max-width:900px,以便他们不会使用相同的像素作为断点。

相关问题