Home > Mobile >  Can't assign animation for each element
Can't assign animation for each element

Time:09-29

I need to assign each 'slideInLeft' animation to the "Hello" message each time I click the button and it shows up. But every time i press the button, it repeats the effect for all. How can I fix it?
For Detail: https://codepen.io/Max333e/pen/GRdQbKv
Thanks for support!

Javascript:

    var i = 0;
    //The function activate when I click on button
    function toast() {
        let toastDom = document.getElementById('toast');
        toastDom.innerHTML  = `
            <div >
              Hello
            </div>
        `;
    
        const j = i;
        let toastEle = document.querySelector(`.box${j}`);
        toastEle.style.animation = `slideInLeft ease .3s`;
        i  ;
    }

CodePudding user response:

It seems to me that the toastDom.innerHTML = ... line causes repaint/reflow to the whole #toast element, thus triggering the animation again. I instead use appendChild() to add the new toast so that it doesn't trigger repaint/reflow as follows:

var i = 0;

let toastDom = document.getElementById('toast');

function toast() {
  let newToastDom = document.createElement('div');
  newToastDom.classList.add(`box${i}`);
  newToastDom.innerText = "Hello";
  newToastDom.style.animation = `slideInLeft ease .3s forwards`;
  toastDom.appendChild(newToastDom)

  i  ;
}
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: "Helvetica Neue";
  background-color: #f4f4f5;
}

body>div {
  margin: auto;
}


/* ======= Buttons ======== */


/* Block */

.btn {
  display: inline-block;
  text-decoration: none;
  background-color: transparent;
  border: none;
  outline: none;
  color: #fff;
  padding: 12px 48px;
  border-radius: 50px;
  cursor: pointer;
  min-width: 120px;
  transition: opacity 0.2s ease;
}


/* Modifier */

.btn--size-l {
  padding: 16px 56px;
}

.btn--size-s {
  padding: 8px 32px;
}

.btn:hover {
  opacity: 0.8;
}

.btn .btn {
  margin-left: 16px;
}

.btn--success {
  background-color: #71be34;
}

.btn--warn {
  background-color: #ffb702;
}

.btn--danger {
  background-color: #ff623d;
}

.btn--disabled {
  opacity: 0.5 !important;
  cursor: default;
}


/* ======= Toast message ======== */

#toast {
  position: fixed;
  top: 32px;
  right: 32px;
  z-index: 999999;
}

.toast {
  display: flex;
  align-items: center;
  background-color: #fff;
  border-radius: 2px;
  padding: 20px 0;
  min-width: 400px;
  max-width: 450px;
  border-left: 4px solid;
  box-shadow: 0 5px 8px rgba(0, 0, 0, 0.08);
  transition: all linear 0.3s;
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(calc(100%   32px));
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeOut {
  to {
    opacity: 0;
  }
}

.toast--success {
  border-color: #47d864;
}

.toast--success .toast__icon {
  color: #47d864;
}

.toast--info {
  border-color: #2f86eb;
}

.toast--info .toast__icon {
  color: #2f86eb;
}

.toast--warning {
  border-color: #ffc021;
}

.toast--warning .toast__icon {
  color: #ffc021;
}

.toast--error {
  border-color: #ff623d;
}

.toast--error .toast__icon {
  color: #ff623d;
}

.toast .toast {
  margin-top: 24px;
}

.toast__icon {
  font-size: 24px;
}

.toast__icon,
.toast__close {
  padding: 0 16px;
}

.toast__body {
  flex-grow: 1;
}

.toast__title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.toast__msg {
  font-size: 14px;
  color: #888;
  margin-top: 6px;
  line-height: 1.5;
}

.toast__close {
  font-size: 20px;
  color: rgba(0, 0, 0, 0.3);
  cursor: pointer;
}
<div id="toast">

</div>

<div>
  <div onclick="toast();" >Show success toast</div>
  <script src="./chuaFix.js">
  </script>
</div>

CodePudding user response:

CSS:

@keyframes slideInLeft { from { transform: translate3d(-100%, 0, 0); visibility: visible; } to { transform: translate3d(0, 0, 0); } } 

Powered by - Light AI

Continue the discussion here - https://stack.lightai.dev/questions/Can't-assign-animation-for-each-element

  • Related