gpt4 book ai didi

javascript - 使用随机起始图像滚动图像

转载 作者:太空宇宙 更新时间:2023-11-04 04:14:48 25 4
gpt4 key购买 nike

我有以下标记:

<div class="image">
<img src="http://placehold.it/525x160" />
<img src="http://placehold.it/525x170" />
<img src="http://placehold.it/525x180" />
<img src="http://placehold.it/525x190" />
</div>

使用 CSS:

.image {
position:relative;
}
.image img {
position:absolute;
top:0;
left:0;
}

我想循环浏览图像。如果我从图像 0 开始,这很好用:

$('.image img:gt(0)').hide(); // to hide all but the first image when page loads

setInterval(function () {
$('.image :first-child').fadeOut(1000)
.next().fadeIn(1000).end().appendTo('.image');
}, 5000);

,但是当我选择一个随机的起始图像时,它以该图像可见开始,然后在图像 2 应该淡入 [随机图像编号]+1 时淡入淡出

var imgNum = Math.floor(Math.random() * 3)
$('.image img:gt(' + imgNum + ')').hide(); //hide all images except the randomly picked one

setInterval(function () {
$('.image :first-child').fadeOut(1000)
.next().fadeIn(1000).end().appendTo('.image');
}, 5000);

Original Fiddle

Random number Fiddle

最佳答案

这是 UPDATED DEMO

JS代码

var maxImg = $('.image img').length,
startImg = Math.floor((Math.random()*maxImg)+1);
$('.image img').hide();
$('.image img:nth-child('+startImg+')').show();
setInterval(function () {
$('.image img:visible').fadeOut(1000);
if(startImg+1 > maxImg)
startImg = 1;
else
startImg++;
console.log(startImg);
$('.image img:nth-child('+startImg+')').fadeIn(1000);
}, 2000);

关于javascript - 使用随机起始图像滚动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20116986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com