显示/隐藏悬停一个div和悬停出(show/hide a div on hover and hove

2019-06-25 14:44发布

我想显示和隐藏悬停在一个div和悬停出来。

这里就是我最近做了。

CSS

 $("#menu").hover(function() { $('.flyout').removeClass('hidden'); }, function() { $('.flyout').addClass('hidden'); }); 
 .flyout { position: absolute; width: 1000px; height: 450px; background: red; overflow: hidden; z-index: 10000; } .hidden { visibility: hidden; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script> <div id="menu" class="margint10 round-border"> <a href="#"><img src="images/menu.jpg" alt="" id="menu_link" /></a> </div> <div class="flyout hidden">&nbsp;</div> 

我的问题是,当我将鼠标悬停在菜单的ID,在弹出的div闪烁。 这是为什么?

Answer 1:

可能是有没有必要JS。 你可以用CSS也做到这一点。 像这样写:

.flyout {
    position: absolute;
    width: 1000px;
    height: 450px;
    background: red;
    overflow: hidden;
    z-index: 10000;
    display: none;
}
#menu:hover + .flyout {
    display: block;
}


Answer 2:

为什么不直接使用.show()/.hide()呢?

$("#menu").hover(function(){
    $('.flyout').show();
},function(){
    $('.flyout').hide();
});


Answer 3:

以下是这样做的不同的方法。 我发现你的代码甚至工作的罚款。

您的代码: http://jsfiddle.net/NKC2j/

jQuery的切换类演示: http://jsfiddle.net/NKC2j/2/

jQuery的淡入淡出切换: http://jsfiddle.net/NKC2j/3/

jQuery的幻灯片切换: http://jsfiddle.net/NKC2j/4/

您可以通过桑迪普作为回敬CSS这样做



Answer 4:

我发现使用CSS透明度是一个简单的显示/隐藏悬停更好,你可以添加CSS3过渡,使一个很好的完成悬停效果。 该过渡将只是旧的ie浏览器被丢弃,所以它缓慢下降到。

JS提琴演示

CSS

#stuff {
    opacity: 0.0;
    -webkit-transition: all 500ms ease-in-out;
    -moz-transition: all 500ms ease-in-out;
    -ms-transition: all 500ms ease-in-out;
    -o-transition: all 500ms ease-in-out;
    transition: all 500ms ease-in-out;
}
#hover {
    width:80px;
    height:20px;
    background-color:green;
    margin-bottom:15px;
}
#hover:hover + #stuff {
    opacity: 1.0;
}

HTML

<div id="hover">Hover</div>
<div id="stuff">stuff</div>


Answer 5:

<script type="text/javascript">
    var IdAry=['reports1'];
    window.onload=function() {
     for (var zxc0=0;zxc0<IdAry.length;zxc0++){
      var el=document.getElementById(IdAry[zxc0]);
      if (el){
       el.onmouseover=function() {
         changeText(this,'hide','show')
        }
       el.onmouseout=function() {
         changeText(this,'show','hide');
        }
      }
     }
    }
    function changeText(obj,cl1,cl2) {
       obj.getElementsByTagName('SPAN')[0].className=cl1;
       obj.getElementsByTagName('SPAN')[1].className=cl2;
    }
</script>

乌尔HTML应该是这样的

<p id="reports1">
                <span id="span1">Test Content</span>
                <span class="hide">

                    <br /> <br /> This is the content that appears when u hover on the it
                </span>
            </p>


Answer 6:

你可以使用jQuery来显示DIV,并在任何地方你的鼠标设置:

HTML:

<!DOCTYPE html>
<html>

  <head>
    <link href="style.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  </head>

  <body>
    <div id="trigger">
      <h1>Hover me!</h1>
      <p>Ill show you wonderful things</p>
    </div>
    <div id="secret">
     shhhh
    </div>
    <script src="script.js"></script>
  </body>

</html>

款式:

#trigger {
  border: 1px solid black;
}
#secret {
  display:none;
  top:0;  
  position:absolute;
  background: grey;
  color:white;
  width: 50%;
}

JS:

$("#trigger").hover(function(e){
    $("#secret").show().css('top', e.pageY + "px").css('left', e.pageX + "px");
  },function(e){
    $("#secret").hide()
})

您可以在这里找到的例子干杯! http://plnkr.co/edit/LAhs8X9F8N3ft7qFvjzy?p=preview



文章来源: show/hide a div on hover and hover out