【问题标题】:Interrupting/stop a CSS3 transition on the actual position/state在实际位置/状态上中断/停止 CSS3 转换
【发布时间】:2011-07-15 19:11:22
【问题描述】:

我正在编写一个 jQuery 插件来通过 CSS3 转换为元素设置动画。在 jQuery 中,.stop() 会中断所选元素上的当前动画。

知道如何停止正在运行的 CSS3 动画吗?是否有本地方法来处理这个问题,还是我必须测量动画,并将动画元素的样式设置为当前位置、颜色大小或其他?

这是 jQuery 插件的当前状态: http://jsfiddle.net/meo/r4Ppw/

我尝试将“-webkit-transition-duration”设置为 0 / none / false。但它不会停止动画。

【问题讨论】:

  • 您是要阻止它-将其保持在当前级别-暂停)还是将动画跳到最后?
  • 我想同时实现,所以我对任何类型的输入 '.stop(false, ture)' 和 '.stop(false,false)' 都开放

标签: javascript css


【解决方案1】:

无需深入了解您的插件,您只需将当前 (computed) 值用于所需道具,并将持续时间设置为 0 (1 在你的插件中,因为你使用!speed 来使用默认值..)

所以在示例中使用

var $animated = $('div');
$animated.css3animate({"height": $animated.css('height'), "width": $animated.css('width')}, 1);

会成功的。

http://jsfiddle.net/T5LVX/1/ 的示例

当然,您应该为当前使用的属性自动执行此操作。如果您在启动动画时使用计时器,而在有人使用停止方法时使用计时器,您还可以模拟暂停/恢复功能..


更新

您可以将传递给动画的cssObject 存储到元素的data 中,并在停止循环时通过它们获取当前值。

所以你可以在 animation 方法中使用 $obj.data('animationCss', cssObject);stop 方法

stop : function( clearQueue,jumpToEnd ){
    return this.each(function(){
        var $that = $(this);
        var currentCss = {};
        var animationCss = $that.data('animationCss');
        for (var prop in animationCss)
            currentCss[prop] = $that.css(prop);

        if (jumpToEnd)
        {
            animation($that,currentCss,1, function(){animation($that,animationCss,1)});
        }
        else
        {
            animation($that,currentCss,1);
        }
       console.log("stop was called");
    });
   }

示例:http://jsfiddle.net/T5LVX/6/

【讨论】:

  • 好吧,我很害怕我不得不这样做:/我希望有一种本地的方式来做到这一点。感谢你的付出。 +1 PS:知道自动化当前属性的最佳方法是什么吗?
猜你喜欢
  • 2018-10-12
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
  • 2012-06-17
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
相关资源
最近更新 更多