将鼠标悬停在一个div上以显示另一个div

时间:2016-09-21 06:50:50

标签: jquery hover settimeout mouseleave mouseout

现在开裂一整天仍然无法找到解决这个问题的简单方法。

我有一个班级

  .displaynone{
      display:none;
    }

我正在尝试

  1. 将鼠标悬停在#hoversubheader1上并通过删除#subheader1上的.displaynone来显示#subheader1。
  2. 当我将鼠标从#hoversubheader1移动到#subheader1
  3. 时,不要让#subheader1消失
  4. 当我的鼠标既不是#subheader1也不是#hoversubheader1时,将该类添加回#subheader1。
  5. 尚未完成第2步和第3步。

    我知道你会要我嵌套元素,但我有理由不这样做。实际上两个div被一些“空间”分开,所以我自然可能需要一个setTimeout或其他与时序相关的函数,但我也不确定我是否在正确的轨道上。

    如果有人可以为此提供帮助,那将是我的一天。

    标记

    <div class="ui basic vertical segment" id="header">
            <div class="ui container">
                <div class="ui text menu">
                    <a class="item">
                        Item
                    </a>
                </div>
                <div class="ui text menu displaynone" id="subheader">
                        <a class="right item" id="hoversubheader1">
                            subheadertitle1
                        </a>
                        <a class="item" id="hoversubheader2">
                            subheadertitle2
                        </a>
                </div><!--end of subheadermenu-->
                <div class="ui text menu displaynone" id="subheader1">
                        <a class="right item">
                            detail
                        </a>
                        <a class="item">
                            detail
                        </a>
                </div><!--end of subheadermenu-->
                <div class="ui text menu displaynone" id="subheader2">
                        <a class="right item">
                            detail
                        </a>
                        <a class="item">
                            detail
                        </a>
                </div><!--end of subheadermenu-->
            </div><!--end of container-->
        </div><!--end of segment-->
    

    JS

        (function($) {
        "use strict"; // Start of use strict
        //header hover brings out subheader bar
        $("#header").hover(
          function () {
            $("#subheader").removeClass("displaynone");
          },
          function () {
            $("#subheader").addClass("displaynone");
          }
        );
        //hovering on each subheadertitle should display each subheader1, subheader2 etc
        $('#hoversubheader1,#subheader1').mouseenter(function(){
    
            $('#subheader1').removeClass("displaynone");
        }).mouseleave(function(){
    
            $("#subheader1").addClass("displaynone");
    
        });
    
        $('#hoversubheader2,#subheader2').mouseenter(function(){
    
            $('#subheader2').removeClass("displaynone");
        }).mouseleave(function(){
    
            $("#subheader2").addClass("displaynone");
    
        });
        }(jQuery)); // End of use strict
    

    CSS

    #header{
        background-color: white;
        opacity: 0.97;
        position: fixed;
        width: 100%;
        z-index: 99;
        padding:0;
        padding-bottom:0;
        -webkit-transition: all 250ms ease-in-out;
        -moz-transition: all 250ms ease-in-out;
        -o-transition: all 250ms ease-in-out;
        transition: all 250ms ease-in-out;
    }
    
    #header > .ui.container > .ui.text.menu{
        margin-bottom:0;
    }
    
    
    #subheader,
    #subheader1,
    #subheader2{
      padding:0;
      margin:0;
    }
    
    #subheader1,
    #subheader2{
      height:200px;
    }
    
    #subheader > .ui.text.menu,
    #subheader1 > .ui.text.menu,
    #subheader2 > .ui.text.menu{
      margin:0;
    }
    #subheader.displaynone,
    #subheader1.displaynone,
    #subheader2.displaynone{
      display:none;
    }
    

1 个答案:

答案 0 :(得分:1)

<!DOCTYPE html>
<html>

<head>
  <style>
    .general {
      height: 100px;
      width: 100px;
      border: solid 1px black;
    }
    .divClass {
      display: inline;
    }
    .divClassB {
      display: none;
    }
  </style>
  <script src="script.js"></script>
  <script>
    var flag = false;

    function MouseOver(obj) {

      if (obj.id == "DivA" || obj.id == "DivB") {
        flag = true;
        document.getElementById('DivB').style.display = 'inline';
      }
      showhide(flag);
    }

    function MouseOut(obj) {

      if (obj.id == "DivA" || obj.id == "DivB")
        flag = false;
      setTimeout(function() {
        showhide(flag);
      }, 3000);

    }

    function showhide(flag) {
      if (flag) {

        document.getElementById('DivB').style.display = 'inline';
      } else
        document.getElementById('DivB').style.display = 'none'

    }
  </script>
</head>

<body>
  <h1>Hello Plunker!</h1>
  <div class="general divClass" id="DivA" onmouseout="MouseOut(this)" onmouseover="MouseOver(this)">
    Div A
  </div>
  <div>
    This is for space
  </div>
  <div id="DivB" class="general divClassB" onmouseout="MouseOut(this)" onmouseover="MouseOver(this)">
    Div B
  </div>
</body>

</html>

相关问题