如何在悬停时进行隐藏侧边栏显示

时间:2017-02-09 03:26:29

标签: javascript html css hover sidebar

所以,如果我点击一个按钮将其打开,我会隐藏并显示补充工具栏。

这是在html:

    <div id="mySidenav" class="sidenav">
      <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">News</a>
      <a href="#">Contact</a>
   </div>

这是关于css:

/* The side navigation menu */
.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    padding-top: 60px;
    transition: 0.5s;
}

/* The navigation menu links */
.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 20px;
}

@media screen and (max-height: 450px) {
    .sidenav {padding-top: 15px;}
    .sidenav a {font-size: 18px;}
}

这是javascript代码:

<script type="text/javascript">
        /* Set the width of the side navigation to 250px and the left margin of the page content to 250px and add a black background color to body */
        function openNav() {
            document.getElementById("mySidenav").style.width = "250px";
            document.getElementById("contoh").style.marginLeft = "250px";
            document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
        }

        /* Set the width of the side navigation to 0 and the left margin of the page content to 0, and the background color of body to white */
        function closeNav() {
            document.getElementById("mySidenav").style.width = "0";
            document.getElementById("contoh").style.marginLeft = "0";
            document.body.style.backgroundColor = "white";
        }
    </script>

我的问题是,如何更改按钮打开,悬停并显示完整的侧边栏。就像在这个网站的侧边栏中一样:https://la24.org/games-concept非常感谢。

3 个答案:

答案 0 :(得分:0)

您可以尝试jQuery的mouseover方法和mouseout方法来解决这个问题。

例如:

$("#mySidenav").mouseover(function(){ //Your code here. })

$("#mySidenav").mouseout(function(){ //Your code here. })

答案 1 :(得分:0)

@OP,是的,您必须在导航栏上放置一些宽度。就像您提供的链接中的导航栏一样。无需使用JavaScript打开它。你可以用简单的CSS来做到这一点:

.sidenav {
   height: 100%;
   width: 50px; /* Put some width to hover on. */
}

/* ON HOVER */
.sidenav:hover{
   width: 250px;
}

&#13;
&#13;
/* The side navigation menu */
.sidenav {
    height: 100%;
    width: 50px;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    padding-top: 60px;
    transition: 0.5s;
}

/* ON HOVER */
.sidenav:hover{
   width: 250px;
}

/* The navigation menu links */
.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 20px;
}
&#13;
<div id="mySidenav" class="sidenav">
      <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">News</a>
      <a href="#">Contact</a>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

You can implement using CSS also without writing javascript code. See below the code. 

<div class="parent">
        <button class="dropbtn">Dropdown</button>
        <div id="mySidenav" class="sidenav">
        <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
        <a href="#">Home</a>
        <a href="#">About</a>
        <a href="#">News</a>
        <a href="#">Contact</a>
        </div>
   </div>


.parent {
    position: relative;
    display: inline-block;
}

.parent:hover  .sidenav {
    display:block;
}


In the code mentioned by Dang Khoa, you have to include the button in side navigation or implement the onMouseHover on your button id.