gpt4 book ai didi

javascript - 更改图像 div 中的一张图像

转载 作者:行者123 更新时间:2023-12-03 10:17:35 25 4
gpt4 key购买 nike

我正在尝试更改带有图像的 div 中的一个随机图像的图像 scr。这是我的代码:

setInterval(ChangeToBomb, 3000);

function ChangeToBomb() {

$('#depionnen img').fadeOut(100, function(){
$(this).attr('src', bombs[0]).fadeIn(100);
})
}

此时 div 元素中的每个图像都在发生变化。

有人知道如何对单个随机图像执行此操作吗?

最佳答案

我会使用 Milind 解决方案的变体。由于我们已经拥有图像列表,因此不必构建一个选择器只是为了提取图像,只需从列表中获取其中一个即可:

var images = $('#depionnen img');
var randomimgindex = Math.floor(Math.random() * images.length);

$(images[randomimgindex]).fadeOut(100, function(){
$(images[randomimgindex]).attr('src', bombs[0]).fadeIn(100);
});

var bombs = [ "http://placehold.it/300x300" ];

var images = $('#depionnen img');
var randomimgindex= Math.floor(Math.random() * (images.length));

$(images[randomimgindex]).fadeOut(100, function(){
$(images[randomimgindex]).attr('src', bombs[0]).fadeIn(100);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="depionnen">
<img src="http://placehold.it/200x200" alt="" />
<img src="http://placehold.it/200x200" alt="" />
<img src="http://placehold.it/200x200" alt="" />
<img src="http://placehold.it/200x200" alt="" />
<img src="http://placehold.it/200x200" alt="" />
<img src="http://placehold.it/200x200" alt="" />
<img src="http://placehold.it/200x200" alt="" />
</div>

关于javascript - 更改图像 div 中的一张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798607/

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