【问题标题】:Set animation effect on button click for the same element设置相同元素的按钮单击动画效果
【发布时间】:2018-05-23 12:06:55
【问题描述】:

我有这样的 div 。 H1标签根据按钮点击动态显示数据。

    <div id="quoteId" >
          <h1>{{ data }}</h1>
        </div>
<button>Show Next</button>

使用css3动画效果作为

#quoteId {
    opacity: 1;
    animation-name: example;
    animation-duration: 1s;
}
@keyframes example {
    0%   {opacity: 0;}
    50%  {opacity: 1;}
    100% {opacity: 1;}
}

它是第一次工作。但问题是每次单击按钮时如何实现相同的css效果。如果有任何不清楚的地方,请告诉我。提前致谢。

【问题讨论】:

    标签: javascript css


    【解决方案1】:

    你可以像这样重用动画:

    let but = document.getElementById("button");
    let div = document.getElementById("quoteId");
    but.addEventListener('click', function(){
        div.classList.add("quote");
        setTimeout(() => div.classList.remove("quote"), 1000);
    });
    .quote {
        opacity: 1;
        animation-name: example;
        animation-duration: 1s;
    }
    @keyframes example {
        0%   {opacity: 0;}
        50%  {opacity: 1;}
        100% {opacity: 1;}
    }
    <div id="quoteId">
        <h1>{{ data }}</h1>
    </div>
    <button id="button">Show Next</button>

    【讨论】:

      【解决方案2】:

      A)使用 jQuery:

      $("#quoteId").toggleClass('anime reve');
      

      $(document).ready(function(){
        $('button').click(function(){
          $("#quoteId").toggleClass('anime reve');
        })
      })
      .anime {
        animation-name: example;
        animation-duration: 5s;
      }
      
      .reve {
        animation-name: reve;
        animation-duration: 5s;
      }
      
      @keyframes example {
        0%   {opacity: 0;}
        50%  {opacity: 1;}
        100% {opacity: 1;}
      }
      
      @keyframes reve {
        0%   {opacity: 0;}
        50%  {opacity: 1;}
        100% {opacity: 1;}
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <div id="quoteId" class="reve" >
        <h1>{{ data }}</h1>
      </div>
      <button>Show Next</button>

      B) 使用纯 Css:

      注意::active 选择器仅在按下按钮时应用。

      在使用 active 选择器后,您必须稍微更改 html 代码。

      button:active + #quoteId {
        animation-name: example;
        animation-duration: 20s;
      }
      

      @keyframes example {
        0%   {opacity: 0;}
        50%  {opacity: 1;}
        100% {opacity: 1;}
      }
      
      
      button:active + #quoteId {
        animation-name: example;
        animation-duration: 1s;
      }
      
      .container {
        position: relative;
        padding-bottom: 20px;
      }
      
      button {
        position: absolute;
        bottom: 0;
      }
      <div class="container">
        <button>Show Next</button>
        <div id="quoteId">
          <h1>{{ data }}</h1>
        </div>
      </div>

      【讨论】:

      • :active 选择器仅在按下按钮时应用。当您松开按钮时,动画将停止。
      • 我知道,所以纯 css 没有更好的方法。 jQuery方法的使用。
      猜你喜欢
      • 1970-01-01
      • 2013-03-18
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2014-07-24
      • 1970-01-01
      相关资源
      最近更新 更多