我有一個圖像滑塊,我把它們放入隨機的輸出順序,但我意識到,其中一個圖像不斷重複。有什麼辦法來防止使用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