在悬停时显示/隐藏div并将其悬停

时间:2012-06-20 07:11:22

标签: jquery html css hover

我想在悬停时显示并隐藏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上时,弹出按钮会闪烁。 那是为什么?

6 个答案:

答案 0 :(得分:31)

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

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

答案 1 :(得分:28)

可能不需要JS。你也可以用css实现这个目标。写得像这样:

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

答案 2 :(得分:16)

以下是执行此操作的不同方法。我发现你的代码工作正常。

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

Jquery切换课程演示:http://jsfiddle.net/NKC2j/2/

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

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

您可以使用CSS执行此操作,如 Sandeep

所示

答案 3 :(得分:3)

我发现使用css不透明度更适合简单的显示/隐藏悬停,并且您可以添加css3过渡以创建一个漂亮的完成悬停效果。旧版IE浏览器只会删除转换,因此会优雅地降级为。

JS fiddle Demo

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>

答案 4 :(得分:1)

<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>

答案 5 :(得分:0)

您可以使用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

相关问题