移动导航 JavaScript 无法正常工作

3

我是一个初学者,正在FreeCodeCamp上做练习。在制作Portfolio网站项目时,我尝试从ws3schools的代码中添加JavaScript响应式移动导航到网站。

导航栏显示正常,但点击它没有任何反应。这是我的CodePen,有什么想法是什么阻止它在单击时弹出导航链接?https://codepen.io/colum1225/pen/xLwMJG

这是导航栏代码:

<!-- HTML -->

<div class="topnav" id="myTopnav">
  <h3>Brandon Ray</h3>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#portfolio">Portfolio</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<!-- CSS -->

.topnav {
  background-color: #2C8597;
  overflow: hidden;
  width: 100%;
  position: fixed;
  z-index: 9;
}

.topnav h3 {
  float: left;
  color: #f2f2f2;
  font-family: sans-serif;
  font-size: 1.5em;
  padding-left: 30px;
  letter-spacing: .04em;
  font-weight: 500;
}

.topnav a {
  float: right;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 27px 30px 27px 30px;
  text-decoration: none;
  font-size: 1.25em;
  font-family: sans-serif;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
  transition: .5s;
}

.topnav a:active {
  background-color: #ddd;
  color: black;
}

.topnav .icon {
  display: none;
  font-size: 1.3em;
}


@media screen and (max-width: 640px) {
  .topnav a {display: none;}
  .topnav .icon {
    float: right;
    display: block;
    color: white;
    margin-right: 30px;
  }
}

@media screen and (max-width: 640px) {
  topnav.responsive {position: relative;}
  topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }

  topnav.responsive a {
    float: none;
    display: block;
    text-align: right;
  }
}

<!-- JS -->

function myFunction() {
  var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }

}

2个回答

2

到目前为止,工作做得很出色。

在你的 .responsive 规则中,topnav 类选择器前面缺少一个点。请参见下面已修复的 CSS:

@media screen and (max-width: 640px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }

  .topnav.responsive a {
    float: none;
    display: block;
    text-align: right;
  }
}

修复重叠问题

为了解决移动导航下拉菜单的第一个菜单项与菜单图标重叠的问题,尝试给第一个菜单项添加一个margin-top。合适的间距大小应该是菜单高度(82px)。

这个规则的写法如下:

  .topnav.responsive a:first-of-type {
    margin-top: 82px;
  }

将此规则与其他.topnav.responsive规则一起包含(第77行左右)。

我知道那是类似于那样的东西。非常感谢! - Brandon
很高兴能够帮助。如果您觉得我的回答直接解决了您原始帖子中提出的问题,请考虑将其标记为“已接受的答案”,以便其他遇到相同问题的人可以快速参考答案。 - Trent

1
在你的CSS中,在topnav之前需要加一个句点:
@media screen and (max-width: 640px) {
    .topnav.responsive {position: relative;}
    .topnav.responsive a.icon {
        position: absolute;
        right: 0;
        top: 0;
    }

    .topnav.responsive a {
        float: none;
        display: block;
        text-align: right;
    }
}

或者你可以完全删除顶部导航栏并只保留响应式部分。
位置修复:
将图标位置改回相对位置,并将图标放置在导航栏的顶部:
HTML
<div class="topnav" id="myTopnav">
      <h3>Brandon Ray</h3>
      <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
      <a href="#portfolio">Portfolio</a>  
    </div>

CSS

.topnav.responsive a.icon {
    position: relative;
    right: 0;
    top: 0;
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接