简单显示随机显示

时间:2011-01-21 16:27:13

标签: jquery random

我有5张照片(pic-1.jpg,pic-2.jpg ......等等)。这5张照片是加载到和数组中的文本与(标题)相关联我喜欢归档(用jquery或javascript)是:每次加载网站时随机获取订单,然后点击下一步,获取下一张图片,而不显示直到结束时显示的图片。

我认为使用随机生成第一个显示,并推动获取另一个

你有没有更好的功能...随机而不重复,直到显示一次...

是否清楚?...如果需要,我会解释更多!

5 个答案:

答案 0 :(得分:1)

通过搜索'javascript randomize array',我找到了一个关于fisheryates数组shuffle算法的页面: http://sedition.com/perl/javascript-fy.html

这是相关的代码:

function fisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

我做了一个使用它的例子: http://www.jsfiddle.net/fftWg/

答案 1 :(得分:0)

随机化jQuery对象,请看一下我之前给出的这个答案。也许它有其他帮助 - 不确定我是否真的明白你在追求什么;) - display galleria list of images in random order

答案 2 :(得分:0)

基本思想是每次进行“下一步”时都有一个随机数组并从其顶部弹出,直到你用完阵列,然后重复。

答案 3 :(得分:0)

从数组大小中获取随机数。每次显示图像时,都会从数组中删除该项目。数组为空后,重新开始。

答案 4 :(得分:0)

你的意思是这样的: 代码:

function RandomSlider()
{
    var self      = this;
    this.pictures = new Array();
    this.timeout  = 2000; // wait 2000 ms
    this.aniTime  = "fast" or 1500;

    // generate a random number
    this.randomStart = function()
    {
        return Math.floor(Math.random() * self.pictures + 1);
    }

    this.SetAnimate = function(arrIndex)
    {
        setTimeout(function()
        {
            // do animation or what you want
            $(....).animate({ .... }, self.aniTime, 'linear', function()
            {
                // do stuff when you are done with animation

                if(arrIndex == self..pictures.length) 
                {
                    // when the random index is the last item in your case image 5 and array index 4
                    // then start over again, start with array index 0
                    self.SetAnimate(0);
                }
                else
                {
                    // set the next slider item ready
                    self.SetAnimate(arrIndex + 1);
                }
            });
        }, self.timeout);
    }

    this.StartSlider = function()
    {
        // start animation
        this.SetAnimate( this.randomStart() );
    }
}

用法:

/

/ you can put in the array ID's classes attr urls all you want
    var picture_array = new Array();
    picture_array[0] = "image1";
    picture_array[1] = "image2";
    picture_array[2] = "image3";
    picture_array[3] = "image4";
    picture_array[4] = "image5";

    var slider = new RandomSlider();
    slider.pictures = picture_array;
    slider.StartSlider();