我已经基于this基于jquery的幻灯片显示并运行得很好,作者甚至提出了我需要的随机化设置。除了之外,它不会使第一张幻灯片随机化。有任何想法吗? 感谢:)
这是js :(或者转到original page获取更多信息)
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>
答案 0 :(得分:1)
看起来它只是随机化活动幻灯片的兄弟,而不包括当前活动幻灯片,这是加载页面时的第一个。您可以修复插件的代码,或者像我一样懒惰,并在初始化幻灯片之前随机化这些图像。使用类似答案中的插件:Randomize a sequence of div elements with jQuery
答案 1 :(得分:0)
尝试使用以下内容进行随机化:
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 );
});