Javascript-форум (https://javascript.ru/forum/)
-   Ваши сайты и скрипты (https://javascript.ru/forum/project/)
-   -   Функция SmoothOpacity (https://javascript.ru/forum/project/28059-funkciya-smoothopacity.html)

yashka525 06.05.2012 05:37

Функция SmoothOpacity
 
Можете посоветовать как улучшить эту функцию? Ведь eval - нехорошо... или нормально?

Если что не понятно - могу комментарии поставить. Но вроде и так все понятно.

var IE = /*@cc_on!@*/0;

function SmoothOpacity(Element, StartFrom, EndOn, Step, Speed, OnComplete){

	IE?(Element.style.filter = 'alpha(opacity=' + StartFrom * 100 + ')'):(Element.style.opacity = StartFrom);
	
	(StartFrom < EndOn)?(
		Condition = 'EndOn > StartFrom'
	):(
		Condition = 'StartFrom > EndOn'
	)
	
	var NewCondition = Condition;
	
	(function(){
		if(eval(NewCondition)){
			StartFrom += Step;
			
			IE?(Element.style.filter = 'alpha(opacity=' + StartFrom * 100 + ')'):(Element.style.opacity = StartFrom);
			
			setTimeout(arguments.callee, Speed);
		}
		else{ //Если уже прозрачность достигла желаемой цифры
			IE?(Element.style.filter = 'alpha(opacity=' + EndOn * 100 + ')'):(Element.style.opacity = EndOn);

			if(OnComplete != 'null'){
				OnComplete(); //Можно выполнить какую-нибудь функцию... или не выполнять - 'null'
			}
		}
	})();
};

//Вызывать так:

SmoothOpacity(document.getElementById('box'), 0, 1, .05, 90, 'null');

SmoothOpacity(document.body, 1, 0, -.08, 200, function(){alert('Done!')});

devote 06.05.2012 07:34

<div>Hello people</div>
<script type="text/javascript">

var SmoothOpacity = (function() {

    var test = document.createElement( 'div' );
    test.innerHTML = "<a style='opacity:.55;'></a>";
    var a = test.getElementsByTagName( "a" )[ 0 ],
        supportOpacity = /^0.55/.test( a.style.opacity );

    return function( element, start, end, step, speed, onComplete ) {

        var rev = start > end ? true : false,

        interval = setInterval( function(){

            if ( rev && start <= end || !rev && start >= end ) {
                start = end;
                clearInterval( interval );
            }

            if ( supportOpacity ) {
                element.style.opacity = start;
            } else {
                element.style.filter = 'alpha(opacity=' + start * 100 + ')';
            }

            onComplete && start == end && onComplete();

            start += rev ? -step : step;

        }, speed );
    }
})();

SmoothOpacity(document.body, 1, 0, .08, 100, function(){
    SmoothOpacity(document.body, 0, 1, .08, 100, function(){alert('Done!')});
});

</script>


Часовой пояс GMT +3, время: 00:43.