简体   繁体   中英

Remove div message using CSS animate and jQuery

I print success message using PHP like this :

<div class='alert alert-success'>Success!!</div>

I have this CSS3 Animate:

.animated {
  -webkit-animation-duration: 1s;
          animation-duration: 1s;
  -webkit-animation-fill-mode: both;
          animation-fill-mode: both;
}

.animated.infinite {
  -webkit-animation-iteration-count: infinite;
          animation-iteration-count: infinite;
}

.animated.hinge {
  -webkit-animation-duration: 2s;
          animation-duration: 2s;
}
@-webkit-keyframes fadeOutUp {
  0% {
    opacity: 1;
  }

  100% {
    opacity: 0;
    -webkit-transform: translate3d(0, -100%, 0);
            transform: translate3d(0, -100%, 0);
  }
}

@keyframes fadeOutUp {
  0% {
    opacity: 1;
  }

  100% {
    opacity: 0;
    -webkit-transform: translate3d(0, -100%, 0);
            transform: translate3d(0, -100%, 0);
  }
}

.fadeOutUp {
  -webkit-animation-name: fadeOutUp;
          animation-name: fadeOutUp;
}

Now, I need to remove success message with my CSS Animate (fadeOutUp) after 5 seconds using jQuery. How do can i create this?!

You can create a hide a class which hide your element setting the opacity to 0 with a transition and add this class to your div with JavaScript.

CSS

.hide {
  opacity: 0;
  transition: opacity 1000ms;
}

JS

function fadeOut(el){
  el.classList.add('hide');
}

div = document.getElementById('yourDiv');
setTimeout(function(){
  fadeOut(div);
}, 5000);

HTML

<div id='yourDiv' class='alert alert-success'>Success!!</div>

Checkout this codepen .

Is this what you are looking for?

setTimeout(animateUp, 5000);

function animateUp() {
    $(".alert").css({'-webkit-animation' : 'fadeOutUp 5s infinite'});
}

or update your .fadeOutUp CSS to

.fadeOutUp {
    -webkit-animation: fadeOutUp 5s infinite;
     animation: fadeOutUp 5s infinite;
}

Then you can do

setTimeout(animateUp, 5000);

function animateUp() {
    $(".alert").addClass("fadeOutUp");
}

JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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