【问题标题】:how to revese a css-animation after trigger it?触发后如何反转css动画?
【发布时间】:2020-09-09 18:33:10
【问题描述】:

晚上好,我是一名初级前端开发人员,正在尝试实现 Pinterest 的草图进行培训。 我已经为注册/登录页面找到了这个 gif,但是我在实现动画方面遇到了一个主要问题,当用户点击注册按钮时,我不知道如何反转动画,谁能告诉我如何反转动画?

function animation() {
    var element = document.getElementById("containerForm")
    element.classList.add("slide")
}
.containerForm {
  width:50px;height:50px;background-color:red;
}
.slide {
    animation: slide 2s ease-in-out;
    position: relative;
    animation-fill-mode: forwards;
}

@keyframes slide {
    0% {
        transform: translateX(0);
    }
    100% {
        transform: translateX(100%);
        overflow: hidden;
        visibility: visible;
    }
}
<div class="containerForm" id="containerForm">
      <form></form>
</div>
<button onclick="animation()">click</button>

但正如我所说,我不知道第二步

Link of gif

【问题讨论】:

    标签: javascript html css frontend css-animations


    【解决方案1】:

    您可以添加另一个 css 类并使用动画方向属性。 即

    .slide-back{
      animation: slide 2s ease-in-out;
      position: relative;
      animation-direction: reverse;
    }
    
    

    【讨论】:

      【解决方案2】:

      function animation() {
          var element = document.getElementById("containerForm")
          element.classList.add("slide")
      }
      function reverse() {
          var element = document.getElementById("containerForm")
          element.classList.remove("slide")
      }
      .containerForm {
          width:50px;
          height:50px;
          background-color:red;
          transform: translateX(0);
          transition: transform 2s;
      }
      .slide {
          transform: translateX(100%);
      }
      <div class="containerForm" id="containerForm">
            <form></form>
      </div>
      <button onclick="animation()">click</button>
      <button onclick="reverse()">click</button>

      【讨论】:

        【解决方案3】:

        过渡也有效:

        示例:

        *{margin:0;}
        #tst {
          display: grid;
          grid-template-columns: repeat(2, 1fr);
          min-height: 100vh;
          background: #555;
        }
        
        label {
          margin: auto;
          appearance: button;
          cursor: pointer;
          min-width: 6em;
          text-align: center;
        }
        
        #a,
        #b {/*hides radios */
          position: absolute;
          right: 100vw;
        }
        
        [for="a"],
        #c {
          grid-row: 1;
          grid-column: 1;
        }
        
        #c {
          display: grid;
          grid-template-columns: 1fr;
          grid-template-rows: auto 1fr;
          background: white;
          border: solid;
          transition: 0.5s;
          font-size:2vw;
        }
        
        #a:checked~#c {
          transform: translatex(0)
        }
        
        #b:checked~#c {
          transform: translatex(100%)
        }
        
        h1 {
          margin: auto;
        }
        
        #c p[class] {
          margin: auto;
          grid-column: 1;
          grid-row: 2;
          transition: 0.25s;
          background: inherit;
          color: #164fb7;
          font-size: 3vw;
        }
        
        #c p.b {
          opacity: 0;
          color: tomato;
        }
        
        #b:checked~#c p.b {
          opacity: 1
        }
        /* there were no js nor animation nor absolute positionning ;) */
        <div id="tst">
          <input id="a" type=radio name=toggle>
          <input id="b" type=radio name=toggle>
          <label for="a">Left</label>
          <label for="b">Right</label>
          <div id="c">
            <h1>here or there ?</h1>
            <p class="a">Standing on the left</p>
            <p class="b">Standing on the right</p>
          </div>
        </div>

        【讨论】:

          【解决方案4】:

          我建议不要添加一个类,而是使用animation-play-state 属性并添加事件侦听器:

          • 一个click按钮监听器
          • 一个 animationiteration 到 div
          • 只需将播放状态从paused更改为running 让我告诉你我的意思:

          const element = document.getElementById("containerForm");
          const btn = document.querySelector("button");
          
          function animation() {
            element.style.animationPlayState = "running";
          }
          
          function stopAnimation() {
            element.style.animationPlayState = "paused";
          }
          
          btn.addEventListener("click", animation);
          element.addEventListener("animationiteration", stopAnimation);
            .containerForm {
            width: 50px;
            height: 50px;
            background-color: red;
            animation: slide 2s ease-in-out infinite alternate paused;
          }
          
          @keyframes slide {
            0% {
              transform: translateX(0);
            }
            100% {
              transform: translateX(100%);
            }
          <div class="containerForm" id="containerForm">
            <form></form>
          </div>
          <button>click</button>

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-03
            • 2020-07-24
            • 1970-01-01
            • 2014-11-20
            • 1970-01-01
            相关资源
            最近更新 更多