我有一个图像滑块,我把它们放入随机的输出顺序,但我意识到,其中一个图像不断重复。有什么办法来防止使用Math.random()重复图像?随机图像生成器,但从来没有重复相同的图像 - Javascript
这是我迄今所做的: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>
任何帮助将不胜感激!
你有可能向我展示一个例子吗?因为我没有线索甚至接近 – Elmer
我希望我可以帮助更多,但我的JavaScript/jQuery知识并不是那么棒。尝试在http://jsfiddle.net/上创建一个完整的示例并编辑您的原始问题,然后其他人可以提供帮助。 –
感谢您的帮助! :) – Elmer