将鼠标悬停在菜单元素上以显示子菜单

时间:2016-01-21 03:36:58

标签: javascript jquery html css

我正在尝试创建一个菜单,其具有子菜单(不是下拉列表),当鼠标悬停时,它从左向右滑入;当鼠标移动时从右到左。

JSFiddle:https://jsfiddle.net/z40xo01d/4/

两个垂直菜单并排:

HTML code:

<aside class="nav-container">

  <!--Main Navigation Structure-->
    <ul id="nav-main">
      <a href="#"><li class="fa fa-home fa-2x" id="home"></li><br/></a>
      <a href="#"><li class="fa fa-folder fa-2x" id="projects"></li><br/></a>
      <a href="#"><li class="fa fa-user fa-2x" id="about"></li><br/></a>
      <a href="#"><li class="fa fa-envelope fa-2x" id="contact"></li><br/></a>

      <!--Additional Navigation Buttons-->
      <div id="nav-additionals">
          <a href="#"><p class="fa fa-cogs fa-s" id="settings"></p></a>
          <a href="#"><p class="fa fa-question fa-s" id="about"></p></a>
      </div>
    </ul>

    <!--Projects submenu-->
    <ul id="proj_menu">
      <a href="#"><li class="icon-python"></li></a>
      <a href="#"><li class="icon-java-bold"></li></a>
      <a href="#"><li class="icon-csharp"></li></a>
      <a href="#"><li class="icon-cplusplus"></li></a>
      <a href="#"><li class="icon-javascript"></li></a>
      <a href="#"><li class="icon-html"></li></a>
      <a href="#"><li class="icon-ruby"></li></a>
      <a href="#"><li class="icon-php"></li></a>
    </ul>
</aside>

JQuery代码:

<script>
  $("document").ready(function(){

    //hides menu when loaded
    $("#proj_menu").hide();

    //shows and hides projects sub menu
    $("#projects").mouseover(function(){

      //if it is hovered SHOW
      if($("#projects").is(":hover")){
          $("#proj_menu").show();
      }
      //if it is not hovered HIDE
      else if($("#projects").not(":hover")){
          $("proj_menu").hide();
      }
    });
  });
</script>

2 个答案:

答案 0 :(得分:0)

也许是这样的?

<强>更新

jsFiddle Demo

$("#projects").hover(function(){
    $("#proj_menu").animate({
        left: '120px'
    });
});

$("#proj_menu").hover(function(){},function(){
    $("#proj_menu").animate({
        left: '-170px'
    });
});

答案 1 :(得分:0)

将以下css类添加到proj_menu

.menu2col li {
    width: 155px !important;
    float: left !important;
    margin-right: 10px !important;
    /*display:inline-block !important;*/
}
希望它可能有效..