Random Slideshow

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.

The Gallery component does not have a src property. Instead, it has the items property which is used to assign the images to be displayed in the Gallery.

If you want to create a completely randomized Gallery, you can build your own list of items as shown in the example code for the items property .

Here’s a simple Random Gallery example which should help you get started.

Hey, your template is really helpful for my design. I’m completely new about code/syntax in Wix, but could you please show me how I should change the code if I need to add a link (a subpage link) for each image so when users click the image, they can be directed to the subpage. Thank you in advance!!! :heart:

Hello.
I have been trying to use this Random Gallery Example .
It’s working fine in preview mode but I have issues when the page is published.

This is the code I use for my gallery

// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixWindow from 'wix-window';
import wixData from 'wix-data';

$w.onReady(async function () {

 // Check the render env so this is just done once.
 // Otherwise, server-side will render, and then the gallery images
 // switch to the gallery images that the client-side rendered.
 if (wixWindow.rendering.env === 'browser' || wixWindow.viewMode === 'Preview') {

 let res = await wixData.query("hasard").find();
 let items = res.items;
 let count = items.length; // how many images do we have?

 const size = 3; // # of images to display in the gallery
 var rnd = [];
 while (rnd.length < size) {
 var r = Math.floor(Math.random() * count);
 if (rnd.indexOf(r) === -1) rnd.push(r);
        }

        $w("#gallery1").items = [{
 "type": "image",
 "alt": items[rnd[0]].title,
 "title": items[rnd[0]].title,
 "src": items[rnd[0]].image
        }, {
 "type": "image",
 "alt": items[rnd[1]].title,
 "title": items[rnd[1]].title,
 "src": items[rnd[1]].image
        }, {
 "type": "image",
 "alt": items[rnd[2]].title,
 "title": items[rnd[2]].title,
 "src": items[rnd[2]].image
        }];

        $w('#gallery1').show(); // show the gallery after the images are set
    }
});

My problems :

  1. My thumbnails don’t show on the published page online. Instead of that I see the default thumbnails set in the gallery settings.

The default thumbnails, not my thumbnails…

I found that if I deleted the “if statement”, the good thumbnails showed up BUT then I ran into my second problem…

if (wixWindow.rendering.env === 'browser' || wixWindow.viewMode === 'Preview')
  1. The thumbnails don’t match the images showing up. When I click on a thumbnail, I open another image in the Lightbox… How is it possible ?

Any idea ? Any help ? Thanks a lot

Well I could make it work finally but ran into a last little problem.

My gallery is shown before the images are randomly picked. So I frequently have problem because the layout doesn’t fit the images and it looks messed up !

When I refresh a few times, it works. But not 100%…

How can I ask the gallery to wait the random pick before loading the layout ? I tried setTimeout() on the gallery but it doesn’t work…

Any idea ?

Well I could make it work finally but ran into a last little problem.

My gallery is shown before the images are randomly picked. So I frequently have problem because the layout doesn’t fit the images and it looks messed up !

When I refresh a few times, it works. But not 100%…

How can I ask the gallery to wait the random pick before loading the layout ? I tried setTimeout() on the gallery but it doesn’t work…

Any idea ?