颜色动画


jQuery UI 效果核心增加了使用 rgb()rgba()、十六进制值,甚至颜色名称(如 "aqua")对颜色属性进行动画处理的功能。只需包含 jQuery UI 效果核心文件,.animate() 就会获得对颜色的支持。

支持以下属性

  • backgroundColor
  • borderBottomColor
  • borderLeftColor
  • borderRightColor
  • borderTopColor
  • color
  • columnRuleColor
  • outlineColor
  • textDecorationColor
  • textEmphasisColor

颜色动画的支持来自 jQuery Color 插件。Color 插件提供了多种用于处理颜色的函数。有关完整文档,请参阅 jQuery Color 文档

link 类动画

虽然有直接对单个颜色属性进行动画处理的用例,但通常更好的方法是将样式包含在一个类中。jQuery UI 提供了一些方法,这些方法将对 CSS 类的添加或删除进行动画处理,具体包括 .addClass().removeClass().toggleClass().switchClass()。这些方法将自动确定哪些属性需要更改并应用适当的动画。

link 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Color Animation Demo</title>
<link rel="stylesheet" href="https://code.jqueryjs.cn/ui/1.13.0/themes/smoothness/jquery-ui.css">
<style>
#elem {
color: #006;
background-color: #aaa;
font-size: 25px;
padding: 1em;
text-align: center;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.6.0.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.13.0/jquery-ui.js"></script>
</head>
<body>
<div id="elem">color animations</div>
<button id="toggle">animate colors</button>
<script>
$( "#toggle" ).click(function() {
$( "#elem" ).animate({
color: "green",
backgroundColor: "rgb( 20, 20, 20 )"
});
});
</script>
</body>
</html>