我的网站上有一个包含图片的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>
请注意,第一个图像具有名为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*/
我正在使用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>
问题是页面当然总是从第一张图片开始,有没有办法随机化这一点,以便它可以启动随机图像而不是始终以a_pic1.jpg
开头?
答案 0 :(得分:6)
在jQuery周期options
中将random
设置为1
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
});
});
答案 1 :(得分:0)
我不熟悉循环库,但这里是一个使用数组的例子:
<script>
var banners = new Array("#banner1","#banner2","#banner3","#banner4");
var counter = //random number between 0-3;
$(document).ready(function()
{
nextBanner();
});
function nextBanner()
{
var currentBanner = counter;
counter++;
if (counter > banners.length -1)
{
counter = 0;
}
$( banners[currentBanner] ).fadeOut(300 );
setTimeout('$( banners[counter] ).fadeIn( 300)', 100);
setTimeout("nextBanner()", 9000);
}
</script>
<div id="banner1" class="banner-item">
<img src="/banner1.jpg" />
</div>
<div id="banner2" class="banner-item" style="display: none;">
<img src="banner2.jpg" />
</div>
...
css
.banner-item
{
position: absolute;
}
答案 2 :(得分:0)
<script type="text/javascript">
$(document).ready(function () {
$('.slideshow img').get(random()*($('.slideshow img').length-1)).addClass('first');
$('.slideshow').cycle({
....
答案 3 :(得分:0)
在拨打.cycle()
电话之前,您可以执行以下操作:
var images = $('.slideshow > img');
images.get(0).removeClass("first");
images.get(Math.floor(Math.random() * images.length)).addClass("first");
答案 4 :(得分:0)
取消class ='first'位,然后运行此代码以随机化获得class =“first”的图像
$(function(){
var rnd = Math.floor(Math.random() * $('.slideshow img').length);
$('.slideshow img').eq(rnd).addClass("first");
});