更改按钮颜色和背景颜色onclick

时间:2018-06-16 10:27:47

标签: javascript html css

我想创建nav-menu折叠,但是当我点击菜单按钮时,它会更改为另一个colorbackground-color。使用hoverfocus一旦无法保留旧颜色,就会更改颜色。所以我使用display: blockdisplay: none一对一地修复了两个按钮。我需要在button点击按钮color并再次更改background-color后,我点击颜色会更改旧颜色。

function openNav() {

    document.getElementById("firstbtn").style.display = 'none';
    document.getElementById("secondbtn").style.display = 'block';
    document.getElementById("navbar").style.height = '200px';

}
function closeNav() {

    document.getElementById("firstbtn").style.display = 'block';
    document.getElementById("secondbtn").style.display = 'none';
    document.getElementById("navbar").style.height = '0px';
}
.nav-bg{
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    background-color: #0dc5c1;
    padding-top: 12px;
    position: fixed;
    overflow: hidden;
}
button.nav-btn {
    float: right;
    font-size: 24px;
    border: none;
    margin-right: 16px;
    padding: 4px 8px;
    background-color: #fff;
    color: #0dc5c1;
border-radius: 4px;
}
button.nav-btn1 {
    float: right;
    font-size: 24px;
    border: none;
    margin-right: 16px;
    padding: 4px 8px;
    background-color: #0dc5c1;
    color: #ffffff;
    border-radius: 4px;
}
.navbar-col{
    color: #fff;
    margin-top: 50px;
    transition: 0.7s;
}
.nav-ul{
    list-style: none;
}
li {
    position: relative;
    display: block;
}
a {
    padding-top: 10px;
    padding-bottom: 10px;
    line-height: 25px;
    font-size: 24px;
    font-family: "Segoe UI";
    position: relative;
    display: block;
    padding: 10px 0px;
}
.d-btn,.l-btn,.t-btn,.s-btn{
    background: none;
    border: none;
    color: #ffffff;
}
<nav  class="nav-bg container">
    <button class="nav-btn"  onclick="openNav()" id="firstbtn"><span class="fa fa-bars"></span></button>
    <button class="nav-btn1"  onclick="closeNav()" id="secondbtn" style="display: none"><span class="fa fa-bars"></span></button>                   
    <div class="navbar-col  collapse" id="navbar" style="height: 0px">
       <ul class="nav-ul">
         <li><button class="d-btn"><a>Desktop</a></button></li>
         <li><button class="l-btn"><a>Laptop</a></button></li>
         <li><button class="t-btn"><a>Tablet</a></button></li>
         <li><button class="s-btn"><a>Smartphone</a></button></li>
       </ul>
    </div>
</nav>

  

NAV-崩

enter image description here

  

NAV-扩大

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过单个按钮使用来实现。您需要根据您的要求在两个类中创建两个类background-colorcolor。现在点击按钮,您可以按类名检查条件。例如

//If you button containe {open} class then you need to assign {close} class else vice versa
if (btn.classList.contains('nav-btn-open')) {
    //Put your logic here.
} else {
    //Put your logic here.
}

<强>样本

function onShowHide(btn) {
  if (btn.classList.contains('nav-btn-open')) {
    btn.classList.remove('nav-btn-open');
    btn.classList.add('nav-btn-close');
    btn.closest('.nav-bg').querySelector('div.navbar-col').style.height = '200px';
  } else {
    btn.classList.remove('nav-btn-close');
    btn.classList.add('nav-btn-open');
    btn.closest('.nav-bg').querySelector('div.navbar-col').style.height = '0px';
  }

}
.nav-bg {
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  background-color: #0dc5c1;
  padding-top: 12px;
  position: fixed;
  overflow: hidden;
}

.right-btn {
  float: right;
  font-size: 24px;
  border: none;
  margin: 0 10px 10px 0;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
}

.nav-btn-open {
  background-color: #fff;
  color: #0dc5c1;
}

.nav-btn-close {
  background-color: #0dc5c1;
  color: #ffffff;
}

.navbar-col {
  color: #fff;
  margin-top: 50px;
  transition: 0.7s;
  height: 0px;
}

.nav-ul {
  list-style: none;
}

li {
  position: relative;
  display: block;
}

a {
  padding-top: 10px;
  padding-bottom: 10px;
  line-height: 25px;
  font-size: 24px;
  font-family: "Segoe UI";
  position: relative;
  display: block;
  padding: 10px 0px;
}

.d-btn,
.l-btn,
.t-btn,
.s-btn {
  background: none;
  border: none;
  color: #ffffff;
}
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<nav class="nav-bg container">

  <button class="right-btn nav-btn-open" onclick="onShowHide(this)"><span class="fa fa-bars"></span></button>

  <div class="navbar-col">
    <ul class="nav-ul">
      <li><button class="d-btn"><a>Desktop</a></button></li>
      <li><button class="l-btn"><a>Laptop</a></button></li>
      <li><button class="t-btn"><a>Tablet</a></button></li>
      <li><button class="s-btn"><a>Smartphone</a></button></li>
    </ul>
  </div>
</nav>