如何让 jquery 函数只影响“this”的子元素?
我用 html / twig、css 和 jquery 构建了一个简单的(实际上是粗略的)画廊。在画廊中,我试图创建一个当用户将鼠标悬停在图像上时激活的功能。该函数将使子 div 淡入(隐藏),并在用户将光标从 div 上移开时淡出。
我遇到的问题是该函数不仅影响子元素,而且影响父元素(我想避免)。
jquery:
$(".gallery_image_wrapper").hover(function(){
$(this).find(':last-child').fadeOut(100);
$(this).find(':first-child').fadeIn(500);
});
html/twig (symfony)
<div class="gallery_image_wrapper">
<div class="gallery_image">
<a href="#">
<img src="/web/images/pieceimages/{{ image.path }}" />
</a>
</div>
<div class="gallery_mouseover">
<div class="gallery_piece_info" align="center">
<span class="gallery_info_title">
{{ image.pieceTitle }}
</span>
<br />
<span class="gallery_info_artist">
{{ image.artistName }}
</span>
<br />
<br />
<span class="gallery_info_more">
Learn More
</span>
</div>
</div>
</div>
CSS
.gallery_image_wrapper {
float: left;
position: relative;
margin: 10px;
}
.gallery_mouseover {
height: 100%;
width: 100%;
background-image: url({{ asset('/web/images/mouseover.png') }});
position: absolute;
top: 0px;
}
.gallery_piece_info {
color: #ffffff;
margin-left: auto;
margin-right: auto;
text-transform: uppercase;
margin-top: 30%;
}
.gallery_info_title {
font-size: 16pt;
font-family: "Giacomo";
}
.gallery_info_artist {
font-size: 12pt;
font-family: "Giacomo Light";
}
.gallery_info_more {
font-size: 9pt;
font-family: "Giacomo Light";
}
我将如何实现这个目标?
I building a simple (crude, in fact) gallery in html / twig, css and jquery. In the gallery I am trying to make a function that activates when the user hovers over an image. The function will fade-in a child div (that is hidden) and fade-out on when the user moves the cursor away from the div.
The problem I am running in to is that the function is not only affecting the child element, but also the parent (which I want to avoid).
jquery:
$(".gallery_image_wrapper").hover(function(){
$(this).find(':last-child').fadeOut(100);
$(this).find(':first-child').fadeIn(500);
});
html/twig (symfony)
<div class="gallery_image_wrapper">
<div class="gallery_image">
<a href="#">
<img src="/web/images/pieceimages/{{ image.path }}" />
</a>
</div>
<div class="gallery_mouseover">
<div class="gallery_piece_info" align="center">
<span class="gallery_info_title">
{{ image.pieceTitle }}
</span>
<br />
<span class="gallery_info_artist">
{{ image.artistName }}
</span>
<br />
<br />
<span class="gallery_info_more">
Learn More
</span>
</div>
</div>
</div>
css
.gallery_image_wrapper {
float: left;
position: relative;
margin: 10px;
}
.gallery_mouseover {
height: 100%;
width: 100%;
background-image: url({{ asset('/web/images/mouseover.png') }});
position: absolute;
top: 0px;
}
.gallery_piece_info {
color: #ffffff;
margin-left: auto;
margin-right: auto;
text-transform: uppercase;
margin-top: 30%;
}
.gallery_info_title {
font-size: 16pt;
font-family: "Giacomo";
}
.gallery_info_artist {
font-size: 12pt;
font-family: "Giacomo Light";
}
.gallery_info_more {
font-size: 9pt;
font-family: "Giacomo Light";
}
How would I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以使用
.children()
来实现此目的:编辑: 在 jsFiddle (暂时放弃你的 CSS),我可能建议你通过启动一个隐藏的孩子然后使用
.toggle()
来简化这一过程:I think you could use
.children()
for this:Edit: After playing with this a bit in jsFiddle (ditching your CSS for the moment), I might suggest that you make this simpler by starting one child hidden and then using
.toggle()
: