简体   繁体   English

jQuery此参数用于选择不起作用的孩子

[英]Jquery this parameter for selecting child not working

I've been trying to change image source on hover, however i can use mousehover function with class name and do this. 我一直试图在悬停时更改图像源,但是我可以将mousehover函数与类名一起使用并执行此操作。 The challenge here is i'm going to dynamically call more divs with same class name so i'm trying to achieve this using the this method. 这里的挑战是我将动态调用具有相同类名的更多div,因此我试图使用this方法来实现这一点。 for some unknown reason i couldn't execute my below code. 由于某些未知原因,我无法执行以下代码。 can anyone suggest what seems to be the problem? 谁能建议似乎是问题所在? Pasting my code below 在下面粘贴我的代码

  $(".img_staff").mouseover(function() {
    alert(2);
    $(this).find('.staffimg:first-child').css("display","none");
    $(this).find('.staffimg:nth-child(2)').css("display","block");
    alert(3);
  });

Both the alerts are working fine just the inbetween 2 lines are not working. 两条警报都正常,只是其中两行之间不起作用。 i want to achieve this effect like moca tucson site's contact page https://moca-tucson.org/contact/ I'm trying to recreate the same effect using Jquery 我想实现这种效果,例如moca tucson网站的联系页面https://moca-tucson.org/contact/我正在尝试使用Jquery重新创建相同的效果

Apart from changing the image, the link that your provided also uses CSS transitions to bring that "image transition" effect. 除了更改图像,您提供的链接还使用CSS过渡来带来“图像过渡”效果。

Here's the similar effect with just CSS, without any javascript. 这只是CSS,没有任何javascript的类似效果。

HTML: HTML:

<div>
    <img src="https://moca-tucson.org/wp-content/uploads/2017/05/Ginger_Staff_Photos_001-800x800.jpg">
    <img src="https://moca-tucson.org/wp-content/uploads/2017/05/Ginger_Staff_Photos_002-800x800.jpg">
</div>

CSS: CSS:

div img:last-child { opacity: 0 }
div:hover img:first-child { opacity: 0 }
div:hover img:last-child { opacity: 1 }
img {
    position: absolute;
    transition: 0.3s;
}

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

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