jQuery 效果- 動畫
animate() 方法
jQuery animate() 方法用於建立自訂動畫。
語法:
$(selector).animate({params},speed,callback);
必要的params 參數定義形成動畫的CSS 屬性。
可選的 speed 參數規定效果的長度。它可以取以下值:"slow"、"fast" 或毫秒。
可選的 callback 參數是動畫完成後所執行的函數名稱。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://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>向右移动</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>說明:
預設情況下,所有 HTML 元素都有一個靜態位置,且無法移動。
如需對位置操作,要記得先把元素的 CSS position 屬性設為 relative、fixed 或 absolute!
animate() - 操作多個屬性
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://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>逐渐变大</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 函式庫中。
animate() - 使用相對值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://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>
<br><br>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>animate() - 使用預先定義的值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://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>收起</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>animate() - 使用佇列功能
#預設地,jQuery 提供針對動畫的佇列功能。
這意味著如果您在彼此之後編寫多個 animate() 調用,jQuery 會建立包含這些方法調用的"內部"隊列。然後逐一運行這些 animate 調用。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://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'},"slow");
div.animate({width:'300px',opacity:'0.8'},"slow");
div.animate({height:'100px',opacity:'0.4'},"slow");
div.animate({width:'100px',opacity:'0.8'},"slow");
});
});
</script>
</head>
<body>
<button>开始动画</button>
<p></p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
新建檔案
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(php.cn)</title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("button:first").click(function() {
$("#block").animate({
left: "-=80px" //相对左移
}, 300);
});
$("button:last").click(function() {
$("#block").animate({
left: "+=80px" //相对右移
}, 300);
});
});
</script>
<style type="text/css">
div {
background-color: #FFFF00;
height: 40px;
width: 80px;
border: 1px solid #000000;
margin-top: 15px;
padding: 15px;
text-align: center;
position: absolute;
}
</style>
</head>
<body style="margin-left:200px;">
<button><--GO</button>
<button>Go--></button>
<div id="block">动画!</div>
</body>
</html>
預覽
Clear
- 課程推薦
- 課件下載
課件暫不提供下載,工作人員正在整理中,後期請多關注該課程~ 















