Randomize ul li content on page load
I've tried to do this several times, and so far I can only get my content slide to randomize the order in which the slides are displayed to the user. What i mean by that is that it keeps the same order, but just displays them randomly. I actually need the ORDER of the slides randomized on page load. For example, I have a ul like so:
- <ul class="rotator">
- <li>Slide 1</li>
- <li>Slide 2</li>
- <li>Slide 3</li>
- <li>Slide 4</li>
- <li>Slide 5</li>
- </ul>
And then a pager that is created on the fly below it that allows the user to go back and forth between slides
- <div class="rotatorNavigation>
- <a href="#" name="1">1</a>
- <a href="#" name="2">2</a>
- <a href="#" name="3">3</a>
- <a href="#" name="4">4</a>
- <a href="#" name="5">5</a>
- </div>
I want the pager order to remain the same, as in 1-5, but the slides I want those to be random. So when you load the page, your order could be like 2,5,3,4,1 OR if you refresh again, your order could be 3,5,1,2,4 etc...
Right now, the jquery we have is like so:
- /**
- * Prepare a rotator
- * @return null
- */
- (function($)
- {
- $.fn.prepRotator = function()
- {
- return this.each(function()
- {
- // Set the navigation
- var count = $(this).children().length;
- for (i = 1; i <= count; i++)
- {
- $(this).siblings('#rotatorNavigation').append("<a href='#' name='" + i + "'>" + i + "</a>");
- $(this).children(':eq(' + (i-1) + ')').attr('name', i);
- }
- $(this).children().preload();
- $(this).siblings('#rotatorNavigation').find('a:first').addClass('active');
-
-
- // Set the rotation
- function rotate()
- {
-
- if ($('.rotator li.active').next('li').length > 0)
- $('.rotator li.active').next('li').showRotatorItem();
- else
- $('.rotator li:first').showRotatorItem();
- globals.RotatorTimeout = setTimeout(function(){ rotate(); }, 5000);
- }
- globals.RotatorTimeout = setTimeout(function(){ rotate(); }, 5000);
-
-
- // Set the buttons
- $('#rotatorNavigation a').unbind('click').click(function()
- {
- $(".rotator").find("li[name='" + $(this).attr('name') + "']").showRotatorItem();
- clearTimeout(globals.RotatorTimeout);
- globals.RotatorTimeout = setTimeout(function(){ rotate(); }, 5000);
- return false;
- });
- });
- };
- })(jQuery);
- /**
- * Show the given rotator item
- * @return null
- */
- (function($)
- {
- $.fn.showRotatorItem = function()
- {
- return this.each(function()
- {
- $(this).siblings('.active').removeClass('active').fadeOut();
- $(this).fadeIn().addClass('active');
- $('#rotatorNavigation a.active').removeClass('active');
- $('#rotatorNavigation').find("a[name='" + $(this).attr('name') + "']").addClass('active');
- var num = Number($(this).attr('name'));
- $('#rotatorNavigation span.triangle').animate(
- {
- left: $('#rotatorNavigation a.active').position().left + (num > 9 ? 14 : 11)
- }, 'fast');
- });
- };
- })(jQuery);
How would I randomize this?