[英]jQuery hover in and out fade transition when changing image src
I have coded the following which changes the image source and does some attempt of a nice fadeout and fadein when changing the image source but it is not perfect and trying to make it smooth.我编写了以下更改图像源的代码,并在更改图像源时尝试了一些不错的淡出和淡入,但它并不完美并试图使其平滑。
The issues I'm facing are:我面临的问题是:
Note the first image has active status so no hover required, so please hover the other images to see what I mean.请注意,第一张图片处于活动状态,因此不需要悬停,因此请悬停其他图片以了解我的意思。
var $profile = $(".influencers-block-profiles"); var $profileLink = $profile.find("a"); var $activeProfile = $profileLink.hasClass("active"); var imageHoverName = "-hover.jpg"; var imageColourName = "-colour.jpg"; $($profile).find("img").hover(function() { $activeProfile = $(this).closest("a").hasClass("active"); if (!$activeProfile) { var src = $(this).attr('src').replace(imageColourName, imageHoverName); $(this).not('[src=' + src + ']').fadeOut(500, 0).attr('src', src).fadeIn(500); } return false }, function() { $activeProfile = $(this).closest("a").hasClass("active"); if (!$activeProfile) { var src = $(this).attr('src').replace(imageHoverName, imageColourName); $(this).not('[src=' + src + ']').fadeOut(500, 0).attr('src', src).fadeIn(500); } return false });
.influencers-block-profiles img { width: 50%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="influencers-block-profiles"> <a href="#" class="active"> <img src="https://www.hostandname.co.uk/clients/tests/images/test-photo-hover.jpg" /> </a> <a href="#"> <img src="https://www.hostandname.co.uk/clients/tests/images/test-photo-colour.jpg" /> </a> <a href="#"> <img src="https://www.hostandname.co.uk/clients/tests/images/test-photo-colour.jpg" /> </a> </div>
Here's a working example这是一个工作示例
var $profile = $(".influencers-block-profiles"); var $profileLink = $profile.find("a"); var $activeProfile = $profileLink.hasClass("active"); var imageHoverName = "-hover.jpg"; var imageColourName = "-colour.jpg"; var TRANSITION_DURATION = 500, inTimeout; $profile.find("img").hover(function() { $activeProfile = $(this).closest("a").hasClass("active"); if (!$activeProfile) { var src = $(this).attr('src').replace(imageColourName, imageHoverName); var self = this; clearTimeout(inTimeout); this.style.opacity = 0; inTimout = setTimeout(function(){ self.src = src; self.style.opacity = 1 }, TRANSITION_DURATION ) } return false }, function() { $activeProfile = $(this).closest("a").hasClass("active"); if (!$activeProfile) { var src = $(this).attr('src').replace(imageHoverName, imageColourName); var self = this; clearTimeout(inTimeout); this.style.opacity = 0; inTimout = setTimeout(function(){ self.src = src; self.style.opacity = 1 }, TRANSITION_DURATION ) } return false });
.influencers-block-profiles img { transition: opacity 0.5s ease; width: 50%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="influencers-block-profiles"> <a href="#" class="active"> <img src="https://www.hostandname.co.uk/clients/tests/images/test-photo-hover.jpg" /> </a> <a href="#"> <img src="https://www.hostandname.co.uk/clients/tests/images/test-photo-colour.jpg" /> </a> <a href="#"> <img src="https://www.hostandname.co.uk/clients/tests/images/test-photo-colour.jpg" /> </a> </div>
For stop repeating you need to use jQuery stop()
.要停止重复,您需要使用 jQuery stop()
。 Check out this answer: https://stackoverflow.com/a/9113598/9453736看看这个答案: https : //stackoverflow.com/a/9113598/9453736
For the animation change image with fade in and our I suggest you do this: use a temporary image, I mean a duplicate image of first image and put that duplicate on top of the image that was there.对于带有淡入的动画更改图像,我们建议您这样做:使用临时图像,我的意思是第一张图像的复制图像并将该复制图像放在那里的图像之上。
Then you change the original image's src to new image, and hide the original image which is not behind the temporary image so far no change is seen.然后将原始图像的 src 更改为新图像,并隐藏不在临时图像后面的原始图像,到目前为止看不到任何变化。
Then you do two animations.然后你做两个动画。 Fade out the temporary image which is on top and fade in the original image which was on back.淡出顶部的临时图像并淡入背面的原始图像。 Then you remove the temporary image.然后删除临时图像。
If you had trouble understanding my idea, or have troubles implementing it, let me know如果您在理解我的想法时遇到困难,或者在实施时遇到困难,请告诉我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.