指定秒後に、フェードアウトする その2?
var Fadeout = function ( o ) { this.obj = o; this.decrease(); this.obj.timerId = setTimeout( (function ( that ) { return function ( ) { that.decrease(1); }; } )( this ), this.obj.wait ); }; Fadeout.prototype.decrease = function ( sw ) { var op = this.obj.opacity, f; if( op < 0 ) f = 1, op = 0; if( op > 100 ) f = 1, op = 100; if( f ) { clearInterval( this.obj.timerId ); this.obj.endFunc && this.obj.endFunc.call( this ); return; } this.obj.element.style./*@if (@_jscript) filter = 'alpha(opacity='+ op+ ')' @else@*/ opacity = op / 100 /*@end@*/; this.obj.opacity += this.obj.step; if( sw ) { this.obj.timerId = setInterval( (function (that) { return function () { that.decrease(); }; } )( this ) , this.obj.time); } }; Fadeout.buffer = [ ]; Fadeout.add = function ( e, wait, step, time, endFunc ) { var i, o, f = false; for( i = 0; o = this.buffer[i++]; ) { if( o.element == e ) { clearTimeout( o.timerId ); clearInterval( o.timerId ); f = true; break; } } o = { 'element': e, 'wait': wait, 'step': step, 'time': time, 'opacity': 100, 'timerId': null, 'endFunc': endFunc }; f || this.buffer.push( o ); new Fadeout( o ); };
使い方
Fadeout.add( document.getElementById('xxx', 3000, -5, 30);// id, 待ち時間, 変化量, 変化時間
bufferは、同じ要素に指定がされた場合、キャンセルして新しく上書きするため