有点奇怪的要求.我有~300个图像URL和27 div秒.每个div都有一个id="img1",其中1一个介于1和(你猜对了)27之间的数字,和一个class="imageblock"(一般造型).
进入这些divs中的每一个,我想插入一个<img src="http://foo.bar/img.jpg" width="640">,其中URL从大约300个随机列表中选择,并随机插入其中一个div.27 div秒中的每一个都应该从300个选项中选择自己的图像.
此外,这些图像应循环.每5秒钟,我想在页面上有一个随机图片网址替换为另一个随机图片网址.
这是我尝试用一点帮助拼凑起来的(可怕的)代码,但是我对javascript没用,并且不能使这个工作:
<script type="text/javascript">
$('document').ready(function() {
var divs = $('div');
$("div").each(function() {
$(this).html("<img src='http://foo.bar/img1.jpg'alt='"+$(this).attr("id")+"'/>");
});
var imageChanger = setInterval(function() {switchImage()}, 500);
function switchImage() {
var new_image_url = [ 'http://foo.bar/anotherimg.jpg', 'http://bar.foo/image.jpg', 'http://foo.bar/imgremote.jpg'];
var random_div = Math.floor(Math.random() * $('div').length);
var random_img = Math.floor(Math.random() * $(new_image_url.length);
$("div:nth-child("+random_div+")").html('<img src="('+random_img+')" width="640">)');
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
请注意,远程图像不遵循任何常见模式,从而使我的random_img变量不起作用.
任何帮助,将不胜感激.