随机显示随机图像jquery

时间:2011-05-19 07:15:14

标签: jquery image-rotation

我正试图在div中的随机区域显示随机图像。我正在寻找的是接近this site。它们显示淡入淡出的图像和框。

我已经找到了类似这样的jQuery插件,但我找不到任何东西。你们对此有什么想法吗?

4 个答案:

答案 0 :(得分:0)

以下函数将在container选择器中添加图像并返回其id。

function rnd(max) { return Math.floor(Math.random()*(max+1)) }

function showImage(container, maxwidth, maxheight, imgsrc, imgwidth, imgheight) {
    var id = "newimage" + rnd(1000000);
    $(container).append(
        "<img id='" + id + "' src='" + imgsrc + 
        "' style='display:block; float:left; position:absolute;" + 
        "left:" + rnd(maxwidth - imgwidth) + "px;" +
        "top:"  + rnd(maxheight - imgheight) + "px'>");
    $('#' + id).fadeIn();
    return id;
}

假设您有10张名为image0.jpgimage10.jpg的图片,它们的宽度/高度相同。

然后您可以每隔10秒调用此函数,如下所示:

setInterval(function() {
    showImage("#container", 300, 300, "image" + rnd(10) + ".jpg", 100, 100);
}, 10000);

容器应该是这样的:

<div id='container' style='width:300px; height:300px; position:relative'>

我制作了一个样本与placekitten.com一起使用,如果更改尺寸,则会生成不同的图像。就在这里:http://jsfiddle.net/G5Xrz/

答案 1 :(得分:0)

使用数字符号为每个div指定一个不同的类名。比如'random1','random2'等等,然后使用css设置样式,颜色和绝对位置。

然后使用jquery循环div并按随机顺序给它们。

$(document).ready(function() {
    var length = $("div").length;
    $('div').each(function(index) {
        $(this).addClass('random'+(Math.floor(Math.random()*length) + 1));
    });
});

答案 2 :(得分:0)

原谅这个长度,随着我更多地考虑OP的意图而不是他的问题的文本,这种情况越来越多。这里需要的是工厂,而不是真正的随机列表。通常情况下,我会把它作为一个课程,但是我已经让那些不那么倾向的人更易于阅读。

此外,我会预先定义图像的“着陆点”,因为在算法上构建它只是要求一个糟糕的体验和麻烦...因此我的建议:

$(document).ready(function(){
  function rndsort(a,b)
  {
     return (Math.random() > 0.5) ? a : b;
  }
  function getImage()
  {
      if (imgs.length > 0)
      {
         return imgs.shift();
      }
  }
  function switchImage(container)
  {
     var newImage = getImage();
     if (newImage)
     {
        if ($(container).data('img'))
        {
           imgs.push($(container.data('img'));
        }
        $(container).html('<img src="'+getImage()+'" />');
     }
  }

  function doRandom()
  {
     switchImage($('.places').get(Math.floor(Math.random() * $('.places'.length)));
  }

  var imgs = ['image1.jpg','image2.jpg', etc... ];

  imgs.sort(rndsort);

  $('.places').each(function(idx, el){
    switchImage(el);
  });
  // add a timer and start calling 'doRandom()';
});

答案 3 :(得分:0)

如果您真的想要像链接网站这样的效果,那么您并不是真的想要随机添加随机图像。这些位置是硬编码的,每个位置都从适当大小的图像子集中随机选择。

var tS;
var tL;
var imgSmall = ["1.jpg", "2.png", "3.png", "10.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg"];
var smallLen = imgSmall.length;
var imgLarge = ["A1.jpg", "A2.jpg", "A3.jpg", "A10.jpg", "A4.bmp", "A5.jpg", "A6.jpg", "A7.jpg", "A8.jpg", "A9.jpg"];
var largeLen = imgLarge.length;

function showLarge() {
    var idxLarge = Math.floor(Math.random() * largeLen);
    $("#large").fadeToggle(300, function() { $("#large").attr("src", "img/" + imgLarge[idxLarge]) }).fadeToggle(300);
    tL = setTimeout("showLarge()", 2000);
}

function showSmall() {
    var idxSmall = Math.floor(Math.random() * smallLen);
    $("#small").fadeToggle(300, function() { $("#small").attr("src", "img/" + imgSmall[idxSmall]) }).fadeToggle(300);
    tS = setTimeout("showSmall()", 1700);
}

$(document).ready(function() {
    showLarge();
    showSmall();
});

在HTML中,您只需要几个图像位置。

<img src="img/1.jpg" id="small" style="position:absolute; top:50px; left:100px;" />

<img src="img/A8.jpg" id="large" style="position:absolute; top:100px; left:400px;" />

这种方法的优点是您可以使用彩色div和all来完全按照自己的意愿设计布局。您还可以使用该代码的变体为彩色div的颜色设置动画。

相关问题