随机化幻灯片上的第一张图片

Randomizing the first image on this slideshow?

本文关键字:一张 幻灯片 随机化      更新时间:2023-09-26

我已经得到了这个基于jquery的幻灯片启动并运行得很好,作者甚至放入了一个随机化设置,这是我需要的。它工作得很好,除了它不会随机化第一张幻灯片。什么好主意吗?谢谢:)

这是js:(或到原始页面查看更多信息)

function slideSwitch() {
    var $active = $('#slideshow DIV.active');
    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
    // use this to pull the divs in the order they appear in the markup
   // var $next =  $active.next().length ? $active.next()
    //    : $('#slideshow DIV:first');
    // uncomment below to pull the divs randomly
var $sibs  = $active.siblings();
var rndNum = Math.floor(Math.random() * $sibs.length );
var $next  = $( $sibs[ rndNum ] );

    $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()", 20000 );
});
HTML:

<div id="slideshow">
    <div class="active">
        <img src="img/img1.jpg" />
    </div>
    <div>
        <img src="img/img2.jpg" />
    </div>
    <div>
        <img src="img/img3.jpg" />
    </div>
</div>

看起来它只随机化活动幻灯片的兄弟幻灯片,不包括当前活动幻灯片,这是加载页面时的第一个。你可以修复插件的代码,或者像我一样懒惰,在初始化幻灯片之前随机化这些图像。使用jQuery随机化div元素序列

尝试随机化:

 function slideSwitch() {
        var $active = $('#slideshow div').eq(Math.floor(Math.random() * $('#slideshow div').length);
        $(".active").removeClass("active");
        $active.addClass("active");
        if ( $active.length == 0 ) $active = $('#slideshow DIV:last');
        // use this to pull the divs in the order they appear in the markup
       // var $next =  $active.next().length ? $active.next()
        //    : $('#slideshow DIV:first');
        // uncomment below to pull the divs randomly
    var $sibs  = $active.siblings();
    var rndNum = Math.floor(Math.random() * $sibs.length );
    var $next  = $( $sibs[ rndNum ] );

        $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()", 20000 );
    });