我正在使用Twitter Bootstrap的轮播脚本来制作徽标炫耀旋转木马。这些徽标应该是随机排序的,每次用户刷新页面时都要随机播放
我在谷歌搜索了很多找到了一些解决方案,但没有一个能为我工作,我不知道会发生什么......无论如何:
<div id="carousel-example-generic"
class="carousel slide"
data-ride="carousel"
style="width: 180px; margin: 0 auto">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<a href="#"><img src="http://i.imgur.com/jSfZIJK.png" alt="Vale"></a>
</div>
<div class="item">
<a href="#"><img src="http://i.imgur.com/z2FyBAt.png" alt="Petrobras"></a>
</div>
<div class="item">
<a href="#"><img src="http://i.imgur.com/mZtv0bA.png" alt="IGAM"></a>
</div>
<div class="item">
<a href="#"><img src="http://i.imgur.com/tqgQxbq.png" alt="Arcelor Mittal"></a>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
$('.carousel').carousel({
interval: 1000
})
});
</script>
答案 0 :(得分:5)
您可以在加载轮播之前随机播放图像数组并构建div
...
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" style="width: 180px; margin: 0 auto">
<!-- Wrapper for slides -->
<div class="carousel-inner"></div>
</div>
jQuery(document).ready(function ($) {
var images = [{
url: "http://i.imgur.com/jSfZIJK.png",
alt: "Vale"
}, {
url: "http://i.imgur.com/z2FyBAt.png",
alt: "Petrobras"
}, {
url: "http://i.imgur.com/mZtv0bA.png",
alt: "IGAM"
}, {
url: "http://i.imgur.com/tqgQxbq.png",
alt: "Arcelor Mittal"
}];
// http://css-tricks.com/snippets/javascript/shuffle-array/
images.sort(function () { return 0.5 - Math.random(); });
$.each(images, function (index, image) {
var element = $('<div class="item"><a href="#"><img src="' + image.url + '" alt="' + image.alt + '" /></a></div>')
if (index === 0) {
element.addClass("active");
}
element.appendTo("div.carousel-inner");
});
$('.carousel').carousel({
interval: 1000,
})
});