Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Lay Theme Forum

  1. Home
  2. General Discussion
  3. Random Intro Image Working JS

Random Intro Image Working JS

Scheduled Pinned Locked Moved General Discussion
5 Posts 3 Posters 250 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nemo
    wrote on last edited by
    #1

    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 !

    1 Reply Last reply
    0
    • RichardR Offline
      RichardR Offline
      Richard
      Global Moderator
      wrote on last edited by
      #2

      Dear @Nemo

      Thank you so much for posting, awesome!
      This will i'm sure help others in the future :)

      Thank you for using Lay Theme & helping the community.

      Have a wonderful day,

      Sincerely
      Richard

      1 Reply Last reply
      0
      • N Offline
        N Offline
        Nemo
        wrote on last edited by
        #3

        Hello Richard,

        Yes I posted it in another thread ! You can delete this one !

        1 Reply Last reply
        0
        • RichardR Offline
          RichardR Offline
          Richard
          Global Moderator
          wrote on last edited by
          #4

          Dear @Nemo

          Thanks once again :)

          Best wishes
          Richard

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chris.vuko
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            I also code custom websites or custom Lay features.
            šŸ’æ Email me here: šŸ’æ
            info@laytheme.com

            Before you post:
            1. When using a WordPress Cache plugin, disable it or clear your cache.
            2. Update Lay Theme and all Lay Theme Addons
            3. Disable all Plugins
            4. 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:
            1. Post a link to where the problem is
            2. Does the problem happen on Chrome, Firefox, Safari or iPhone or Android?
            3. If the problem is difficult to explain, post screenshots / link to a video to explain it
            Online Users
            arminunruhA
            arminunruh
            F
            francesco
            L
            lurchifon
            Forgot your key, lost your files, need a previous Lay Theme or Addon version? Go to www.laykeymanager.com
            laytheme.com
            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Recent
            • Tags
            • Popular
            • Users
            • Search