Trying to randomise a jQuery content slider

  • ace5p1d0r
  • Expert
  • Expert
  • User avatar
  • Posts: 630
  • Loc: UK

Post 3+ Months Ago

Hi everyone,

I'm using a very nice jQuery content slider called Easy Slider on my site that I downloaded from Css Globe.

The script is excellent and does just what I want - except I can't make it randomise the list, it always scrolls from left to right or right to left!

I'm far from good with JavaScript, so my attempts at solving this have been feeble. Although I'm sure it must be an easy fix!

If anyone wouldn't mind taking a glance over the script to see if they can spot what I need to change to make it random it would be greatly appreciated!

I've tried contacting the original plugin developer but have had no response yet. The comments on the Easy Slider page didn't bear much fruit either unfortunately.

I've pasted the script I'm using on my site below:

Code: [ Select ]
/*
*     Easy Slider 1.7 - jQuery plugin
*    written by Alen Grakalic    
*    http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
*
*    Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
*    Dual licensed under the MIT (MIT-LICENSE.txt)
*    and GPL (GPL-LICENSE.txt) licenses.
*
*    Built for jQuery library
*    http://jquery.com
*
*/

/*
*    markup example for $("#slider").easySlider();
*    
*     <div id="slider">
*        <ul>
*            <li><img src="images/01.jpg" alt="" /></li>
*            <li><img src="images/02.jpg" alt="" /></li>
*            <li><img src="images/03.jpg" alt="" /></li>
*            <li><img src="images/04.jpg" alt="" /></li>
*            <li><img src="images/05.jpg" alt="" /></li>
*        </ul>
*    </div>
*
*/

(function($) {

    $.fn.easySlider = function(options){
     
        // default configuration properties
        var defaults = {            
            prevId:         'prevBtn',
            prevText:         'Previous',
            nextId:         'nextBtn',    
            nextText:         'Next',
            controlsShow:    true,
            controlsBefore:    '',
            controlsAfter:    '',    
            controlsFade:    true,
            firstId:         'firstBtn',
            firstText:         'First',
            firstShow:        false,
            lastId:         'lastBtn',    
            lastText:         'Last',
            lastShow:        false,                
            vertical:        false,
            speed:             800,
            auto:            false,
            pause:            7000,
            continuous:        false,
            numeric:         false,
            numericId:         'controls'
        };
        
        var options = $.extend(defaults, options); 
                
        this.each(function() { 
            var obj = $(this);                 
            var s = $("li", obj).length;
            var w = $("li", obj).width();
            var h = $("li", obj).height();
            var clickable = true;
            obj.width(w);
            obj.height(h);
            obj.css("overflow","hidden");
            var ts = s-1;
            var t = 0;
            $("ul", obj).css('width',s*w);            
            
            if(options.continuous){
                $("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
                $("ul", obj).append($("ul li:nth-child(2)", obj).clone());
                $("ul", obj).css('width',(s+1)*w);
            };                
            
            if(!options.vertical) $("li", obj).css('float','left');
                                
            if(options.controlsShow){
                var html = options.controlsBefore;                
                if(options.numeric){
                    html += '<ol id="'+ options.numericId +'"></ol>';
                } else {
                    if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
                    html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
                    html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
                    if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';                
                };
                
                html += options.controlsAfter;                        
                $(obj).after(html);                                        
            };
            
            if(options.numeric){                                    
                for(var i=0;i<s;i++){                        
                    $(document.createElement("li"))
                        .attr('id',options.numericId + (i+1))
                        .html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
                        .appendTo($("#"+ options.numericId))
                        .click(function(){                            
                            animate($("a",$(this)).attr('rel'),true);
                        });                                                 
                };                            
            } else {
                $("a","#"+options.nextId).click(function(){        
                    animate("next",true);
                });
                $("a","#"+options.prevId).click(function(){        
                    animate("prev",true);                
                });    
                $("a","#"+options.firstId).click(function(){        
                    animate("first",true);
                });                
                $("a","#"+options.lastId).click(function(){        
                    animate("last",true);                
                });                
            };
            
            function setCurrent(i){
                i = parseInt(i)+1;
                $("li", "#" + options.numericId).removeClass("current");
                $("li#" + options.numericId + i).addClass("current");
            };
            
            function adjust(){
                if(t>ts) t=0;        
                if(t<0) t=ts;    
                if(!options.vertical) {
                    $("ul",obj).css("margin-left",(t*w*-1));
                } else {
                    $("ul",obj).css("margin-left",(t*h*-1));
                }
                clickable = true;
                if(options.numeric) setCurrent(t);
            };
            
            function animate(dir,clicked){
                if (clickable){
                    clickable = false;
                    var ot = t;                
                    switch(dir){
                        case "next":
                            t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;                        
                            break;
                        case "prev":
                            t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
                            break;
                        case "first":
                            t = 0;
                            break;
                        case "last":
                            t = ts;
                            break;
                        default:
                            t = dir;
                            break;
                    };    
                    var diff = Math.abs(ot-t);
                    var speed = diff*options.speed;                        
                    if(!options.vertical) {
                        p = (t*w*-1);
                        $("ul",obj).animate(
                            { marginLeft: p },
                            { queue:false, duration:speed, complete:adjust }
                        );                
                    } else {
                        p = (t*h*-1);
                        $("ul",obj).animate(
                            { marginTop: p },
                            { queue:false, duration:speed, complete:adjust }
                        );                    
                    };
                    
                    if(!options.continuous && options.controlsFade){                    
                        if(t==ts){
                            $("a","#"+options.nextId).hide();
                            $("a","#"+options.lastId).hide();
                        } else {
                            $("a","#"+options.nextId).show();
                            $("a","#"+options.lastId).show();                    
                        };
                        if(t==0){
                            $("a","#"+options.prevId).hide();
                            $("a","#"+options.firstId).hide();
                        } else {
                            $("a","#"+options.prevId).show();
                            $("a","#"+options.firstId).show();
                        };                    
                    };                
                    
                    if(clicked) clearTimeout(timeout);
                    if(options.auto && dir=="next" && !clicked){;
                        timeout = setTimeout(function(){
                            animate("next",false);
                        },diff*options.speed+options.pause);
                    };
            
                };
                
            };
            // init
            var timeout;
            if(options.auto){;
                timeout = setTimeout(function(){
                    animate("next",false);
                },options.pause);
            };        
            
            if(options.numeric) setCurrent(0);
        
            if(!options.continuous && options.controlsFade){                    
                $("a","#"+options.prevId).hide();
                $("a","#"+options.firstId).hide();                
            };                
            
        });
     
    };

})(jQuery);
  1. /*
  2. *     Easy Slider 1.7 - jQuery plugin
  3. *    written by Alen Grakalic    
  4. *    http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
  5. *
  6. *    Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
  7. *    Dual licensed under the MIT (MIT-LICENSE.txt)
  8. *    and GPL (GPL-LICENSE.txt) licenses.
  9. *
  10. *    Built for jQuery library
  11. *    http://jquery.com
  12. *
  13. */
  14. /*
  15. *    markup example for $("#slider").easySlider();
  16. *    
  17. *     <div id="slider">
  18. *        <ul>
  19. *            <li><img src="images/01.jpg" alt="" /></li>
  20. *            <li><img src="images/02.jpg" alt="" /></li>
  21. *            <li><img src="images/03.jpg" alt="" /></li>
  22. *            <li><img src="images/04.jpg" alt="" /></li>
  23. *            <li><img src="images/05.jpg" alt="" /></li>
  24. *        </ul>
  25. *    </div>
  26. *
  27. */
  28. (function($) {
  29.     $.fn.easySlider = function(options){
  30.      
  31.         // default configuration properties
  32.         var defaults = {            
  33.             prevId:         'prevBtn',
  34.             prevText:         'Previous',
  35.             nextId:         'nextBtn',    
  36.             nextText:         'Next',
  37.             controlsShow:    true,
  38.             controlsBefore:    '',
  39.             controlsAfter:    '',    
  40.             controlsFade:    true,
  41.             firstId:         'firstBtn',
  42.             firstText:         'First',
  43.             firstShow:        false,
  44.             lastId:         'lastBtn',    
  45.             lastText:         'Last',
  46.             lastShow:        false,                
  47.             vertical:        false,
  48.             speed:             800,
  49.             auto:            false,
  50.             pause:            7000,
  51.             continuous:        false,
  52.             numeric:         false,
  53.             numericId:         'controls'
  54.         };
  55.         
  56.         var options = $.extend(defaults, options); 
  57.                 
  58.         this.each(function() { 
  59.             var obj = $(this);                 
  60.             var s = $("li", obj).length;
  61.             var w = $("li", obj).width();
  62.             var h = $("li", obj).height();
  63.             var clickable = true;
  64.             obj.width(w);
  65.             obj.height(h);
  66.             obj.css("overflow","hidden");
  67.             var ts = s-1;
  68.             var t = 0;
  69.             $("ul", obj).css('width',s*w);            
  70.             
  71.             if(options.continuous){
  72.                 $("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
  73.                 $("ul", obj).append($("ul li:nth-child(2)", obj).clone());
  74.                 $("ul", obj).css('width',(s+1)*w);
  75.             };                
  76.             
  77.             if(!options.vertical) $("li", obj).css('float','left');
  78.                                 
  79.             if(options.controlsShow){
  80.                 var html = options.controlsBefore;                
  81.                 if(options.numeric){
  82.                     html += '<ol id="'+ options.numericId +'"></ol>';
  83.                 } else {
  84.                     if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
  85.                     html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
  86.                     html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
  87.                     if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';                
  88.                 };
  89.                 
  90.                 html += options.controlsAfter;                        
  91.                 $(obj).after(html);                                        
  92.             };
  93.             
  94.             if(options.numeric){                                    
  95.                 for(var i=0;i<s;i++){                        
  96.                     $(document.createElement("li"))
  97.                         .attr('id',options.numericId + (i+1))
  98.                         .html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
  99.                         .appendTo($("#"+ options.numericId))
  100.                         .click(function(){                            
  101.                             animate($("a",$(this)).attr('rel'),true);
  102.                         });                                                 
  103.                 };                            
  104.             } else {
  105.                 $("a","#"+options.nextId).click(function(){        
  106.                     animate("next",true);
  107.                 });
  108.                 $("a","#"+options.prevId).click(function(){        
  109.                     animate("prev",true);                
  110.                 });    
  111.                 $("a","#"+options.firstId).click(function(){        
  112.                     animate("first",true);
  113.                 });                
  114.                 $("a","#"+options.lastId).click(function(){        
  115.                     animate("last",true);                
  116.                 });                
  117.             };
  118.             
  119.             function setCurrent(i){
  120.                 i = parseInt(i)+1;
  121.                 $("li", "#" + options.numericId).removeClass("current");
  122.                 $("li#" + options.numericId + i).addClass("current");
  123.             };
  124.             
  125.             function adjust(){
  126.                 if(t>ts) t=0;        
  127.                 if(t<0) t=ts;    
  128.                 if(!options.vertical) {
  129.                     $("ul",obj).css("margin-left",(t*w*-1));
  130.                 } else {
  131.                     $("ul",obj).css("margin-left",(t*h*-1));
  132.                 }
  133.                 clickable = true;
  134.                 if(options.numeric) setCurrent(t);
  135.             };
  136.             
  137.             function animate(dir,clicked){
  138.                 if (clickable){
  139.                     clickable = false;
  140.                     var ot = t;                
  141.                     switch(dir){
  142.                         case "next":
  143.                             t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;                        
  144.                             break;
  145.                         case "prev":
  146.                             t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
  147.                             break;
  148.                         case "first":
  149.                             t = 0;
  150.                             break;
  151.                         case "last":
  152.                             t = ts;
  153.                             break;
  154.                         default:
  155.                             t = dir;
  156.                             break;
  157.                     };    
  158.                     var diff = Math.abs(ot-t);
  159.                     var speed = diff*options.speed;                        
  160.                     if(!options.vertical) {
  161.                         p = (t*w*-1);
  162.                         $("ul",obj).animate(
  163.                             { marginLeft: p },
  164.                             { queue:false, duration:speed, complete:adjust }
  165.                         );                
  166.                     } else {
  167.                         p = (t*h*-1);
  168.                         $("ul",obj).animate(
  169.                             { marginTop: p },
  170.                             { queue:false, duration:speed, complete:adjust }
  171.                         );                    
  172.                     };
  173.                     
  174.                     if(!options.continuous && options.controlsFade){                    
  175.                         if(t==ts){
  176.                             $("a","#"+options.nextId).hide();
  177.                             $("a","#"+options.lastId).hide();
  178.                         } else {
  179.                             $("a","#"+options.nextId).show();
  180.                             $("a","#"+options.lastId).show();                    
  181.                         };
  182.                         if(t==0){
  183.                             $("a","#"+options.prevId).hide();
  184.                             $("a","#"+options.firstId).hide();
  185.                         } else {
  186.                             $("a","#"+options.prevId).show();
  187.                             $("a","#"+options.firstId).show();
  188.                         };                    
  189.                     };                
  190.                     
  191.                     if(clicked) clearTimeout(timeout);
  192.                     if(options.auto && dir=="next" && !clicked){;
  193.                         timeout = setTimeout(function(){
  194.                             animate("next",false);
  195.                         },diff*options.speed+options.pause);
  196.                     };
  197.             
  198.                 };
  199.                 
  200.             };
  201.             // init
  202.             var timeout;
  203.             if(options.auto){;
  204.                 timeout = setTimeout(function(){
  205.                     animate("next",false);
  206.                 },options.pause);
  207.             };        
  208.             
  209.             if(options.numeric) setCurrent(0);
  210.         
  211.             if(!options.continuous && options.controlsFade){                    
  212.                 $("a","#"+options.prevId).hide();
  213.                 $("a","#"+options.firstId).hide();                
  214.             };                
  215.             
  216.         });
  217.      
  218.     };
  219. })(jQuery);


Many thanks again!

Alec
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 152 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.