:animated
选择所有正在执行动画效果的元素.
jQuery(":animated")
注意:如果您使用一个自定义的jQuery绑定一个没有效果模块,:animated
选择器会抛出一个错误。
注意
- 因为
:animated
是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用:animated
查询不能充分利用原生DOM提供的querySelectorAll()
方法来提高性能。为了当使用:animated
的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用.filter(":animated")
.
例子
改变正在执行动画的 div 的颜色。
<!DOCTYPE html> <html> <head> <style> div { background:yellow; border:1px solid #AAA; width:80px; height:80px; margin:0 5px; float:left; } div.colored { background:green; } </style> <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script> </head> <body> <button id="run">Run</button> <div></div> <div id="mover"></div> <div></div> <script> $("#run").click(function (){ $("div:animated").toggleClass("colored"); });function animateIt() { $("#mover").slideToggle("slow", animateIt); } animateIt(); </script> </body> </html>