ホームページ  >  記事  >  ウェブフロントエンド  >  シンプルなjsアニメーションエフェクトコード_javascriptスキル

シンプルなjsアニメーションエフェクトコード_javascriptスキル

WBOY
WBOYオリジナル
2016-05-16 18:22:49957ブラウズ

実装された関数: 移動、一時停止、再開、停止。

未実装の関数: 単位変換ができない、色のグラデーションが実現できない (改善すべき点は他にもたくさんあります...)

コードでの使用方法は次のとおりです。

コードをコピーします コードは次のとおりです:

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

_("btnAnimate").onclick = function(){
this.disabled = true;
var that = this; >$m.animate( {left:"300px",top:"100px",width:"300px", height:"200px",opacity:0.3},{duration:5000,easing:Tween.Back.easeInOut}) .delay(200)
.animate({left:"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; = true;
_("btnResume").onclick = function(){
$m.resume();
this.disabled = true;
_("btnPause").disabled = false;
}

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



関数の実装:



コードをコピー
コードは次のとおりです。 /* 単純なアニメーション方法* 単位変換は実装されていません
*/
var $M = function(obj){
var elem = ("string" === obj のタイプ )?document.getElementById (obj):obj;
var _this = {},props = {},timeId,isBusy = false,isPause = false;
var queue = [],_current; ;
//線形運動アルゴリズム
function Linear(t,b,c,d){ return c*t/d b;}
function setCss(className,value){
if(className = = "不透明度"){
if (document.defaultView){
elem.style["不透明度"] = 値;
} else {
elem.style["filter"] = 'alpha (opacity=' 100 * 値 ')' ;
}
} else {
elem.style[className] = 値;
}
}
function getCss(className){
if(className == "opacity "){
var ret = "";
if(document.defaultView){
ret = document.defaultView.getComputedStyle(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;
for(var p in params){
if(!props[p]){
var iv = parseFloat(getCss(p)) || 0;
var ev = parseFloat(params[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 = イージング(t,props[p].iv,(props[p].ev - props[p].iv),ht );
nv = parseFloat(nv);
setCss( p,nv props[p].eu);

if(t < ht){
= setTimeout(function(){
_move(params,easing,st ,ht,callback);
},13);
props = {}; false;
if(callback){
callback();
run()
}
関数 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){
isBusy = true;
setTimeout() var st = (new Date()).getTime();
_move(o.params,o.options.easing,st,o.options.duration,o.options.callback); );
}
}
}

var _this = {
animate:function(params,options){
queue.push({params:params,options:オプション});
isPause = false;
return _this; });
return _this;
},
stop:function(){
isPause = true;
resume:function(){
if(_current){
var o = _current;
isPause = false;
var st = (new Date()).getTime() - _current.t; .params,o.options.easing,st,o.options.duration, o.options.callback);
return _this>}
},
stop:function(){
isPause = true;
isBusy = false;
return _this>}
return _this; 🎜>}


参考アドレス:

http://www.jb51.net/article/24309.htm
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。