简体   繁体   English

使用 Jquery 或 Javascript 手动触发 animation

[英]Trigger animation manually using Jquery or Javascript

I have an alert/error message div that shows errors in my website我有一个警报/错误消息 div,显示我的网站中的错误

.flash {
  height: 75px;
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 10;
  background-color: #ffffff;
  box-shadow: 0 0 30px 2px #dddddd;
  -webkit-animation: flash-show 300ms ease 0s;
  animation: flash-show 300ms ease 0s;
  &.hide {
    -webkit-animation: flash-hide 5ms ease 0s;
    animation: flash-hide 5ms ease 0s;
    right: -100%;
    opacity: 0;
  }
  .color {
    display: inline-block;
    width: 15px;
    height: 15px;
    border: 5px solid #3498db;
    border-radius: 100%;
    margin: 25px;
    &.green {
      border-color: #2ecc71;
    }
    &.red {
      border-color: #e74c3c;
    }
  }
  .text {
    display: inline-block;
    line-height: 75px;
    vertical-align: top;
    margin-right: 100px;
    font-family: "Roboto";
    font-size: 0.9em;
    font-weight: 300;
    color: rgba(25, 25, 25, 0.75);
  }
  .close {
    width: 16px;
    height: 16px;
    cursor: pointer;
    position: absolute;
    top: 10px;
    right: 10px;
    &:hover span {
      &:before, &:after {
        background-color: rgba(25, 25, 25, 0.25);
      }
    }
    span {
      width: 16px;
      height: 16px;
      position: relative;
      &:before, &:after {
        content: "";
        width: 16px;
        height: 2px;
        background-color: rgba(25, 25, 25, 0.5);
        transition: all 200ms ease;
        position: absolute;
        top: 7px;
      }
      &:before {
        -webkit-transform: rotate(45deg);
        transform: rotate(45deg);
      }
      &:after {
        -webkit-transform: rotate(-45deg);
        transform: rotate(-45deg);
      }
    }
  }
}

@-webkit-keyframes flash-show {
  0% {
    right: -100%;
    opacity: 0;
  }

  100% {
    right: 20px;
    opacity: 1;
  }
}


@keyframes flash-show {
  0% {
    right: -100%;
    opacity: 0;
  }

  100% {
    right: 20px;
    opacity: 1;
  }
}


@-webkit-keyframes flash-hide {
  0% {
    right: 20px;
    opacity: 1;
  }

  100% {
    right: -100%;
    opacity: 0;
  }
}


@keyframes flash-hide {
  0% {
    right: 20px;
    opacity: 1;
  }

  100% {
    right: -100%;
    opacity: 0;
  }
}

I use this div to populate my flash error or success message on page load.我使用这个 div 在页面加载时填充我的 flash 错误或成功消息。 This has been working perfectly till now.到目前为止,这一直运行良好。 However, I wanted to get more use out of this class and trigger it manually on button click instead.但是,我想更多地利用这个 class 并在按钮单击时手动触发它。

I tried adding and removing the class name on a div with an id message我尝试在带有 id message的 div 上添加和删除 class 名称

Inside button click event I had:我有内部按钮单击事件:

  $("#message").addClass("flash")

 $("#message").removeClass("flash")

However, this does not work.但是,这不起作用。 Is there a way to trigger/retrigger the CSS animation using javascript or jquery?有没有办法使用 javascript 或 ZD223E143918865063429EZ52 触发/重新触发 CSS animation?

Please use fadeIn and fadeOut instead of CSS animation.请使用 fadeIn 和 fadeOut 而不是 CSS animation。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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