【问题标题】:Is there a JavaScript function to do a animation in reverse after clicking a button a 2nd time?是否有 JavaScript 函数可以在第二次单击按钮后反向执行动画?
【发布时间】:2020-11-08 23:48:30
【问题描述】:

我还在学习 JavaScript,对它不太熟悉,或者我完全忘记了它。但我的页面上有一个按钮,如下所示:

function hiddenjs() {
     var x = document.getElementById("hiddenjs");
     if (x.style.display === "block") {
          x.style.display = "none";
      } else {
          x.style.display = "block";
     }
   }
   .hiddenjsa {
      margin: 0 auto;
      background-color: red;
      animation-name: expandz;
      animation-duration: 1.5s; 
      animation-timing-function: ease-out; 
      animation-delay: 0;
      animation-direction: alternate;
      animation-fill-mode: both;
      animation-play-state: running;
    }
    
    @keyframes expandz {
      0% {
        transform: scale(.3);
        background-color: red;
        border-radius: 100%;
      }
      50% {
        background-color: rgb(71, 8, 8);;
      }
      100% {
        transform: scale(1.5);
        background-color: rgb(139, 139, 3);
      }
    }
    <div class="btn4">
        <button class="btn4a" onclick="hiddenjs()">List of Other Games I'm going to review!</button>
    </div>
    
    <div id="hiddenjs" class="hiddenjsa">
        Note: These are NOT clickable links yet as I have yet to review them and add them to the site. 
        These are just references to which I am in the process of reviewing!
    </div>

我想知道,如果没有 jQuery 或任何其他形式的语言,如果只有通过 JavaScript 的方法,你可以做一个切换按钮来做动画(我已经做过),当你再次单击所述按钮时,它会做动画但相反?

我已经阅读了一些关于它的内容,但是如果没有复制和粘贴 jQuery sn-p 就找不到一些东西,我不想这样做,因为我想了解我在做什么。

【问题讨论】:

  • 不,这是不可能的,但您可以创建另一个关键帧,然后从那里反转步骤?然后,您决定在哪个时刻启动什么动画。
  • 如何将它与单击按钮一起“关闭”我正在执行的显示/隐藏按钮?
  • 改用过渡。它们只适合这种类型的东西。

标签: javascript html jquery css animation


【解决方案1】:

您可以在每次点击时设置动画以展开或反转展开。代替显示设置,将正向动画开始时的比例设置为 0。

这个 sn-p 故意有两个相同的关键帧,但一组反向运行,只是为了更清楚发生了什么。

function hiddenjs() {
     var x = document.getElementById("hiddenjs");
     if ( x.style.animationDirection != "normal" ) {
x.style.animationDirection = "normal";
x.style.animationName = "expandz";
}
      else {
x.style.animationDirection = "reverse";
x.style.animationName= "expandz1";
   
   }
}
   .hiddenjsa {
      margin: 0 auto;
      transform: scale(0);
      background-color: red;
      animation-duration: 1.5s; 
      animation-timing-function: ease-out; 
      animation-delay: 0;
      animation-fill-mode: forwards;
    }
    
    @keyframes expandz {
      0% { transform: scale(0); }
      1% {
        transform: scale(.3);
        background-color: red;
        border-radius: 100%;
      }
      50% {
        background-color: rgb(71, 8, 8);;
      }
      100% {
        transform: scale(1.5);
        background-color: rgb(139, 139, 3);
      }
    }
@keyframes expandz1 {
0% { transform: scale(0); }
  1% {
    transform: scale(.3);
    background-color: red;
    border-radius: 100%;
  }
  50% {
    background-color: rgb(71, 8, 8);;
  }
  100% {
    transform: scale(1.5);
    background-color: rgb(139, 139, 3);
  }
}
    <div class="btn4" style=“z-index:100;”>
        <button class="btn4a" onclick="hiddenjs()">List of Other Games I'm going to review!</button>
    </div>
    
    <div id="hiddenjs" class="hiddenjsa">
        Note: These are NOT clickable links yet as I have yet to review them and add them to the site. 
        These are just references to which I am in the process of reviewing!
    </div>

【讨论】:

  • 好的,所以它就像一个普通的“如果”语句?这总结起来对我来说真的很好。很抱歉关于 JavaScript 的一个简单问题。我还在学习。 :D 另外,当我尝试这个时,它会一起摆脱动画,所以当我点击我的按钮时,什么也没有出现。我也有这个代码作为 JS 函数: function hiddenjs() { var x = document.getElementById("hiddenjs"); if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; } }
  • 我认为您需要删除该代码-我们已将其替换为使 hiddenjs div 缩放(0)和缩放(1.5)-CSS 动画不允许您为显示设置动画:无等等,但它确实让你可以动画比例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
  • 1970-01-01
  • 2023-03-22
  • 2018-08-11
  • 2014-08-03
相关资源
最近更新 更多