概述 animated selector
返回值:Array<Element(s)>
描述:选择所有正在执行动画效果的元素.
注意: 如果您使用一个自定义的jQuery绑定一个没有效果模块, :animated选择器会抛出一个错误。
:animated 是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分,使用:animated 查询不能充分利用原生DOM提供的querySelectorAll() 方法来提高性能。为了当使用:animated 的时候在现代浏览器上获得更佳的性能,首先使用纯CSS选择器选择元素,然后使用.filter(":animated").
示例
只有对不在执行动画效果的元素执行一个动画特效
<button id="run">Run</button><div></div>
$("#run").click(function(){
$("div:not(:animated)").animate({ left: "+=20" }, 1000);
});
改变正在执行动画的 div 的颜色。
<!doctype html> <html lang="en">
<head>
<meta charset="utf-8">
<title>animated demo</title>
<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://code.jquery.com/jquery-1.10.2.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>
运行一下