Hello:
I am trying to add a gallery on the homepage of my website that is linked to a data collection of mine that will pick from this data collection random images every single time a new image pops up. My field for my data collection is entitled “Image” and my data collection is entitled “MyCollection901”. Here:
(I blue-d out the images because they are private).
Anyways, I previously tried coping and pasting a code I found posted by an Admin named Yisrael that looks like this:
import wixWindow from ‘wix-window’;
import wixData from ‘wix-data’;
$w.onReady( async function () {
if (wixWindow.rendering.env === ‘browser’ || wixWindow.viewMode === ‘Preview’) {
let res = await wixData.query(“MyCollection901”).find();
let items = res.items;
let count = items.length; // how many images do we have?
let rnd = Math.floor(Math.random() * count);
$w(‘#gallery1’).src = items[rnd].image; // get a random image
$w(‘#gallery1’).show(); // image on page is hidden, we we now show it
}
});
I have integrated my own website information in the pasted code above, i.e. I added in my data collection entitled “MyCollection901” and I added in “#gallery1” because, as mentioned in the first few words of this post, “I am trying to add a gallery…” and this gallery I have added onto my homepage is the only gallery I have on any of my pages on my website.
However: this pasted code above has a problem according to my needs. It runs smoothly (as tested out on the Preview version of my site), but doesn’t quite have in mind what I am trying to get at, and I have been unable to find any sort of other help to reach my goal stated in the first part of this post. According to this code, the first image in my gallery is completely random, but from there, it only continues either in an increasing manner (e.g. image 9/15 turns into image 10/15 from my data collection, and so on) or in a decreasing manner (e.g. image 8/15 turns into image 7/15 from my data collection, and so on). This increasing manner or decreasing manner, I have found out, is only due to my manual switch in the Settings of the gallery itself, where it says, once scrolled all the way down, “Where do they start?”
My goal in a possibly clearer form: How can I edit the code above to keep the first image that pops up randomized, but continue with this randomization process every slide, rather than in an increasing or decreasing manner. For example, how can I have a slideshow on my homepage that starts, say (out of random selection from a successful Corvid code), on image 5/15 of my data collection (entitled, again, as “MyCollection901”), and then only move forward to another randomly selected image on slide number 2, say, on image 13/15, and then again onto, say, image 1/15…and so on, on loop for as long as the page is open.
If I need to be more clear, please let me know. I have been trying to figure this out for the past 3 days and I finally refrained to typing out and asking on here my own specific situation rather than trying to mirror the situations of others who may or may not have the same intentions with a slideshow…
Anyways, I appreciate the help.