【问题标题】:Start a css animation on button click单击按钮时开始 CSS 动画
【发布时间】:2021-09-27 02:26:53
【问题描述】:

嗨,我一直在寻找有关我的问题的答案可能已经有几个星期了,但我一无所获。我正在尝试进行反应测试,以检查用户在做出反应之前需要多长时间,它会弹出一个正方形或一个圆形,我遇到了问题......

我的问题是,当用户单击屏幕上的按钮时,是否有任何方法可以启动动画?

到目前为止,这是我的代码:

HTML:

  <div id="first-child"></div>
  <button id="Second-parent">Click me !</button>

CSS:

 #first-child {
  height: 200px;
  width: 200px;
  background: white;
  border-radius: 0%;
  margin-top: 150px;
  margin-bottom: 0px;
  margin-left: 500px;
  margin-right: 0px;
  -webkit-animation: myfirst 1s;
  animation: myfirst 1s;
  }
@-webkit-animation myfirst {
    0% {background: white;}
   20% {background: white;}
   40% {background: white;}
   60% {background: white;}
   80% {background: white;}
  100% {background: red;}
  }

#second-parent {
  margin-top: 0px;
  margin-bottom: 0px;
  margin-left: 415px;
  margin-right: 0px;
  }

我更喜欢 CSS、HTML、jQuery 或 Javascript。但如果有其他方法,我也很乐意听到。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    $(function(){
    	$('#second-parent').click(function(){
    		e1 = $('#first-child');
            e1.addClass('animate');
            e1.one('webkitAnimationEnd oanimationend msAnimationEnd animationend',
            function (e) {
                e1.removeClass('animate');
            });
    	});
    });
     #first-child {
      height: 200px;
      width: 200px;
      background: white;
      border-radius: 0%;
      margin-top: 150px;
      margin-bottom: 0px;
      margin-left: 500px;
      margin-right: 0px;
      }
      .animate {
      -webkit-animation: myfirst 3s;
      animation: myfirst 3s;
      }
    @keyframes myfirst {
        0% {background: white;}
       40% {background: gray;}
       70% {background: yellow;}
      100% {background: red;}
      }
    @-webkit-keyframes myfirst {
        0% {background: white;}
       40% {background: gray;}
       70% {background: yellow;}
      100% {background: red;}
      }
    
    #second-parent {
      margin-top: 0px;
      margin-bottom: 0px;
      margin-left: 415px;
      margin-right: 0px;
      }
    <div id="first-child"></div><button id="second-parent">Click me !</button>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    $(function(){
    		$('#second-parent').on('click',function(){
    			$('#first-child').addClass('animate');
    		});
    	});
     #first-child {
      height: 200px;
      width: 200px;
      background: white;
      border-radius: 0%;
      margin-top: 150px;
      margin-bottom: 0px;
      margin-left: 500px;
      margin-right: 0px;
      }
      .animate {
      -webkit-animation: myfirst 3s;
      animation: myfirst 3s;
      }
    @keyframes myfirst {
        0% {background: white;}
       40% {background: gray;}
       70% {background: yellow;}
      100% {background: red;}
      }
    
    #second-parent {
      margin-top: 0px;
      margin-bottom: 0px;
      margin-left: 415px;
      margin-right: 0px;
      }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="first-child"></div>
      <button id="second-parent">Click me !</button>

    【讨论】:

    • 尝试在 Firefox 而不是 Google Chrome 中运行这段代码 sn-p
    • 它可以在 firefox 上运行,但我每次单击按钮时都需要它弹出,而且它只能工作一次:p
    • 我添加了一个新代码 sn-p 它应该在 firefox 和 chrome 中都可以工作,而且每次单击按钮时动画都会工作,而不仅仅是一次
    【解决方案2】:

    为动画使用 css 类,并在单击按钮时将该类添加到 div。 (使用@keyframes定义css动画。)

    【讨论】:

    • 您好,谢谢您的回答,您能否发布一个代码,以便我了解您的意思对于答案,我很感激
    【解决方案3】:

    这是一个使用纯javascript和CSS的解决方案@keyframes:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
      <meta charset="utf-8" />
      <title>CSS Animations</title>
    
      <style>
    /* <![CDATA[ */
    div {
      height: 2em;
      width: 50%;
      border: 1px solid black;
      background-color: black;
      color: yellow;
    }
    
    .animate {
      animation-name: slide-right;
      animation-duration: 2s;
      /* Preserve the effect of the animation at ending */
      animation-fill-mode: forwards;
    }
    
    @keyframes slide-right {
    
      from {
        margin-left: 0px;
      }
    
      50% {
        margin-left: 110px;
        opacity: 0.5;
      }
    
      to {
        margin-left: 200px;
        opacity: 0.2;
      }
    }
    
    /* ]]> */
      </style>
    
      <script>
    /* <![CDATA[ */
    
    function DoAnimation() {
      var targetElement = document.getElementById("target");
      targetElement.className = "animate";
    }
    /* ]]> */
      </script>
    </head>
    <body>
      <h1>CSS Animations</h1>
      <div id="target">Super div</div>
      <button onclick="DoAnimation();">Go</button>
    </body>
    </html>

    【讨论】:

      【解决方案4】:

      从#firstChild 中移除-webkit-animation 和动画定义,改为创建“动画”类定义:

      #first-child {
          height: 200px;
          width: 200px;
          background: white;
          border-radius: 0%;
          margin-top: 150px;
          margin-bottom: 0px;
          margin-left: 500px;
          margin-right: 0px;
      }
      .anim{
          -webkit-animation: myfirst 1s;
          animation: myfirst 1s;
      }
      

      然后,当您想要触发动画时,只需使用 jQUery 将 .anim 类添加到您的元素:

      $("#first-child").addClass("anim");
      

      【讨论】:

      • 它也没有用。我在我的代码中强调了它应该是关键帧而不是动画的问题。原始代码是正确的,我在那里写了关键帧,但是因为我没有复制原始代码,而且我有点着急,所以还是出错了,对此感到抱歉...
      【解决方案5】:

      这里是给你的示例

      #first-child {
        height: 200px;
        width: 200px;
        background: white;
        border-radius: 0%;
        margin-top: 150px;
        margin-bottom: 0px;
        margin-left: 500px;
        margin-right: 0px;
        -webkit-animation: myfirst 1s;
        animation: myfirst 1s;
        }
      @-webkit-animation myfirst {
          0% {background: white;}
         20% {background: white;}
         40% {background: white;}
         60% {background: white;}
         80% {background: white;}
        100% {background: red;}
        }
      
      .second-parent {
        margin-top: 0px;
        margin-bottom: 0px;
        margin-left: 415px;
        margin-right: 0px;
        }
      
      <script>
      $(document).ready(function()
      {
          $('#Second-parent').click(function()
          {
              $('#first-child').addClass('second-parent');
           });
      });
      </script>
      
      </head>
      
      <body>
        <div id="first-child"></div>
        <button id="Second-parent">Click me !</button>
      </body>
      
      </html>
      

      ==> 请在 中添加jquery 库,可以在以下库中下载:http://jquery.com/download/

      【讨论】:

      • 我尝试了代码,但它没有工作......但如果我找到任何解决方案,我会在这里发布
      • 有错误吗?您可以使用console.log 来准确了解。
      • 是的,它说:“Uncaught ReferenceError: $ is not defined”
      • 你添加jquery库了吗?缺少库将显示该错误。
      • 我很困惑我应该添加哪一个?我有一些阅读问题:p
      猜你喜欢
      • 1970-01-01
      • 2017-03-22
      • 2018-10-12
      • 2013-05-29
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 2021-09-26
      • 2016-06-12
      相关资源
      最近更新 更多