在jQuery中循环图像 - 随机化吗?

oJM*_*86o 3 css jquery jquery-cycle

我有一个网站有一个div有一些图像:

 <div class="slideshow">
            <img src="lib/images/grid/slideshow/a-960x305/a_pic1.jpg" width="960" height="305" alt="1" class="first" />
            <img src="lib/images/grid/slideshow/a-960x305/a_pic2.jpg" width="960" height="305" alt="2" />
            <img src="lib/images/grid/slideshow/a-960x305/a_pic3.jpg" width="960" height="305" alt="3" />
            <img src="lib/images/grid/slideshow/a-960x305/a_pic4.jpg" width="960" height="305" alt="4" />
  </div>
Run Code Online (Sandbox Code Playgroud)

请注意,第一个图像具有名为first的类名.这样页面只能通过css一次显示一个图像:

这是CSS:

/*begin slideshow*/
div.slideshow                              { margin:0 0 5px 0; /*height:305px;*/ } /*for the jQuery cycle plug in*/
div.slideshow img                          { display:none;}
div.slideshow img.first                    { display:block;}
.caption                                   { text-align:center; font-weight:bold; color:#1F7FB6;}
/*end slideshow*/
Run Code Online (Sandbox Code Playgroud)

我正在使用jquery循环库循环遍历图像,如下所示:

 <script type="text/javascript">
        $(document).ready(function () {
            $('.slideshow').cycle({
                fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                timeout: 7000,
                after: function () {
                    $('.caption').html(this.alt);
                }
            });
        });
    </script>
Run Code Online (Sandbox Code Playgroud)

问题是页面当然总是从第一个图像开始,是否有一种方法可以随机化这一点,以便它可以启动随机图像,而不是始终以a_pic1.jpg?开头?

Mik*_*keM 6

设置random1在jQuery的周期选择

random:0,// true表示随机,false表示序列(不适用于shuffle fx)

$(document).ready(function () {
    $('.slideshow').cycle({
        fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        timeout: 7000,
        after: function () {
            $('.caption').html(this.alt);
        },
        random: 1 // <-- add this
    });
});
Run Code Online (Sandbox Code Playgroud)