>  기사  >  웹 프론트엔드  >  간단한 js 애니메이션 효과 code_javascript 기술

간단한 js 애니메이션 효과 code_javascript 기술

WBOY
WBOY원래의
2016-05-16 18:22:49958검색

구현된 기능: 이동, 일시정지, 재개, 중지

구현되지 않은 기능: 단위 변환 불가, 색상 그라데이션 불가능(개선할 사항이 더 많습니다...)

코드 의 사용방법은 다음과 같습니다.

코드를 복사하세요 코드는 다음과 같습니다.

var $m = $M ("divAnimate");

_("btnAnimate").onclick = function(){
this.disabled =
var that = this; >$m.animate({왼쪽:"300px",top:"100px",너비:"300px", 높이:"200px",opacity:0.3},{duration:5000,easing:Tween.Back.easeInOut}) .delay(200)
.animate({왼쪽:"50px",top:"300px",width:"150px",height:"100px",opacity:0.8},{duration:2000,easing:Tween. Elastic.easeInOut,
callback:function(){
that.disabled = false;
}
})
}
_("btnPause").onclick = function( ){
$ m.pause();
this.disabled = true;
_("btnResume").disabled = false
_("btnResume").disabled = true;
_("btnResume").onclick = function(){
$m.resume();
this.disabled =
_("btnPause").disabled = false;
}

_("btnStop").onclick = function(){
$m.stop()
_("btnAnimate").disabled = false; 🎜>}



함수 구현:



코드 복사 코드는 다음과 같습니다. /* 간단한 애니메이션 방식
* 단위 변환은 구현되지 않음
*/
var $M = function(obj){
var elem = ("string" === typeof obj )?document.getElementById (obj):obj;
var _this = {},props = {},timeId,isBusy = false,isPause = false; ;
//선형 모션 알고리즘
function Linear(t,b,c,d){ return c*t/d b;}
function setCss(className,value){
if(className = = "불투명도"){
if (document.defaultView){
elem.style["opacity"] = value
} else {
elem.style["filter"] = 'alpha (불투명도=' 100 * 값 ')' ;
}
} else {
elem.style[className] = value
}
}
function getCss(className)
if(className == "opacity "){
var ret = "";
if(document.defaultView){
ret = document.defaultView.getCompulatedStyle(elem,null)['opacity '] || 1;
} else {
ret = elem.currentStyle['filter']?(elem.currentStyle['filter'].match(/^alpha(opacity=([d.] ) )$/))[1]/100 : 1;
}
return ret.toString();
} else {
return elem.style[className].toString(); >}
}

function _move(params,easing,st,ht,callback){
var t = ((new Date()).getTime() - st); _current.t = t;
if (isPause){return;}
easing = easing||Linear;
ht = ht || 500
for(var p in params) >if(!props[p]){
var iv = parseFloat(getCss(p)) || 0
var ev =parms[p])
props[p] = {
iv:iv,
iu:iv?getCss(p).substring(iv.toString().length):null,
ev:ev,
eu:params[p].toString ().substring(ev.toString() .length)
}
//TODO (초기값의 단위와 목표값의 단위가 달라서 처리해야 함)
}
if(t >= ht){t = ht;}
var nv = easing(t,props[p].iv,(props[p].ev - props[p].iv),ht );
nv = parseFloat(nv);
setCss( p,nv props[p].eu)
}

if(t < ht){
timeId = setTimeout(function(){
_move(params,easing,st ,ht,callback);
},13)
} else {
props =
isBusy = false;
if(콜백){
콜백();
}
run()
}
}
function run(){
if(! isBusy && queue.length != 0){
var o = queue .shift();
var _delay = 0
while(o && o.delay){
_delay = o.delay ;
o = queue.shift();
}
if(o){
_current = o
isBusy =
setTimeout(){
var st = (new Date()).getTime();
_move(o.params,o.options.easing,st,o.options.duration,o.options.callback)
},_delay );
}
}
}

var _this = {
animate:function(params,options){
queue.push({params:params,options: options});
isPause = false;
run() ;
return _this,
delay:function(ms){
queue.push({delay:ms) });
return _this;
},
Pause:function(){
isPause =
return _this;
resume:function(){
if(_current){
var o = _current;
isPause = false
var st = (new Date()).getTime() -
_move(o) .params,o.options.easing,st,o.options.duration, o.options.callback)
return _this;
}
},
stop:function(){
isPause = true;
queue = [];
props = {};
}
return _this; 🎜>}


참고주소:

http://www.jb51.net/article/24309.htm
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.