简体   繁体   English

我制作了一个 javascript 代码,将我的图像从数组随机显示到照片网格中,但我不希望图像重复

[英]i've made a javascript code that randomly displays my images from an array into a photo grid but i don't want the images to repeat

Ok so i've used javascript to randomly display images in my grid using an array the problem i have is that since its grabbing a random image sometimes it grabs the same image twice.好的,所以我使用 javascript 使用数组在我的网格中随机显示图像,我遇到的问题是,由于它有时会抓取随机图像,因此它会两次抓取相同的图像。 i will post my code below but i was just looking for a solution so that once it displays an image it won't display that image again我将在下面发布我的代码,但我只是在寻找解决方案,以便一旦显示图像就不会再次显示该图像

this is my javascript:这是我的javascript:

    var random_images_array = ['atoodbranding2.png', 'thebegining.jpg', 'ella_playin_in_the_yard.png', 'project-1.png','Frontier-attachments-for-sale.png','ipfwadd-01.jpg','invites.png'];

function getRandomImage(imgAr, path) {
    path = path || 'images/portfolio/'; // default path here
    var num = Math.floor( Math.random() * imgAr.length );
    var img = imgAr[ num ];
    var imgStr = '<img src="' + path + img + '" alt = "">';
    document.write(imgStr); document.close();
}

this is my html这是我的 html

<div class="container photo-container bg-3 text-center">


      <div class="photos">
      <a data-toggle="modal" id="1" href="#myModal"><script type="text/javascript">getRandomImage(random_images_array, 'images/portfolio/')</script></a>
      <a data-toggle="modal" id="2" href="#myModal"><script type="text/javascript">getRandomImage(random_images_array, 'images/portfolio/')</script></a>
      <a data-toggle="modal" id="3" href="#myModal"><script type="text/javascript">getRandomImage(random_images_array, 'images/portfolio/')</script></a>
      <a data-toggle="modal" id="4" href="#myModal"><script type="text/javascript">getRandomImage(random_images_array, 'images/portfolio/')</script></a>
      <a data-toggle="modal" id="5" href="#myModal"> <script type="text/javascript">getRandomImage(random_images_array, 'images/portfolio/')</script></a>
      <a data-toggle="modal" id="6" href="#myModal"> <script type="text/javascript">getRandomImage(random_images_array, 'images/portfolio/')</script></a>
      <a data-toggle="modal" id="7" href="#myModal"><script type="text/javascript">getRandomImage(random_images_array, 'images/portfolio/')</script></a>
</div>
</div>

I'm pretty new to javascript so sorry if this is a question thats been answered already or one that is a super easy fix我对 javascript 还很陌生,如果这是一个已经回答的问题或者一个超级简单的解决方法,我很抱歉

You could just splice the selected image out of your array, then it won't be available on the next iteration:您可以将所选图像从数组中拼接出来,然后在下一次迭代中将不可用:

function getRandomImage(imgAr, path) {
    path = path || 'images/portfolio/'; // default path here
    var num = Math.floor( Math.random() * imgAr.length );
    var img = imgAr[ num ];
    // remove the selected image
    imgAr.splice(num, 1);
    var imgStr = '<img src="' + path + img + '" alt = "">';
    document.write(imgStr); document.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个 javascript 数组,想按名称调用图像 - I've got a javascript array and want to call images by name 我不想在javascript上重复相同的代码**新** - I don't want to repeat the same code on javascript **new ** 每次我重新加载网站时,从数组中随机选择一组6张不同的图像 - Randomly select a group of 6 different images from an array each time that I reload my site 我有7张图片。 我想使用javascript向不同的用户随机显示不同的图像。 但是一旦显示,它就不会改变 - I have 7 images. I want to display different images to different users randomly using javascript. But once it is displayed, it should not change 我想在javascript中预加载图像数组并暂停直到所有图像都加载完毕 - I want to preload an array of images in javascript and pause until they are all loaded 我看不到Javascript数组中的图像 - I cannot see images from Javascript array 我想将图像放入 <div> 代替图像数组 - I want to put my images in a <div> instead of an image array 我不希望使用javascript为我的cookie设置特定路径 - I don't want a specific path for my cookie using javascript 我想重复代码,但不知道如何使用循环 - I want to repeat code but don't know how t use loops 我希望我的代码创建由输入行和列数的块组成的网格 - I want my code to create grid made of blocks with input amount of rows and columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM