## jQuery 效果- 動畫
在使用jQuery動畫時,你可能想要實現更加豐富的效果,那么你可以通過使用?jQuery animate() 方法自定義動畫來達到目的,具體的使用方法如下文所述。
*****
## jQuery 動畫 - animate() 方法
jQuery animate() 方法用于創建自定義動畫。
**語法:**
```
$(*selector*).animate({*params*}*,speed,callback*);
```
必需的 params 參數定義形成動畫的 CSS 屬性。
可選的 speed 參數規定效果的時長。它可以取以下值:"slow"、"fast" 或毫秒。
可選的 callback 參數是動畫完成后所執行的函數名稱。
下面的例子演示 animate() 方法的簡單應用。它把 元素往右邊移動了 250 像素:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left:'250px'});
});
});
</script>
</head>
<body>
<button>開始動畫</button>
<p>默認情況下,所有的 HTML 元素有一個靜態的位置,且是不可移動的。
如果需要改變為,我們需要將元素的 position 屬性設置為 relative, fixed, 或 absolute!</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
```
## jQuery animate() - 操作多個屬性
請注意,生成動畫的過程中可同時使用多個屬性:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
});
</script>
</head>
<body>
<button>開始動畫</button>
<p>默認情況下,所有的 HTML 元素有一個靜態的位置,且是不可移動的。
如果需要改變為,我們需要將元素的 position 屬性設置為 relative, fixed, 或 absolute!</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
```
**可以用 animate() 方法來操作所有 CSS 屬性嗎?**
是的,幾乎可以!不過,需要記住一件重要的事情:當使用 animate() 時,必須使用 Camel 標記法書寫所有的屬性名,比如,必須使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margin-right,等等。
同時,色彩動畫并不包含在核心 jQuery 庫中。
如果需要生成顏色動畫,您需要從 [jquery.com](https://jquery.com/download/) 下載 [Color Animations](https://plugins.jquery.com/color) 插件。
*****
## jQuery animate() - 使用相對值
也可以定義相對值(該值相對于元素的當前值)。需要在值的前面加上 += 或 -=:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left:'250px',
height:'+=150px',
width:'+=150px'
});
});
});
</script>
</head>
<body>
<button>開始動畫</button>
<p>默認情況下,所有的 HTML 元素有一個靜態的位置,且是不可移動的。
如果需要改變為,我們需要將元素的 position 屬性設置為 relative, fixed, 或 absolute!</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
```
## jQuery animate() - 使用預定義的值
您甚至可以把屬性的動畫值設置為 "show"、"hide" 或 "toggle":
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
height:'toggle'
});
});
});
</script>
</head>
<body>
<button>開始動畫</button>
<p>默認情況下,所有的 HTML 元素有一個靜態的位置,且是不可移動的。
如果需要改變為,我們需要將元素的 position 屬性設置為 relative, fixed, 或 absolute!</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
```
## jQuery animate() - 使用隊列功能
默認地,jQuery 提供針對動畫的隊列功能。
這意味著如果您在彼此之后編寫多個 animate() 調用,jQuery 會創建包含這些方法調用的"內部"隊列。然后逐一運行這些 animate 調用。
## 實例 1
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div=$("div");
div.animate({height:'300px',opacity:'0.4'});
div.animate({width:'300px',opacity:'0.8'});
div.animate({height:'100px',opacity:'0.4'});
div.animate({width:'100px',opacity:'0.8'});
});
});
</script>
</head>
<body>
<button>開始動畫</button>
<p>默認情況下,所有的 HTML 元素有一個靜態的位置,且是不可移動的。
如果需要改變為,我們需要將元素的 position 屬性設置為 relative, fixed, 或 absolute!</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
```
##實例2
下面的例子把 元素往右邊移動了 100 像素,然后增加文本的字號:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="//libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div=$("div");
div.animate({left:'100px'},"slow");
div.animate({fontSize:'3em'},"slow");
});
});
</script>
</head>
<body>
<button>開始動畫</button>
<p>默認情況下,所有的 HTML 元素有一個靜態的位置,且是不可移動的。
如果需要改變為,我們需要將元素的 position 屬性設置為 relative, fixed, 或 absolute!</p>
<div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div>
</body>
</html>
```
## **提示:**有關jQuery動畫的更多介紹,請參考 [jQuery?效果?-?animate()?方法](https://www.w3cschool.cn/jquery/effect-animate.html)!
- 簡介
- 安裝
- 語法
- 選擇器
- 事件
- click
- dblclick
- mouseenter
- mouseleave
- mousedown
- mouseup
- hover
- focus
- blur
- 鍵盤事件
- 效果
- 隱藏和顯示
- 淡入淡出
- 滑動
- 動畫
- 停止滑動
- jQuery Callback 方法
- jQuery Chaining
- jQuery_HTML
- jQuery獲取
- jQuery設置
- jQuery添加元素
- jQuery刪除元素
- jQuery CSS類
- jQuery css() 方法
- jQuery 遍歷
- jQuery AJAX
- jQuery AJAX簡介
- jQuery - AJAX load() 方法
- jQuery - AJAX get() 和 post() 方法