代码之家  ›  专栏  ›  技术社区  ›  Rad

如果一个元素正在被动画化,我如何用jquery找出它?

  •  97
  • Rad  · 技术社区  · 17 年前

    我试图移动页面上的一些元素,在动画发生的过程中,我希望将“overflow:hidden”应用于元素,并在动画完成后将“overflow”返回“auto”。

    我知道jquery有一个实用程序函数,可以确定某个元素是否正在被动画化,但在文档中的任何地方都找不到它。

    5 回复  |  直到 9 年前
        1
  •  193
  •   James    17 年前
    if( $(elem).is(':animated') ) {...}
    

    更多信息 : http://docs.jquery.com/Selectors/animated


    或:

    $(elem)
        .css('overflow' ,'hidden')
        .animate({/*options*/}, function(){
            // Callback function
            $(this).css('overflow', 'auto');
        };
    
        2
  •  5
  •   Bill    10 年前

    或者,要测试某个内容是否为动画,只需添加一个“!”:

    if (!$(element).is(':animated')) {...}
    
        3
  •  0
  •   Lucky    9 年前

    如果要将CSS应用于动画元素,可以使用 :animated 伪选择器,然后这样做,

    $("selector").css('overflow','hidden');
    $("selector:animated").css('overflow','auto');
    

    来源: https://learn.jquery.com/using-jquery-core/selecting-elements/

        4
  •  0
  •   Erazihel    9 年前
    $('selector').click(function() {
      if ($(':animated').length) {
        return false;
      }
    
      $("html, body").scrollTop(0);
    });
    
        5
  •  -1
  •   Ari    11 年前

    如果您正在使用 css 通过使用特定的 class name ,然后您可以这样检查它:

    if($("#elem").hasClass("your_animation_class_name")) {}
    

    但确保在动画完成后移除处理动画的类名!

    此代码可用于删除 类名 动画完成后:

    $("#elem").on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',
    function(){ 
            $(this).removeClass("your_animation_class_name");
    });