- This topic has 18 replies, 3 voices, and was last updated 3 days, 22 hours ago by
David.
-
AuthorPosts
-
January 25, 2023 at 3:40 am #2508360
Benjamin
Hi David, thank you!
All is in order now.
One last thing, would it be possible to tweak the code so that the image changes automatically every X seconds, without refreshing the page?
Or perhaps another idea is that it is guaranteed to switch to the next image when refreshing the page?
Not sure if this will significantly increase the code size and its effects on page speed. Thanks.
January 25, 2023 at 5:49 am #2508481David
StaffCustomer SupportHmmm… not using that method, as that will only work on page load. And unless you start storing stuff in the browsers localStorage theres no way to load the next image on refresh and that could hammer your LCP times.
Alternative method.
1. Create a Hook Element
1.1 set the Hook towp_footer
1.2 Set the Display Rules to the page you want it to apply,
1.3 In the hooks text area add this script:<script> window.addEventListener("load", function() { // set the time delay const interval = 10000; // set the number of images to cycle const maxImages = 3; var randomNumber = function (min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }; let root = document.documentElement; let cnt = randomNumber(1, maxImages); setInterval(function() { root.style.setProperty('--hero-bg', "var(--hero-image-" + cnt + ")" ); cnt++; if (cnt > maxImages) cnt = 1; }, interval ); }); </script> <style> :root { --hero-bg: var(--hero-fallback); --hero-fallback: url('url_to_fallback'); --hero-image-1: url('url_to_image_one'); --hero-image-2: url('url_to_image_two'); --hero-image-3: url('url_to_image_three'); } .page-hero:before{ background-image: var(--hero-bg) !important; } </style>
NOTEs: in
script
theinterval
variable is set to10000ms
( 10 seconds ) and themaxImages
is3
Change those to suit.
In thestyle
change theurl_to_*
strings to the the full image url for each of your URLs.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/January 25, 2023 at 10:47 am #2508946Benjamin
Damn! That’s a lot of code.
Thank you, David. As always, I sincerely appreciate the support you guys provide. World-class!
January 26, 2023 at 3:16 am #2509560David
StaffCustomer SupportGlad to be of help!
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.