【问题标题】:Simple display random show简单展示随机展示
【发布时间】:2011-06-13 06:27:06
【问题描述】:

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

我想用随机来生成第一个显示,并推动获得另一个

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

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

【问题讨论】:

  • 我已经评估了 pop(),但它可以附加人们将旋转 5 张图像,并且必须再次显示第一个... POP 不好

标签: jquery random


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        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();
        

        【讨论】:

          【解决方案5】:

          通过搜索“javascript randomize array”,我找到了一个关于 FisherYates 数组洗牌算法的页面: 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/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-06-23
            • 2011-09-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-20
            • 1970-01-01
            相关资源
            最近更新 更多