我有一个图像滑块,我将它们按随机顺序排列,但我意识到其中一个图像不断重复。有什么方法可以防止使用 Math.random() 重复图像吗?
这是我到目前为止所做的:http: //jsfiddle.net/Y4jr6/
<script type="text/javascript" language="javascript">
//switch the landing page background images
function slideSwitch() {
//set variable active
var $active = $('#slideshow IMG.active');
//check if attribute active exists
if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
// use this to pull the images in the order they appear in the markup
var $next = $active.next().length ? $active.next()
: $('#slideshow IMG:first');
//throw images in random order
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()", 7000 );
});
</script>
任何帮助将非常感激!!