繁体   English   中英

页面刷新时如何随机插入图像横幅?

[英]how to interchage image banners randomly when page refresh?

这是我的横幅:

在此处输入图片说明

请帮助我。 我希望页面加载时,每个横幅以任何顺序交换位置。

我为此使用apache速度,但我只想知道如何编码它的逻辑。 谢谢

如果横幅是图像,则可以使用所有图像创建一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM