如何让 jquery 函数只影响“this”的子元素?

发布于 2024-12-14 14:23:52 字数 2040 浏览 1 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-12-21 14:23:52

我认为你可以使用 .children() 来实现此目的:

$(".gallery_image_wrapper").hover(function(){
    $(this).children(':last-child').fadeOut(100);
    $(this).children(':first-child').fadeIn(500);
});

编辑:jsFiddle (暂时放弃你的 CSS),我可能建议你通过启动一个隐藏的孩子然后使用 .toggle() 来简化这一过程:

$(".gallery_image_wrapper").hover(function(){
        $(this).children().toggle(100);
});

I think you could use .children() for this:

$(".gallery_image_wrapper").hover(function(){
    $(this).children(':last-child').fadeOut(100);
    $(this).children(':first-child').fadeIn(500);
});

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():

$(".gallery_image_wrapper").hover(function(){
        $(this).children().toggle(100);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文