【问题标题】:How to end CSS transition with jquery or js?如何用 jquery 或 js 结束 CSS 过渡?
【发布时间】:2018-09-16 06:25:33
【问题描述】:

我想用 jquery 或 js 结束我的 css 转换。我不是说暂停,我想结束它们,就好像它们已经 100% 完成了一样。我有 css 过渡而不是动画。

我使用过渡的属性是top和left,它是一个位置绝对元素。

【问题讨论】:

  • 分享您的代码,以便我们为您提供帮助。

标签: javascript jquery transition


【解决方案1】:

您可以简单地将转换属性规则覆盖为none。

#el {
  position: relative;
  left: 0;
  top: 25px;
  width: 50px;
  height: 50px;
  transition: all 5s linear;
  background: red;
}
body:hover #el{
  left: calc(100vw - 50px);
}
button:active + #el {
  transition-property: none;
}
:root,body{margin:0}
<button>stop transition</button>
<div id="el"></div>

现在你如何触发它取决于你,它可以使用一个特殊的类,或者任何其他条件。

【讨论】:

    【解决方案2】:

    你好,你的问题有点含糊。

    如果您使用过渡而不是动画,您可以在同一个 css 示例中控制它的流程:

       transition: background-color .2s linear 0s; /*Standard*/
    

    如果您想用 JS 中断转换,您可以在触发某些您想要的操作时将其他 Css valor 分配给不同的类名或属性。

           .normal{
            transition: background-color .2s linear 0s; /*Standard*/ 
            }  
    
             [stop = yes] {
              background: black; 
              } 
    
            document.body.addEventListener('mouseover', function(e){
            e.stopPropagation();
            if(someting you want){
              document.queryselector(".normal").setAttribute("stop","yes");
             }
         else{
           document.queryselector(".normal").setAttribute("stop","no");
          }
         },false);
    

    如果你想要的东西被触发,那么属性将被设置为 no 过渡,这也切断了正在运行的过渡。

    【讨论】:

      【解决方案3】:

      $('.toggle-animation').click(function(){
        $('.element').stop(true).toggleClass('animating');
      });
      .element{
          top: 0px;
          left: 0px;
          width: 100px;
          height: 100px;
          position:relative;
          background: red
      }
      .animating{
          top: 100px;
          left: 100px;;
          transition: all 5s linear 0s;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="element"></div>
      <button type="button" class="toggle-animation">Click me</button>

      【讨论】:

      • 嗯,我很确定你在意外中两次发布了完全相同的代码
      猜你喜欢
      • 2012-03-04
      • 2012-12-11
      • 2013-05-19
      • 2014-05-03
      • 2015-07-10
      • 2015-10-12
      • 2016-02-23
      • 1970-01-01
      相关资源
      最近更新 更多