开发者

Random Image by using s3slider

I am trying to display images randomly using this plugin I downloaded. (s3slider)

Here is the creators code

    (function($){  

    $.fn.s3Slider = function(vars) {       

        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4000;
        var current     = null;
        var timeOutFn   = null;
        var faderStat   = true;
        var mOver       = false;
        var items       = $("#" + element[0].id + "Content ." + element[0].id + "Image");
        var itemsSpan   = $("#" + element[0].id + "Content ." + element[0].id + "Image span");

        items.each(function(i) {

            $(items[i]).mouseover(function() {
               mOver = true;
            });

            $(items[i]).mouseout(function() {
                mOver   = false;
                fadeElement(true);
            });

        });

        var fadeElement = function(isMouseOut) {
            var thisTimeOut = (isMouseOut) ? (timeOut/2) : timeOut;
            thisTimeOut = (faderStat) ? 10 : thisTimeOut;
            if(items.length > 0) {
                timeOutFn = setTimeout(makeSlider, thisTimeOut);
            } else {
                console.log("Poof..");
            }
        }

        var makeSlider = function() {
            current = (current != null) ? current : items[(items.length-1)];
            var currNo      = jQuery.inArray(current, items) + 1
            currNo = (currNo == items.length) ? 0 : (currNo - 1);
            var newMargin   = $(element).width() * currNo;
            if(faderStat == true) {
                if(!mOver) {
                    $(items[currNo]).fadeIn((timeOut/6), function() {
                        if($(itemsSpan[currNo]).css('bottom') == 0) {
                            $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        } else {
                            $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                                faderStat = false;
                                current = items[currNo];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        }
                    });
                }
      开发者_StackOverflow中文版      } else {
                if(!mOver) {
                    if($(itemsSpan[currNo]).css('bottom') == 0) {
                        $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                            $(items[currNo]).fadeOut((timeOut/6), function() {
                                faderStat = true;
                                current = items[(currNo+1)];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    } else {
                        $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                        $(items[currNo]).fadeOut((timeOut/6), function() {
                                faderStat = true;
                                current = items[(currNo+1)];
                                if(!mOver) {
                                    fadeElement(false);
                                }
                            });
                        });
                    }
                }
            }
        }

        makeSlider();

    };  

})(jQuery);  

All I do when firing this js file is this code in my header....now how would I randomize all my images?

 <script type="text/javascript">
$(document).ready(function() {
   $('#s3slider').s3Slider({ 
      timeOut: 4000 
   });
});
</script>

Any and all help is appreciative.


Or for an even simpler way of doing it...

Replace line 50 of s3slider.js

current = (current != null) ? current : items[(items.length-1)];

With this:

current = (current != null) ? current : items[(Math.floor(Math.random()*(items.length)))];

Viola!


For random you can either output them randomly on the server side if the page is dynamic, or I use an array shuffle plugin.

(function ($) { $.shuffle = function (arr) { for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x); return arr; } })(jQuery);

Input like

var imageArr = ["image1.jpg", "image2.jpg", ...etc];
imageArr = $.shuffle(imageArr);

And then output the array into the s3slider

for (var i = 0; i < imageArr.length; i++) {
    $('#s3SliderContent').append("<li><img src='/images/" + imageArr[i] + "'></li>");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜