CodeMan03
CodeMan03

Reputation: 226

Css not fading in smoothly after hover leave

I am trying to get this image to fade back in smoothly after hover leave however it seems to just pop back in and I want the transition to look smooth and blend together on fade in the same way it works on fade out.

I may have gone overkill with the css but my main objective is to have the first div be an image and the second to be a background with information on it.

.imageInfo {
  display: block;
  position: absolute;
  bottom: 5px;
  left: 15px;
  color: black;
  font-style: italic !important;
  font-size: smaller;
}

.imageContainer {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  margin-left: 15px;
  margin-bottom: 20px;
  min-width: 330px;
  height: 150px;
}

.cImage {
  max-width: 400px;
  min-height: 130px;
  width: 100%;
  height: 100%;
  padding: 5px;
  vertical-align: middle;
}

imageContainer.img {
  width: auto;
  display: inline-block;
}

.imageContainer img.play,
.imageContainer img.picture,
.imageContainer img.fade {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  transition: ease-in-out 0.6s;
}

.imageContainer img.play {
  opacity: 1;
  visibility: visible;
}

.imageContainer:hover img.play {
  opacity: 0;
  visibility: hidden;
}

.imageContainer div.picture {
  opacity: 0;
  visibility: hidden;
}

.gradient {
  background-image: linear-gradient(to top, orange, red);
}

.imageContainer div.picture .fade {
  opacity: 0;
  visibility: hidden;
}

.imageContainer:hover div.picture {
  opacity: 1;
  visibility: visible;
}

.imageContainer:hover div.picture .fade {
  opacity: 1;
  visibility: visible;
}
<span class="imageContainer">
        <img  [src]="https://www.w3schools.com/images/w3schools_green.jpg"
          class="play cImage">   
            <div class="picture cImage gradient">
              <a class="fade">Baby Yoda</a>
              <div class="imageInfo fade">
               <strong>Date:</strong><span>"01/01/1999"</span>
</div>
</div>
</span>

Upvotes: 0

Views: 104

Answers (2)

AbyssfireX
AbyssfireX

Reputation: 1

Unfortunately, there isn't a real way (that I know of, at least) to make it fade in simply when your mouse leaves but not when it loads. So, I just made it load in with the show animation and placed on a "loading..." that fades as the images comes on. Sorry, but I couldn't adapt it with you code, because I couldn't make complete sense of it, so I created my own code. Adapt it to fill your code:

img{
  position: absolute;
  width: 100%
  height: 100%;
  z-index: 1;
  animation-name: show;
  animation-duration: 1s;
}

img:hover{
  animation-name: fade;
  animation-duration: 1s;
  opacity: 0;
}

#hi{
  background-color: green;
}

#loading{
  opacity: 0;
  position: absolute;
  z-index: 2;
  font-size: 20px;
  text-align: center;
  animation-name: fade;
  animation-duration: 1s;
}

@keyframes fade{
  from{opacity: 1;}
  to{opacity: 0;}
}

@keyframes show{
  from{opacity: 0;}
  to{opacity: 1;}
}
    
<p>Opacity Animation Test</p>
<div>
  <p id="loading">Loading...</p>
  <img src="https://via.placeholder.com/150">
  <div id="hi">
    <a href="#">HI</a>
    <p><b>Date:</b>1/1/10</p>
  </div>
</div>

The vital parts are the z-index attributes (keeps the picture in front of the text), position absolute, the animations (for obvious reasons), animation-name and animation-duration (they trigger the animation, but feel free to edit the duration time), and the opacity.

I hope this answered your question.

Upvotes: 0

Rahul
Rahul

Reputation: 2071

Use src instead of [src]. Change <img [src]="https://www.w3schools.com/images/w3schools_green.jpg" class="play cImage" /> to <img src="https://www.w3schools.com/images/w3schools_green.jpg" class="play cImage" />

.imageInfo {
  display: block;
  position: absolute;
  bottom: 5px;
  left: 15px;
  color: black;
  font-style: italic !important;
  font-size: smaller;
}

.imageContainer {
  position: relative;
  display: inline-block;
  vertical-align: middle;
  margin-left: 15px;
  margin-bottom: 20px;
  min-width: 330px;
  height: 150px;
}

.cImage {
  max-width: 400px;
  min-height: 130px;
  width: 100%;
  height: 100%;
  padding: 5px;
  vertical-align: middle;
}

imageContainer.img {
  width: auto;
  display: inline-block;
}

.imageContainer img.play,
.imageContainer img.picture,
.imageContainer img.fade {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  transition: opacity 0.6s linear, visibility 0.6s linear;
}

.imageContainer img.play {
  opacity: 1;
  visibility: visible;
  transition: opacity 0.6s linear, visibility 0.6s linear;
}

.imageContainer:hover img.play {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.6s linear, visibility 0.6s linear;
}

.imageContainer div.picture {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.6s linear, visibility 0.6s linear;
}

.gradient {
  background-image: linear-gradient(to top, orange, red);
}

.imageContainer div.picture .fade {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.6s linear, visibility 0.6s linear;
}

.imageContainer:hover div.picture {
  opacity: 1;
  visibility: visible;
  transition: opacity 0.6s linear, visibility 0.6s linear;
}

.imageContainer:hover div.picture .fade {
  opacity: 1;
  visibility: visible;
  transition: opacity 0.6s linear, visibility 0.6s linear;
}
<span class="imageContainer">
        <img src="https://www.w3schools.com/images/w3schools_green.jpg" class="play cImage">   
            <div class="picture cImage gradient">
              <a class="fade">Baby Yoda</a>
              <div class="imageInfo fade">
               <strong>Date:</strong><span>"01/01/1999"</span>
</div>
</div>
</span>

After your comments if I understand correctly then you want same transition effect on mouse in and on mouse leave. Then transition-timing-function will be linear instead of easy-in-out | easy-in | ease-out etc. You have to read more about transition-timing-function. And you have to separate transition-propery like (opacity, visibility). You have to read more about transition-property.

You wrote transition only for :hover or mouse in. Now you have to write transition for mouse leave. You CSS will be like this.

.imageContainer img.play{
    opacity: 1;
    visibility: visible;
    transition: opacity 0.6s linear, visibility 0.6s linear;
}

.imageContainer:hover img.play{
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.6s linear, visibility 0.6s linear;
}

Check above code snippet it's working mouse in and mouse out.

Upvotes: 1

Related Questions