1

このスライドショー スクリプトをセットアップして、表示される最初の写真がランダムになるようにしようとしています (サイトにアクセスするたびに、最初の写真を異なる/ランダムなスライドにする必要があります)。残りの写真は正しい順序で表示できます。それはいいです。

Jon Raasch のシンプルな jquery スライドショー スクリプトを使用しています。

<script type="text/javascript">
function slideSwitch() {
    var $active = $('#slideshow DIV.active');
    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
$(function() {
    setInterval( "slideSwitch()", 2500 );
});
</script>

<div id="slideshow">
<div class="active"><img src="images/slide1.jpg"></div>
<div><img src="images/slide2.jpg"></div>
<div><img src="images/slide3.jpg"></div>
<div><img src="images/slide4.jpg"></div>
<div><img src="images/slide5.jpg"></div>
</div>

そしてCSS:

#slideshow {
position:relative;
height:401px;
}
#slideshow div {
position:absolute;
top:0;
left:0;
z-index:8;
opacity:0.0;
}
#slideshow div.active {
z-index:10;
opacity:1.0;
}
#slideshow div.last-active {
z-index:9;
}

私はいくつかのことを試しましたが、javascript は本当に私のお茶ではなく (ここでは「デザイナーの心」)、私が試したことがうまくいきません。何か案は?

本当にありがとう!

4

3 に答える 3

1

jfriend00 の回答に基づいて、スクリプトを次のように設定する必要があります。

 function slideSwitch() {
    var $active = $('#slideshow div.active');
    if ( $active.length == 0 ) {
    var slides = $('#slideshow div');
    $active = slides.eq(Math.floor(Math.random() * slides.length));
}
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow div:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
$(function() {
    slideSwitch();
    setInterval( function(){slideSwitch()}, 2500 );
});

いくつかの構文エラーがあり、setInterval の呼び出し方法を変更する必要がありました。

作業フィドル: http://jsfiddle.net/f2UPm/

于 2013-05-23T07:58:02.053 に答える
0

このようなもの:

$(function() {
    var slides, index, current, last, last_index;
    slides = $('#slideshow div');
    // initialize index randomly
    index = Math.floor(Math.rand() * slides.length);
    // do a % trick to get index - 1 looping
    last_index = (index + slides.length - 1) % slides.length;
    last = slices[last_index]; // initialize last

    function slideSwitch() {
        current = slides[index];
        index = (index + 1) % slides.length; // % to loop
        current.addClass("active");
        last.addClass("last-active");
        current.css({ "opacity": 0.0 })
            .animate({ "opacity": 1.0}, 1000, function() {
                current.removeClass("active");
                last.addClass("active");
                last.removeClass("last-active");
                last = current;
            });
    }
    setInterval(slideSwitch, 2500);
    slideShow(); // run once now, because interval doesn't run until 2500ms
});
于 2013-05-23T00:43:18.720 に答える