1

I am not the best at wording my questions, sorry. I am trying to make a sidenav which displays when you hover over a small strip on the side of the screen, then disappears on mouse out.

<!--the small bar on the side-->
<div id="sidebar"></div>

<!--the nav to display-->
<nav id="sidenav">
<ul>
<li>HOME</li>
<li>ABOUT</li>
</ul>
</nav>

Then I used some css to put them on the side and make them look pretty. Next, I added some JS:

<div id="sidebar" onmouseenter="show()">""</div>
<nav id="sidenav" onmouseout="hide()">""</nav>

<script>
function show() {
    document.getElementById("sidebar").style.display = "none";
    document.getElementById("sidenav").style.display = "block";
}

function hide() {
    document.getElementById("sidebar").style.display = "block";
    document.getElementById("sidenav").style.display = "none";
}
</script>

The scirpt works fine, just as I want it to. The only problem I am running into is that when I try to hover over the list items, the hide() function runs, as if it takes me entering the UL as a mouseout.

P.S. I am currently in the process of learning JS so please bare that in mind when making judgment calls.

Dr. Owning
  • 171
  • 2
  • 14

1 Answers1

3

A very simplistic solution with CSS:

#sidebar {
  background: blue;
}
#sidenav {
  background: red;
  display: none;
}
#parent:hover #sidebar {
  display: none;
}
#parent:hover #sidenav {
  display: block;
}
<div id="parent">
  <div id="sidebar">""</div>
  <nav id="sidenav">""</nav>
</div>

There a lot of things that can be improved on from this way, but it's a good start.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Thank you. I considered doing this from the start, but wasn't sure how to go about it and/or if there was a better way to do it. Seeing it actually put to action though, it's really simple and easy to work with. – Dr. Owning Mar 20 '15 at 13:06