仅在菜单上单击隐藏

时间:2017-04-25 06:20:52

标签: javascript jquery html css

我正在处理菜单项的导航。将鼠标悬停在我的菜单项上时,它将延伸并在鼠标移出时退出。我让它处于伸展位置,直到我们点击菜单项的外面一侧。但是我只需要点击其他菜单项。

例如,如果我点击任何菜单项它将是固定位置,但是当我点击任何其他项目时它应该移入。

目前,只要我点击窗口,它就会隐藏起来。但是只要我们点击其他菜单项时它就应该隐藏了。

Click here for image ref



#mySidenav a {
  position: absolute;
  left: -80px;
  transition: 0.3s;
  padding: 15px;
  width: 100px;
  text-decoration: none;
  font-size: 20px;
  color: white;
  border-radius: 0 5px 5px 0;
}

#mySidenav a:focus {
  position: fixed;
  left: 0;
}

#mySidenav a:hover {
  left: 0;
}

#about {
  top: 20px;
  background-color: #4CAF50;
}

#blog {
  top: 80px;
  background-color: #2196F3;
}

#projects {
  top: 140px;
  background-color: #f44336;
}

#contact {
  top: 200px;
  background-color: #555
}

<div id="mySidenav" class="sidenav">
  <a href="#" id="about">About</a>
  <a href="#" id="blog">Blog</a>
  <a href="#" id="projects">Projects</a>
  <a href="#" id="contact">Contact</a>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

使用额外的课程,例如:

&#13;
&#13;
$('#mySidenav a').on('click', function() {
  $('a.fixed').removeClass('fixed');
  $(this).addClass('fixed');
});
&#13;
#mySidenav a {
  position: absolute;
  left: -80px;
  transition: 0.3s;
  padding: 15px;
  width: 100px;
  text-decoration: none;
  font-size: 20px;
  color: white;
  border-radius: 0 5px 5px 0;
}

#mySidenav a:focus,
#mySidenav a.fixed{
  position: fixed;
  left: 0;
}

#mySidenav a:hover {
  left: 0;
}

#about {
  top: 20px;
  background-color: #4CAF50;
}

#blog {
  top: 80px;
  background-color: #2196F3;
}

#projects {
  top: 140px;
  background-color: #f44336;
}

#contact {
  top: 200px;
  background-color: #555
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mySidenav" class="sidenav">
  <a href="#" id="about">About</a>
  <a href="#" id="blog">Blog</a>
  <a href="#" id="projects">Projects</a>
  <a href="#" id="contact">Contact</a>
</div>

<div style="margin-left:80px;">
  <h2>Hoverable Sidenav Buttons</h2>
  <p>Hover over the buttons in the left side navigation to open them.</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你需要一些Javascript!

$(document).ready(function() {
  $('#mySidenav a').on('click', function() {
    $('#mySidenav a').removeClass('fix');
    $(this).addClass('fix');
  });
});
#mySidenav a {
  position: absolute;
  left: -80px;
  transition: 0.3s;
  padding: 15px;
  width: 100px;
  text-decoration: none;
  font-size: 20px;
  color: white;
  border-radius: 0 5px 5px 0;
}

#mySidenav a.fix {
  position: fixed;
  left: 0;
}

#mySidenav a:hover {
  left: 0;
}

#about {
  top: 20px;
  background-color: #4CAF50;
}

#blog {
  top: 80px;
  background-color: #2196F3;
}

#projects {
  top: 140px;
  background-color: #f44336;
}

#contact {
  top: 200px;
  background-color: #555
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mySidenav" class="sidenav">
  <a href="#" id="about">About</a>
  <a href="#" id="blog">Blog</a>
  <a href="#" id="projects">Projects</a>
  <a href="#" id="contact">Contact</a>
</div>