在菜单按钮上切换徽标图像单击

时间:2018-09-24 04:27:21

标签: javascript html css

单击菜单按钮以匹配覆盖样式时,我想更改徽标的颜色。

目前,我已经能够将其从黑色徽标更改为白色徽标。但是,当您尝试关闭菜单时,即使我的js函数应该处理该开关,它也不会变回黑色徽标。

感谢您的帮助。

function menuButton(x) {
  x.classList.toggle("change");
}

function toggleNav() {
  var x = document.getElementById("myNav");
  if (x.style.height === "100%") {
    x.style.height = "0%";
  } else {
    x.style.height = "100%";
  }
}

function changeImg() {
  var logo = document.getElementById("logo");
  if (logo.src == "/images/1x/White.png") {
    logo.src = "/images/1x/Asset 1.png";
  } else {
    logo.src = "/images/1x/White.png";
  }
}
/* Global */

.white {
  color: #fff
}

html,
body {
  margin: 0;
  padding: 0;
  /* background-color: darkgray; */
}


/* Header */

header {
  width: 100%;
  min-height: 80px;
  padding: 0;
  background-color: white;
  color: black;
  position: fixed;
  margin: 0px;
  /* border-bottom: 5px solid black; */
  /* z-index: 2; */
}

.container {
  width: 80%;
  margin: auto;
  overflow: hidden;
}

header .logo {
  float: left;
  margin-top: 4px;
  position: relative;
  z-index: 2;
  transition: 0.5s;
}

header .menu {
  float: right;
  margin-top: 25px;
  cursor: pointer;
  position: relative;
  z-index: 2;
}


/* header .title h1{
        margin: 21.5px 0px 0px 0px;
        padding: 0;
        text-align: center;
        font-family: 'Raleway', sans-serif;
        font-size: 30px;
    }
    
    header .title a:link, a:visited{
        color: black;
        text-decoration: none;
    } */

.bar1,
.bar2,
.bar3 {
  background-color: black;
  width: 35px;
  height: 5px;
  margin-bottom: 6px;
  transition: 0.4s;
}

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
  background-color: white;
}

.change .bar2 {
  opacity: 0;
}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
  background-color: white;
}


/* Navigation */

.overlay {
  width: 100%;
  height: 0;
  position: fixed;
  left: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.9);
  z-index: 0;
  transition: 0.5s;
  overflow: hidden;
}

.overlayContent {
  position: relative;
  text-align: center;
  top: 25%;
  height: 100%;
  margin-top: 30px;
  font-family: 'Raleway', sans-serif;
}

.overlay a {
  padding: 8px;
  text-decoration: none;
  font-size: 36px;
  color: whitesmoke;
  display: block;
  transition: 0.3s;
}

.overlay a:hover,
a:active {
  color: darkgrey;
}
<link href="https://fonts.googleapis.com/css?family=Raleway" type="text/css" rel="stylesheet">

<!-- Header Start - Includes Title, Logo, and Menu -->
<header id="top">

  <div class="container">

    <!-- Logo -->
    <div class="logo">
      <a href="#top" class="logo"><img id="logo" src="/images/1x/Asset 1.png" alt="Initials JB"></a>
    </div>

    <!-- Menu Button -->
    <div class='menu' onclick="menuButton(this); toggleNav(); changeImg()">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
    </div>

    <!-- Title -->
    <!-- <div class = "title">
                    <h1>
                        <a href="#top">JADON BULL</a>
                    </h1>
                </div> -->

    <!-- Navigation Start -->
    <div class="container">
      <nav id="myNav" class="overlay">
        <div class="overlayContent">
          <a href="#">Overview</a>
          <a href="#">Projects</a>
          <a href="#">Services</a>
          <a href="#">About Me</a>
          <a href="#">Contact</a>
        </div>
      </nav>
    </div>
  </div>
</header>
<!-- Header End -->

<!-- Showcase -->
<div id="showcase">

</div>






JS:

Black Logo

White Logo

2 个答案:

答案 0 :(得分:2)

您的下一行有一个小错误:

logo.src = "/images/1x/Asset 1.png";

始终确保任何文件名或资产名称中都没有空格。您可以使用下划线Asset_1.png并保存图像。以后,将其用作

logo.src = "/images/1x/Asset_1.png";

如果在html或css的其他地方使用过,也要更改名称。

答案 1 :(得分:2)

您必须使用图像到图像源的绝对路径。

changeImg函数中,只需将您的if条件更改为:

if (logo.getAttribute('src') === '/images/1x/White.png')

它将起作用。