[英]how to interchage image banners randomly when page refresh?
如果横幅是图像,则可以使用所有图像创建一个JS数组,将其随机化 ,然后更改img
源。
举个例子 :
<img id="banner1" src="" />
<img id="banner2" src="" />
<img id="banner3" src="" />
<script>
var images = ["http://exemple.com/banner1", "http://exemple.com/banner2", "http://exemple.com/banner3"];
images = shuffle(images);
$('#banner1').attr('src', images[0]);
$('#banner2').attr('src', images[1]);
$('#banner3').attr('src', images[2]);
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.