TweenMax
一個(gè)專業(yè)的動(dòng)畫js庫
TweeMax使用
得到動(dòng)畫的實(shí)例
var t = new TimelineMax();
to()
作用: 添加動(dòng)畫
參數(shù)說明:
- 元素選擇器(字符串)或?qū)ο?/li>
- 持續(xù)時(shí)間(數(shù)字), 單位秒
- 變化的屬性(對(duì)象)
- [可選] 動(dòng)畫延遲發(fā)生時(shí)間
- 數(shù)字
- "-=0.5"
- "+=0.5"
基本使用:
<style>
#div1{
height: 100px;
width: 100px;
background: red;
}
</style>
<div id="div1"></div>
<script>
var t = new TimelineMax();
t.to( '#div1', 1, { width: 200 } );
</script>
這里添加了動(dòng)畫就開始運(yùn)行
添加多個(gè)動(dòng)畫
t.to( '#div1', 1, { width: 300 } );
t.to( '#div1', 1, { marginLeft: 300 } );
t.to( '#div1', 1, { height: 300 } );
t.to( '#div1', 1, { marginTop: 300 } );
t.to( '#div1', 1, { rotationZ: 180 } );//TweenMax特有屬性
t.to( '#div1', 1, { opacity: 0 } );
動(dòng)畫依次執(zhí)行
加上第四個(gè)參數(shù):
t.to( '#div1', 1, { width: 300 }, 1 );
t.to( '#div1', 1, { marginLeft: 300 }, 1 );
當(dāng)?shù)谒膫€(gè)參數(shù)為數(shù)字時(shí),表示延遲時(shí)間, 但是不會(huì)等待之前的運(yùn)動(dòng)了, marginLeft會(huì)和width延遲一秒后一起改變
t.to( '#div1', 1, { width: 300 } );
t.to( '#div1', 1, { marginLeft: 300 }, 0 );
marginLeft也會(huì)和width一起運(yùn)動(dòng), 0表示沒有延遲,并且第四個(gè)參數(shù)為數(shù)字不會(huì)等待先前的運(yùn)動(dòng)
t.to( '#div1', 1, { width: 300 } );
t.to( '#div1', 1, { marginLeft: 300 }, '+=0.5' );
如果是這'+=0.5'這種字符串形式, 表示在前一個(gè)運(yùn)動(dòng)的基礎(chǔ)上再延遲0.5秒
t.to( '#div1', 1, { width: 300 } );
t.to( '#div1', 1, { marginLeft: 300 }, '-=0.5' );
如果是這'-=0.5'這種字符串形式, 表示在前一個(gè)運(yùn)動(dòng)的基礎(chǔ)上再提前0.5秒
當(dāng)width運(yùn)動(dòng)到200時(shí), marginLeft就開始運(yùn)動(dòng)
stop():停止動(dòng)畫 play():開始動(dòng)畫 reverse():反向開始動(dòng)畫
<button id="play">play</button>
<button id="stop">stop</button>
<button id="reverse">reverse</button>
<div id="div1"></div>
<script>
var t = new TimelineMax();
t.stop();//開始的時(shí)候,先停止動(dòng)畫
t.to( '#div1', 1, { width: 300 } );
t.to( '#div1', 1, { marginLeft: 300 } );
t.to( '#div1', 1, { height: 300 } );
t.to( '#div1', 1, { marginTop: 300 } );
t.to( '#div1', 1, { rotationZ: 180 } );
t.to( '#div1', 1, { opacity: 0 } );
$('#stop').click(function(){
t.stop();
})
$('#play').click(function(){
t.play();
})
$('#reverse').click(function(){
t.reverse();
})
</script>
onComplete():運(yùn)動(dòng)結(jié)束后觸發(fā)對(duì)應(yīng)的函數(shù) onReverseComplete():反向運(yùn)動(dòng)結(jié)束后觸發(fā)對(duì)應(yīng)的函數(shù)
t.to( '#div1', 1,
{
width: 300,
onComplete: function(){
alert(1);
} ,
onReverseComplete: function(){
alert(2);
}
});
add() tweenTo()
add() 添加狀態(tài)
參數(shù): 狀態(tài)名(字符串)
或一個(gè)函數(shù)
tweenTo() 完成指定的動(dòng)畫
參數(shù):
狀態(tài)的字符串
或數(shù)字
add() tweenTo()配合使用
var t = new TimelineMax();
t.add('state1');//添加狀態(tài)
t.to( '#div1', 1, { width: 200 } );
t.add('state2');
t.to( '#div1', 1, { width: 200 } );
t.add('state3');
t.tweenTo('state2');//運(yùn)動(dòng)到指定狀態(tài),這里是運(yùn)動(dòng)到200就停止了
add()傳入函數(shù)
var t = new TimelineMax();
t.add('state1');
t.to( '#div1', 1, { width: 200 } );
//添加函數(shù), width到200就執(zhí)行該函數(shù)
t.add(function(){
$('#div').css('background','blue');
});
t.add('state2');
t.to( '#div1', 1, { width: 200 } );
t.add('state3');
t.tweenTo('state2');
tweenTo()也可以傳入數(shù)字,表示運(yùn)動(dòng)到指定時(shí)間
t.to( '#div1', 1, { width: 200 } );
t.tweenTo(0.5);
seek()
完成指定的動(dòng)畫(無過渡)
seek跟tweenTo作用類似,區(qū)別在于seek沒有過渡效果,是瞬間完成
參數(shù)說明:
- 指定時(shí)間或狀態(tài)
- [可選]
- true 不執(zhí)行onComplete函數(shù) 默認(rèn)
- false 執(zhí)行onComplete函數(shù)
t.to( '#div1', 1, { width: 200, onComplete: function(){ alert(1) }} );
t.stop();
t.seek(1, false);
time()
返回動(dòng)畫已執(zhí)行的時(shí)間
function getTime(){
alert( t.time() );
}
var t = new TimelineMax();
t.to( '#div1', 1, { width: 300 , onComplete: getTime}, '+=1' );
t.to( '#div1', 1, { marginLeft: 300 , onComplete: getTime} );
t.to( '#div1', 1, { height: 300 , onComplete: getTime} );
t.to( '#div1', 1, { marginTop: 300 , onComplete: getTime} );
t.to( '#div1', 1, { rotationZ: 180 , onComplete: getTime} );
t.to( '#div1', 1, { opacity: 0 , onComplete: getTime} );
clear()
清除所有動(dòng)畫
staggerTo()
添加動(dòng)畫
跟to()方法的效果基本一致,除了第四個(gè)參數(shù)的效果
參數(shù)說明:
- 元素選擇器或?qū)ο?/li>
- 持續(xù)時(shí)間
- 對(duì)象(變化的屬性)
- 【可選】動(dòng)畫延遲發(fā)生時(shí)間
- 可寫數(shù)字,“-=0.5”,“+=0.5“
<style>
.div1{
height: 100px;
width: 100px;
margin: 1px;
background: red;
}
</style>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<script>
var t = new TimelineMax();
//t.to( '.div1', 1, { width: 300}, 1 );//同時(shí)執(zhí)行
t.staggerTo( '.div1', 1, { width: 300}, 1 );//依次執(zhí)行
</script>
totalDuration()
獲取動(dòng)畫的總時(shí)長
alert( t.totalDuration() );
getLabelTime()
返回從開始到傳入的狀態(tài)的時(shí)間
參數(shù)說明:
- 狀態(tài)的字符串
返回值是一個(gè)數(shù)字
alert( t.getLabelTime('state2') )
currentLabel()
獲取當(dāng)前狀態(tài)
返回值是狀態(tài)的字符串
getLabelAfter()
獲取下一個(gè)狀態(tài)
參數(shù)說明:
- 時(shí)間數(shù)字
返回值是狀態(tài)的字符串,如果沒有下一個(gè)狀態(tài)返回null
getLabelBefore()
獲取上一個(gè)狀態(tài)
function getCurrentLabel(){
//獲取當(dāng)前的時(shí)間
var currentTime = t.getLabelTime( t.currentLabel() );
//獲取到上一個(gè)狀態(tài)
var beforeLabel = t.getLabelBefore( currentTime );
//獲取到下一個(gè)狀態(tài)
var afterLabel = t.getLabelAfter( currentTime );
var str = "<p>上一個(gè)狀態(tài):"+ beforeLabel +"</p><p>當(dāng)前狀態(tài):"+ t.currentLabel() +"</p><p>下一個(gè)狀態(tài):"+ afterLabel +"</p>";
$("#label").html( str );
}
狀態(tài)可以應(yīng)該是一段時(shí)間,而不是一個(gè)時(shí)間點(diǎn)
t.add('state1');
t.to();
t.add('state2');
|___state1_____|___state2_____|
ease
t.to( '#div1', 1, { width: 300, ease:Bounce.easeIn } );