gpt4 book ai didi

jquery - 带有 href 的多个随机 img

转载 作者:行者123 更新时间:2023-12-01 07:06:14 25 4
gpt4 key购买 nike

我有多个带有链接的图像。我想随机展示其中 5 个。我在网上找到了一些脚本并修改了它。这是一个糟糕的代码,有时它并不总是显示 5 个图像。

html:

<div id="story">
<a href="#1"><img src="https://dummyimage.com/170x170/000/fff&text=image+1" alt=""></a>
<a href="#2"><img src="https://dummyimage.com/170x170/000/fff&text=image+2" alt=""></a>
<a href="#3"><img src="https://dummyimage.com/170x170/000/fff&text=image+3" alt=""></a>
<a href="#4"><img src="https://dummyimage.com/170x170/000/fff&text=image+4" alt=""></a>
<a href="#5"><img src="https://dummyimage.com/170x170/000/fff&text=image+5" alt=""></a>
<a href="#6"><img src="https://dummyimage.com/170x170/000/fff&text=image+6" alt=""></a>
<a href="#7"><img src="https://dummyimage.com/170x170/000/fff&text=image+7" alt=""></a>
<a href="#8"><img src="https://dummyimage.com/170x170/000/fff&text=image+8" alt=""></a>
<a href="#9"><img src="https://dummyimage.com/170x170/000/fff&text=image+9" alt=""></a>
<a href="#10"><img src="https://dummyimage.com/170x170/000/fff&text=image+10" alt=""></a>
<a href="#11"><img src="https://dummyimage.com/170x170/000/fff&text=image+11" alt=""></a>
<a href="#12"><img src="https://dummyimage.com/170x170/000/fff&text=image+12" alt=""></a>
<a href="#13"><img src="https://dummyimage.com/170x170/000/fff&text=image+13" alt=""></a>
<a href="#14"><img src="https://dummyimage.com/170x170/000/fff&text=image+14" alt=""></a>
<a href="#15"><img src="https://dummyimage.com/170x170/000/fff&text=image+15" alt=""></a>
<a href="#16"><img src="https://dummyimage.com/170x170/000/fff&text=image+16" alt=""></a>
<a href="#17"><img src="https://dummyimage.com/170x170/000/fff&text=image+17" alt=""></a>
</div>

js

function randomize() {
var allBanners = $('#story a');
allBanners.hide();

var index = Math.floor(Math.random() * allBanners.length);
allBanners.eq(index).show();

var index2 = Math.floor(Math.random() * allBanners.length - 1);
allBanners.not(allBanners.eq(index)).eq(index2).show();

var index3 = Math.floor(Math.random() * allBanners.length - 1);
allBanners.not(allBanners.eq(index).eq(index2)).eq(index3).show();

var index4 = Math.floor(Math.random() * allBanners.length - 1);
allBanners.not(allBanners.eq(index).eq(index2).eq(index3)).eq(index4).show();

var index5 = Math.floor(Math.random() * allBanners.length - 1);
allBanners.not(allBanners.eq(index).eq(index2).eq(index3).eq(index4)).eq(index5).show();
}
randomize();

最佳答案

您可以使用这个小代码

randomElements = allBanners.get().sort(function() {
return Math.round(Math.random()) - 0.5
}).slice(0, 5)

在调用$(randomElements).show()

之后

function randomize() {
var allBanners = $('#banners a');
allBanners.hide();

randomElements = allBanners.get().sort(function() {
return Math.round(Math.random()) - 0.5
}).slice(0, 5)

$(randomElements).show()
}
$('button').click(randomize);
randomize();
a img {
width: 19%
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banners">
<a href="#1"><img src="https://dummyimage.com/170x170/000/fff&text=image+1" alt=""></a>
<a href="#2"><img src="https://dummyimage.com/170x170/000/fff&text=image+2" alt=""></a>
<a href="#3"><img src="https://dummyimage.com/170x170/000/fff&text=image+3" alt=""></a>
<a href="#4"><img src="https://dummyimage.com/170x170/000/fff&text=image+4" alt=""></a>
<a href="#5"><img src="https://dummyimage.com/170x170/000/fff&text=image+5" alt=""></a>
<a href="#6"><img src="https://dummyimage.com/170x170/000/fff&text=image+6" alt=""></a>
<a href="#7"><img src="https://dummyimage.com/170x170/000/fff&text=image+7" alt=""></a>
<a href="#8"><img src="https://dummyimage.com/170x170/000/fff&text=image+8" alt=""></a>
<a href="#9"><img src="https://dummyimage.com/170x170/000/fff&text=image+9" alt=""></a>
<a href="#10"><img src="https://dummyimage.com/170x170/000/fff&text=image+10" alt=""></a>
<a href="#11"><img src="https://dummyimage.com/170x170/000/fff&text=image+11" alt=""></a>
<a href="#12"><img src="https://dummyimage.com/170x170/000/fff&text=image+12" alt=""></a>
<a href="#13"><img src="https://dummyimage.com/170x170/000/fff&text=image+13" alt=""></a>
<a href="#14"><img src="https://dummyimage.com/170x170/000/fff&text=image+14" alt=""></a>
<a href="#15"><img src="https://dummyimage.com/170x170/000/fff&text=image+15" alt=""></a>
<a href="#16"><img src="https://dummyimage.com/170x170/000/fff&text=image+16" alt=""></a>
<a href="#17"><img src="https://dummyimage.com/170x170/000/fff&text=image+17" alt=""></a>
</div>

关于jquery - 带有 href 的多个随机 img,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44158865/

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