Tell me, please, how to make the random block instead of the first block .eq(0) selected in your script instead of the first block, and then everything went in order?

 $(document).ready(function() { $('.text .text-block').eq(0).addClass("active").fadeIn(1000); // Показываем первый блок, можно и не первый, если прописать нужную цифру в eq() setInterval('blockAnimate();', 5000); // Вызываем функцию для смены блока каждые 5 секунд }); // Функция для смены блоков, показывает блоки по очереди, начальный блок задаётся выше function blockAnimate() { var length = $('.text .text-block').length - 1; $('.text .text-block').each(function(index) { if($(this).hasClass('active') && index != length) { $(this).removeClass("active").fadeOut(1000).next('.text-block').addClass("active").delay(1000).fadeIn(1000); return false; } else if (index == length) { $(this).removeClass('active').fadeOut(1000); $('.text .text-block').eq(0).addClass("active").delay(1000).fadeIn(1000); return false; } }); }; 
  • you need to replace 0 with the number obtained by chance, for example using Math.random - Grundy

1 answer 1

 random=Math.random() * (max - min) + min; $(document).ready(function() { $('.text .text-block').eq(random).addClass("active").fadeIn(1000); setInterval('blockAnimate();', 5000); }); 

Where min - 0 and max - the number of your items.

  • Thanks, I'll check it out now ;;) - Maxim
  • Everything works, thanks again!) - Max.