Androbaut
Androbaut

Reputation: 409

Add animate.css class on mouseenter & mouseleave

I'd like to use animate.css to fadeIn/fadeOut content when the user hovers over a image.

I'm trying to figure out the best way to switch classes from 'FadeIn' & 'FadeInUp' on mouseenter to 'FadeOut' & 'FadeOutDown' on mouseleave.

Codepen Example

CSS

.overlay{
position: absolute;
top: 0;
height: 100%;
width: 100%;
background: rgba(40,40,40,0.88);
left: 0;
padding: 10px;
text-align: left;
display: none;
}
.col-sm-6:hover .overlay{
display: block;
}
.overlay h2{
font-size: 20px;
color: #fff;
}
.overlay p{
color: #bbbbbb;
}
.linkbox{
position: absolute;
height: 50px;
bottom: 0;
left: 0;
background: #000;
width: 100%;
padding: 16px;
text-align: left;
color: #fff;
}
.caption{
display: none;
}

Any help/advice/suggestions will be appreciated!

Upvotes: 1

Views: 1141

Answers (3)

Saurav Rastogi
Saurav Rastogi

Reputation: 9731

Use jQuery instead. Checkout this Codepen.

Also there are some minor CSS changes, that are commented in the CSS code as well. Such as not using .col-sm-6 directly as they are very common classes, etc.

For reference, your JS should look like:

$(".screenshot").hover(function () {
    //stuff to do on mouse enter
    var overlay_div = $(this).find('.overlay');
    overlay_div.removeClass('fadeOut');
    overlay_div.addClass('fadeIn');
    overlay_div.find('.another-animation').removeClass('fadeOutDown');
    overlay_div.find('.another-animation').addClass('fadeInUp');
    overlay_div.css('display', 'block');
}, 
function () {
    //stuff to do on mouse leave
    var overlay_div = $(this).find('.overlay');
    $(this).find('.overlay').removeClass('fadeIn');
    $(this).find('.overlay').addClass('fadeOut');
    overlay_div.find('.another-animation').removeClass('fadeInUp');
    overlay_div.find('.another-animation').addClass('fadeOutDown');
    setTimeout(function() {
        overlay_div.css('display', 'none');
    }, 1000);
});

Hope this helps!

Upvotes: 1

four
four

Reputation: 564

try this script:

$('.col-sm-6').on('mouseenter', function(){
  $('.overlay').addClass('fadeIn fadeInUp');
  $('.overlay').removeClass('fadeOut fadeOutDown');
  $('.overlay').css('display', 'block');
});

$('.col-sm-6').on('mouseleave', function(){
  $('.overlay').removeClass('fadeIn fadeInUp');
  $('.overlay').addClass('fadeOut fadeOutDown');
  $('.overlay').css('display', 'block');
  setTimeout(function(){
     $('.overlay').css('display', 'none');
  }, 1000);
});

Upvotes: 4

Felix Fong
Felix Fong

Reputation: 985

const box = document.querySelector(".box");

box.addEventListener("mouseenter",function(){
  const move = "shake";
  this.classList.add(move);
  
  this.addEventListener("animationend",function(){
    this.classList.remove(move);
  });
  
  this.addEventListener("mouseleave",function(){
    this.classList.remove(move);
  });
  
});
.box{
  background: skyblue;
  width: 5em;
  height: 5em;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
<div class="box animated "></div>

Upvotes: 2

Related Questions