简体   繁体   English

jQuery:当图像悬停时隐藏并显示div

[英]Jquery: Hide and show a div when image is hover

I wonder if someone can help me find a solution to an effect hover to an image in my blog. 我想知道是否有人可以帮助我找到将效果悬停在博客中的图像的解决方案。 The idea was when I hover an image, you see a div with the image information, link project name, date,... 我的想法是,当我将鼠标悬停在图片上时,您会看到一个div,其中包含图片信息,链接项目名称,日期,...

What i have done is, assign two classes do the div information, class show and class hide , and at the beginning it apears with a class hide . 我所做的是,分配两个类来做div信息,即类show和类hide ,并且一开始它会以类hide终止。

Then with jQuery/JavaScript when the img:hover it remove the class hide and add a class show . 然后,当img:hover时,使用jQuery / JavaScript删除类hide并添加类show

the problem is, when i do hover to a image, appears the information of all images. 问题是,当我将鼠标悬停在图像上时,会出现所有图像的信息。

I am wonder if some can help me to make just appear the information of the image that the mouse are hover. 我想知道是否有人可以帮助我使鼠标悬停的图像信息出现。

My HTML: 我的HTML:

<div id="content">
  <img src="images/1.jpg" alt="Projecto 1" height="290" width="220" />
  <div class="information hide">
      <h2>Titulo</h2>
      <p>Lorem Ipsun</p>
      <a href="#" class="info">Read More</a>
  </div>
</div>

<div id="content">
  <img src="images/1.jpg" alt="Projecto 1" height="290" width="220" />
  <div class="information hide">
      <h2>Titulo</h2>
      <p>Lorem Ipsun</p>
      <a href="#" class="info">Read More</a>
  </div>
</div>

My CSS: 我的CSS:

body div.information {
background: rgba(219, 49, 49, 0.52);
width: 220px;
height: 290px;
position: relative;
top: -290px;
}

/* HOVER EFFECT */ / *悬停效果* /

.hide {
display: none;
}

.show {
display: block;
}

My JavaScript: 我的JavaScript:

$('img').hover(function() {
    $('.information').addClass("mostrar");
    $('.information').removeClass("hide");      
});

And by the way, if some one can tell me, how to hide the information again when the image is not hover, I appreciate to. 顺便说一句,如果有人可以告诉我,当图像没有悬停时,如何再次隐藏信息,我非常感谢。

What about something simpler: 那更简单的事情呢:

$("div.content > img").hover(function() {
    $(this).next(".information").show(); //hover in
}, function() {
    $(this).next(".information").hide(); //hover out
});

This way, using jquery .show and .hide you don't need to use the css which you created for the hover effect, since these jquery's functions already take care of the attributes. 这样一来,使用jQuery .show.hide你不需要使用您的悬停效果产生的CSS,因为这些jQuery的功能已经采取属性的照顾。

If you don't need to support IE7 and lower, you can do this with just CSS using the adjacent sibling combinator selector, no JavaScript required: 如果不需要支持IE7及更低版本,则可以使用CSS来使用相邻的兄弟组合器选择器,而无需JavaScript:

img + div.information {
    display: none;
}
img:hover + div.information {
    display: block;
}

That says: "Hide div.information when it's immediately after an img " but "Show div.information when it's immediately after a hovered img ". 那就是说:“在img之后立即隐藏div.information ”,但“ img 悬停之后立即显示div.information ”。 The latter rule being both later in the CSS and (I think ) more specific, it wins when the image is hovered and not when it isn't. 后一个规则既在CSS中出现,也更具体(在我看来 ),它会在图像悬停时(而不是悬停时)获胜。

Live example - Works in modern browsers, including IE8 and higher. 实时示例 -在包括IE8和更高版本的现代浏览器中均可使用。

the problem is, when i do hover to a image, appears the information of all images. 问题是,当我将鼠标悬停在图像上时,会出现所有图像的信息。

I believe this is because you're referencing the generic information class, not the id of a single div. 我相信这是因为您引用的是通用信息类,而不是单个div的ID。 Instead, use the adjacent sibling reference to get only the information for the image you're hovering. 而是使用相邻的兄弟参考仅获取您要悬停的图像的信息。 You could use the :nth-child() selector. 您可以使用:nth-​​child()选择器。

$("#content:nth-child(1)")

Also, you shouldn't have multiple divs with the same id. 另外,您不应有多个具有相同ID的div。

$(this).parent().find('.information').addClass("mostrar")
    .removeClass("hide");   

This will grab the parent of the individual img which is being hovered, search within the parent and find the .information specific to that img. 这将获取要悬停的单个img的父级,在父级中搜索并找到特定于该img的.information

Try this: 尝试这个:

$('img').hover(function(){
    $(this).siblings('.information').removeClass('hide').addClass('mostrar').addClass('show');

}, function(){
    $(this).siblings('.information').removeClass('show').removeClass('mostrar').addClass('hide');
});

Try this 尝试这个

$('img').hover(function() {
    $('.information').addClass("hide")
    $(this).next().addClass("mostrar").removeClass("hide");
}, function(){
    $('.information').addClass("hide").removeClass("mostrar")
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM