Hello, I have three distinct background images: back_1.jpg, back_2.jpg, and back_3.jpg.
I’m wondering how I can make it so that each time the page loads, it displays a different background randomly using NODEJS.
I was contemplating something like this:
<div style="background-image: url(back1.jpg);"></div>
However, how could I create an array, randomize the selection, and then apply and display the randomized background?
Any help will be very appreciated.
@ben Any idea?? You always have a solution to my problems…
Teodor
August 25, 2023, 5:46am
3
Chackmool:
However, how could I create an array, randomize the selection, and then apply and display the randomized background?
hi there, your question actually contains the answer.
Use the array component to store the 3 images;
Use the randomize formatter to randomize the array;
Get the 1st image from the randomized array and use it in the dynamic styling for your backgorund;
Example array:
<dmx-array id="arr1" dmx-bind:items="['img1.jpg', 'img2.jpg', 'img3.jpg']"></dmx-array>
The code looks like:
dmx-style:background-image="'url(/assets/img/'+arr1.items.randomize().top(1)+')'"
1 Like
bpj
August 25, 2023, 9:05am
4
@teodor ’s approach is great but you can also do it pre-render (to prevent possible flicker after render but before App Connect gets started) if you are using NodeJS:
<% var randomNumber = Math.floor(Math.random() * 3) + 1; %>
<div style="background-image: url(/assets/img/back<%= randomNumber %>.jpg);"></div>
Notum
August 25, 2023, 9:07am
5
@Teodor Tried to replicate your code but with static image and it’s not working:
<div class="container-fluid" dmx-style:background-image="'url(https://mdbcdn.b-cdn.net/img/new/standard/city/041.webp)'"></div>
bpj
August 25, 2023, 9:09am
6
Can your server handle webp? There was a discussion here:
Hello all,
I have the below code for one of my images and hope that most modern browsers would use the webp version so that the image is optimised but on every browser it just uses the fallback png version.
Any advice on why this could be?
<picture>
<source srcset="/assets/images/The Real Life Money Promise.webp" type="image/webp">
<img src="/assets/images/The Real Life Money Promise.png" class="img-fluid" id="i_howdoesitwork_rlmpromise" alt="Real Life Money Promise Image">
</picture>
…
Teodor
August 25, 2023, 9:11am
7
Notum:
@Teodor Tried to replicate your code but with static image and it’s not working:
<div class="container-fluid" dmx-style:background-image="'url(https://mdbcdn.b-cdn.net/img/new/standard/city/041.webp)'"></div>
Works perfectly fine.
Are you sure your div has some height set at all so you can see the background?
Notum
August 25, 2023, 9:13am
8
I’ve just created blank project to test your solution.
Page is 100% empty.
Here is page code bellow:
<!-- Wappler include head-page="layouts/main" fontawesome_5="cdn" bootstrap5="local" is="dmx-app" id="index" appConnect="local" -->
<div class="container" dmx-style:background-image="url('https://mdbcdn.b-cdn.net/img/new/standard/city/041.webp')"></div>
<meta name="ac:route" content="/">
Teodor
August 25, 2023, 9:14am
9
Well … if your container has a height of 0px it would be extremely hard (impossible) to see its background
1 Like
Notum
August 25, 2023, 9:16am
10
I’m sorry, but I didn’t get it.
I’ve added Heading and changed to fluid and still the same.
<!-- Wappler include head-page="layouts/main" fontawesome_5="cdn" bootstrap5="local" is="dmx-app" id="index" appConnect="local" -->
<div class="container-fluid" dmx-style:background-image="'url('https://mdbcdn.b-cdn.net/img/new/standard/city/041.webp')'">
<div class="row">
<div class="col">
<h1>Fancy display heading</h1>
</div>
</div>
</div>
<meta name="ac:route" content="/">
Teodor
August 25, 2023, 9:20am
11
this:
dmx-style:background-image="'url('https://mdbcdn.b-cdn.net/img/new/standard/city/041.webp')'"
should be
dmx-style:background-image="'url(https://mdbcdn.b-cdn.net/img/new/standard/city/041.webp)'"
1 Like
Notum
August 25, 2023, 9:22am
12
Thank you!
Now I hate CSS even more.
1 Like
I follow your example
But when I run and inspect get this.
Any help??
Teodor
August 25, 2023, 6:51pm
15
Well did you just copy my code without actually applying the formatters using the UI? If that’s the case the formatter js include is probably missing.
1 Like
you was right.
My only problem is now the code is like this
The ": on the code is normal? or I need to scape them in other way??
Thanks a lot
Apple
August 25, 2023, 8:18pm
17
Isn’t missing a [0] to get the first element of the array? Or is .top(1) returning a single object instead of an array with a single object?
Teodor
August 25, 2023, 8:19pm
18
Well top(1) is returning a single item. The code i provided is 100% correct
Is correct this one but as you told me, need to be added via the interface.
Thanks
Apple
August 26, 2023, 2:37am
20
Well top(1) is returning a single item. The code i provided is 100% correct
So it’s returning an array, it shouldn’t be possible the code you wrote! It’s missing the [0] to select the 1st element of the array:
Why it works without [0] is a mystery to me… Where do we stand? Is it ok to teach Wapplers this undocumented “trick”? Or is it better to edit the example?
Maybe Patrick can provide further insight into the behaviour of why your code works when it shouldn’t:
dmx-style:background-image="'url(/assets/img/'+arr1.items.randomize().top(1)+')'"
Seems to me there’s an implicit array to string conversion and it grabs the 1st element of the array, but I don’t know if this is by intentional design