php中图像的随机播放顺序
我有以下图像用于旋转木马。每次加载页面时,我都希望它们以不同的顺序排列。我正在考虑使用随机数生成器对数字进行排序,但后来我不确定如何使这些数字仅使用一次。如果这可以在循环中完成并且可以扩展,那就太好了。
请参阅下面的静态代码,除了末尾的数字之外,所有图像的名称都相同,
<div class="image-entry">
<img src="/images/carousel-1.jpg" />
</div>
<div class="image-entry">
<img src="/images/carousel-2.jpg" />
</div>
<div class="image-entry">
<img src="/images/carousel-3.jpg" />
</div>
<div class="image-entry">
<img src="/images/carousel-4.jpg" />
</div>
谢谢!
I have the following images in that I'm using for a carousel. Each time the page loads I want them in in a different order. I was thinking of just ordering the numbers using a random number generator, but then I'm not sure how to make it so that the numbers are used only once. If this could be done in a loop so it's expandable that would be great.
See the static code below, all the images are named the same except for the number at the end
<div class="image-entry">
<img src="/images/carousel-1.jpg" />
</div>
<div class="image-entry">
<img src="/images/carousel-2.jpg" />
</div>
<div class="image-entry">
<img src="/images/carousel-3.jpg" />
</div>
<div class="image-entry">
<img src="/images/carousel-4.jpg" />
</div>
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个函数,
shuffle()
:There is a function for that,
shuffle()
:假设您知道需要多少张图像(比如 4 张),并且该数量以内的所有图像都是有效的并且以 1 开头:
Assuming that you know how many images (say 4) you want and that all images up to that number are valid and start with 1:
创建一个数字/图像名称数组,然后对数组进行打乱,然后输出它,就像这样。
您可以调整上述代码以满足您的需求。
Create an array of the numbers/image names and then shuffle the array, and then output it, like so.
You can adapt the above code to suit your needs.
该解决方案的好处是,它将自动检测图像文件夹中名为 carousel-X.jpg 的所有图像,并在轮播中使用它们。
或者甚至:
The benefits of this solution being that it will automagically detect all images called carousel-X.jpg in the images folder and use them in the carousel.
Or even: