Random Intro Image Working JS
-
Hello,
Since I finally managed to do it and some people are asking it, here's the JS code I used :
<script> var images = ['https://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/article_thumbnails/other/cat_relaxing_on_patio_other/1800x1200_cat_relaxing_on_patio_other.jpg', 'https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg', 'https://cdnuploads.aa.com.tr/uploads/Contents/2020/05/14/thumbs_b_c_88bedbc66bb57f0e884555e8250ae5f9.jpg?v=140708', ]; var randomImage = Math.floor(Math.random() * 3) window.laytheme.on("newpageshown", function(layoutObj, type, obj){ if(type == "page" && obj.id == 72){ jQuery("body").css("background-image", "url('" + images[randomImage] +"')"); } else { jQuery("body").css("background-image", "") } }); </script>
You just have to change the url of the images, the number of images and the id of the corresponding page.
Enjoy !
-
Hey @Nemo!
Thanks for sharing your solution!
I'm on it, but I still have some issues. Maybe you have a small hint?
Instead of having random background images I want to have just random images placed in a separate row. Do I need to give the image I place an id or do I need to insert code via "+more > +html". Also, do I need to change sth for "background-image"?
Thanks for your help!
Best,
Christian
Before you post:
- When using a WordPress Cache plugin, disable it or clear your cache.
- Update Lay Theme and all Lay Theme Addons
- Disable all Plugins
- Go to Lay Options → Custom CSS & HTML, click "Turn Off All Custom Code", click "Save Changes"
This often solves issues you might run into
When you post:
- Post a link to where the problem is
- Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
- If the problem is difficult to explain, post screenshots / link to a video to explain it
Forgot your key, lost your files, need a previous Lay Theme or Addon version?
Go to www.laykeymanager.com