简体   繁体   English

使用jQuery将悬停效果添加到同一类的不同div中的特定元素

[英]Add hover effect to specific elements in different divs of same class with jQuery

I have a grid of images all have the same class (which I would like to toggle when you hover over the image. These images are pull from a mongo database. 我有一个图像网格,所有图像都具有相同的类(当您将鼠标悬停在图像上时,我想切换该类。这些图像是从mongo数据库中提取的。

Here is my HTML (using ejs to do the logic to loop through all images on the database) 这是我的HTML(使用ejs进行遍历数据库中所有图像的逻辑)

 <div class="ui two column padded stackable center aligned grid">
  <% videos.forEach(function(video){ %>
    <div class="column">
      <div class="ui fluid inverted segment">
        <img class="ui rounded image" src="<%= video.thumbnail %>" href="/videos/<%= video._id %>">
        <div class="textbox hidden">
          <div class="description"><%= video.description %></div>
        </div> 
      </div>
    </div>
  <% }); %>
</div>

I am trying to toggle the hidden class on textbox div, so the text pops out the bottom (also using semantic-UI for some styling) 我试图在文本框div上切换隐藏的类,所以文本从底部弹出(也使用语义UI进行某些样式设置)

Here is my jQuery 这是我的jQuery

$(".image").hover(function(){
$(".textbox").removeClass("hidden");
}, function(){
$(".textbox").addClass("hidden")

}); });

here is an image of the sort of thing I want to achieve (only different the text appears when you hover on the image) 这是我要实现的目标的图像(当您将鼠标悬停在图像上时,只会显示不同的文本)

Thanks Miles 感谢迈尔斯

Edit: The answer below works wonderfully however I would like to animate the hover effect, at the moment it is just changing from display: none; 编辑:下面的答案很好用,但是我想为悬停效果设置动画,目前它只是从显示更改:无; to display: block; 显示: How do I animate this so it fades in/out? 如何为它设置动画以使其淡入/淡出? Miles 迈尔斯

Updated answer: 更新的答案:

Showing and hiding the text while animating can be achieved using a different approach, whereby you can change the margin of the text on hover and attaching an animation effect of your choice. 可以在动画时显示和隐藏文本,可以使用另一种方法来实现,您可以在悬停时更改文本的边距并附加您选择的动画效果。

For a fade in/out animation see the answer by @Kris, which makes use of opacity rather than margin. 有关淡入/淡出动画,请参阅@Kris的答案,该答案使用了不透明度而不是边距。

Good article on CSS transitions: https://css-tricks.com/almanac/properties/t/transition/ 关于CSS过渡的好文章: https//css-tricks.com/almanac/properties/t/transition/

 .segment { position: relative; } .segment img.image { position: relative; z-index: 10; } .segment .textbox.hidden { position: relative; z-index: 5; transition: ease-out 0.5s; margin-top: -30px; } .segment:hover .textbox.hidden { margin-top: 0; } 
 <div class="ui two column padded stackable center aligned grid"> <div class="column"> <div class="ui fluid inverted segment"> <img class="ui rounded image" src="http://www.skrenta.com/images/stackoverflow.jpg" href="/videos/<%= video._id %>"> <div class="textbox hidden"> <div class="description">Video Description</div> </div> </div> </div> </div> 

Original answer: 原始答案:

You don't need JavaScript or jQuery to achieve this. 您不需要JavaScript或jQuery即可实现此目的。 It can be done with CSS alone. 可以单独使用CSS来完成。 The text can be hidden by default and you can then define a rule that on the parent hover, the child text will be shown. 默认情况下可以隐藏文本,然后可以定义一个规则,即在父悬停时将显示子文本。

Have a look at the code: 看一下代码:

 .hidden { display: none; } .segment:hover .hidden { display: block } 
 <div class="ui two column padded stackable center aligned grid"> <div class="column"> <div class="ui fluid inverted segment"> <img class="ui rounded image" src="http://www.skrenta.com/images/stackoverflow.jpg" href="/videos/<%= video._id %>"> <div class="textbox hidden"> <div class="description">Video Description</div> </div> </div> </div> </div> 

The animation should work if you start at opacity:0 and set opacity:1 when you hover over the segment. 如果将鼠标悬停在该段上,则动画应该可以从opacity:0开始并设置opacity:1来工作。 Here's an example: 这是一个例子:

HTML: HTML:

<div class = "segment">
  <h1>Hi There!</h1>
  <p class = "hidden">This is hidden</p>
</div>

CSS: CSS:

.hidden{
  opacity:0;
  transition: 1s linear;
}

.segment:hover .hidden{
  opacity:1;
}

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

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