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

从 HTML 中删除活动类,然后通过更改此行并调整 CSS 使用此代码初始设置它,以便在您的 JS 运行之前没有幻灯片可见:

if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

对此:

if ( $active.length == 0 ) {
    var slides = $('#slideshow DIV');
    $active = slides.eq(Math.floor(Math.random() * slides.length));
}

工作演示:http: //jsfiddle.net/jfriend00/mpYSL/

于 2013-05-23T00:44:59.303 回答
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 回答