0

javascriptを使用して単純な画像ローテーターを作成しましたが、「古いブラウザー」(IE 6、7、8など)では低速です。画像が最初に読み込まれ、次にローテーターが起動します。高速化するためのヒントはありますか?

なぜ自分でローテーターを作ったのですか?私が見つけたすべてのローテーターは画像をカット(トリミング)しました。完全な画像が必要です...必要に応じて、左/右または上/下に空白を入れます。

Javascriptの部分:

//Loop through pictures
var tid = setInterval(mycode, 3000);
function mycode() {

    if($.random(0,1) == 1){
        //Fade
        $('#alleplaatjes img.active').fadeOut(500,function(){
            $(this).removeClass();

            if($(this).next().length == 0){
                $('#alleplaatjes img').first().fadeIn(500);
                $('#alleplaatjes img').first().addClass('active');
            } else {
                $(this).next().fadeIn(500);
                $(this).next().addClass('active');
            }
        });
    } else {
        //Slide
        $('#alleplaatjes img.active').slideUp(500,function(){
            $(this).removeClass();

            if($(this).next().length == 0){
                $('#alleplaatjes img').first().slideDown(500);
                $('#alleplaatjes img').first().addClass('active');
            } else {
                $(this).next().slideDown(500);
                $(this).next().addClass('active');
            }
        });
    }
};

PHPの部分:

<?php

$dir = "/home/zwuq/domains/***/public_html/afbeelding/";
foreach(glob($dir."*") as $file){
    $afbeelding = 'afbeelding/'.str_replace($dir, '', $file);
    $toevoeging = FALSE;
    if(!$eerste_plaatje){
        $toevoeging = ' class="active"';
        $eerste_plaatje = $afbeelding;
    }
    $plaatjes .= '<img'.$toevoeging.' src="'.$afbeelding.'" style="max-width: 99%; max-height: 99%;">';
}

?>

HTML部分:

<div id="alleplaatjes" style="width:100%; height:100%; margin:0px; padding:0px; z-index:1; text-align: center;">
    <? echo $plaatjes; ?>
</div>
4

1 に答える 1

1

1つの提案は、を使用しないことsetIntervalです。操作に時間がかかりすぎる場合(この場合は3秒以上)、累積遅延によりアニメーションがさらに悪化します。

私の提案を試すには、setTimeoutの代わりに呼び出すだけsetIntervalで、最後にmycodesetTimeoutをもう一度呼び出します。理想的には、関数が呼び出された遅延を追跡し、次のタイムアウトまでに渡される間隔を調整します。

StackOverflowで最良の結果を得るには、http://jsfiddle.netに例を投稿すると、他の人が問題をライブで確認できるようになり、私たちがあなたを助けることができるかもしれません。

別の提案

jQueryオブジェクトをキャッシュします。たとえば、次の代わりに:

    $(this).removeClass();
    if($(this).next().length == 0){
        $('#alleplaatjes img').first().fadeIn(500);
        $('#alleplaatjes img').first().addClass('active');
    } else {
        $(this).next().fadeIn(500);
        $(this).next().addClass('active');
    }

あなたが持っている必要があります

    // prepending jquery variables with $ to distinguish them
    var $this = $(this),
        $next = $this.next();
    $this.removeClass();

    if( $next.length == 0 ){
        var $first = $('#alleplaatjes img').first();
        $first.fadeIn(500);
        $first.addClass('active');
    } else {
        $next.fadeIn(500);
        $next.addClass('active');
    }
于 2011-12-20T18:36:37.883 に答える