2

I'm changing an image on hover using JS, and would like to add a fading animation to it. I've tried added .fadeToggle(), but nothing is working correctly.

$("document").ready(function(){
  $(".logo_container").mouseenter(function(){
    $("#logo").attr('src',function(index, attr){
      return attr.replace(".png", "-color.png");
    });
  });
  $(".logo_container").mouseleave(function(){
    $("#logo").attr('src',function(index, attr){
      return attr.replace("-color.png",".png");
    });
  }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="logo_container">
  <img src="https://ironhorseclub.wpengine.com/wp-content/uploads/2018/05/logo2-sm.png" alt="Wordpress Install" id="logo" data-height-percentage="100">
</div>
Trisha
  • 539
  • 3
  • 10
  • 30
  • What exactly do you mean by a fading animation? – Taplar May 15 '18 at 22:06
  • CSS/CSS3 animations were made for this. Do you want a jQuery specific solution? If not look at https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions – soulshined May 15 '18 at 22:10
  • 2
    Possible duplicate of [jQuery to change (with fade animation) background image of div on hover](https://stackoverflow.com/questions/18759402/jquery-to-change-with-fade-animation-background-image-of-div-on-hover) – Muhammad Usman May 15 '18 at 22:10

3 Answers3

2

I recommend achieving the same using only CSS

.logo_container {
  height:172px;
  width:403px;
  display:inline-block;
  background-image: url("https://ironhorseclub.wpengine.com/wp-content/uploads/2018/05/logo2-sm.png");
  -webkit-transition: background-image 2s ease-out;
  -moz-transition: background-image 2s ease-out;
  -o-transition: background-image 2s ease-out;
  transition: background-image 2s ease-out;
}
.logo_container:hover {
  background:url("https://ironhorseclub.wpengine.com/wp-content/uploads/2018/05/logo2-sm-color.png") no-repeat center;
}
<div class="logo_container"></div>
vsevolodts
  • 157
  • 8
  • you may see a quick blink of an image when first load the page. This is because the second image that is about to replace the original is not loaded. To avoid this you can use 'preload' or JavaScript. Detals: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content – vsevolodts May 16 '18 at 22:58
1

You can do this with just CSS:

img:hover {
  opacity: 0.5;
  transition: opacity 0.3s linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="logo_container">
  <img src="https://ironhorseclub.wpengine.com/wp-content/uploads/2018/05/logo2-sm.png" alt="Wordpress Install" id="logo" data-height-percentage="100">
</div>

Just change the opacity value, the time value, and/or the easing-type to whatever you want.

Kurt
  • 1,868
  • 3
  • 21
  • 42
1

You can use jQuery fadeTo

$( "img" ).hover(function(){ 
$(this).fadeTo( "slow" , 0.5, function() {
// Animation complete.
});
});
Cliff Rono
  • 324
  • 3
  • 9