Skip to content
  • Recent
  • Tags
  • Popular
  • Users
  • Search
Skins
  • Light
  • Brite
  • 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. Change background colour upon refresh

Change background colour upon refresh

Scheduled Pinned Locked Moved General Discussion
6 Posts 3 Posters 1.2k Views 2 Watching
  • 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.
  • F
    F
    fernandol97
    wrote on last edited by fernandol97
    #1
    This post is deleted!
    1 Reply Last reply
    0
    • F
      F
      fernandol97
      wrote on last edited by
      #2

      I have found the following code on the forum form a couple years ago. however it does not seem to work. Can anyone tell me what i am doing wrong? here is a link to the site: http://mungoadam-smith.co.uk/category/work/

          var colors = ["#551066", '#7b4289', '#a09d24', '#3f3e19', '#3a6b39', '#98c197', '#ab97c1']
          Frontend.GlobalEvents.on("newpageshown", function(){
              var ix = getRandomInt(0, colors.length);
              var color = colors[ix];
              jQuery('body').css('background-color', color);
          });
          
          // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
          // Returns a random integer between min (included) and max (excluded)
          // Using Math.round() will give you a non-uniform distribution!
          function getRandomInt(min, max) {
              min = Math.ceil(min);
              max = Math.floor(max);
              return Math.floor(Math.random() * (max - min)) + min;
          }
      </script
      1 Reply Last reply
      0
      • F
        F
        fernandol97
        wrote on last edited by
        #3

        should i be placing the code under lay options > custom CSS & HTML > Custom HTML at bottom? Any advice would be so appreciated!

        1 Reply Last reply
        0
        • F
          F
          fernandol97
          wrote on last edited by
          #4

          ive also tried the following code :

          const colors = ["#fff", "#000", "#f0f", "#0ff", "#00f", "#ff0", "#0f0"];
          
          window.onload = () => {
            const colorToSet = colors[Math.floor(Math.random() * colors.length)];
            document.querySelector("body").style.backgroundColor = colorToSet;
          };
          

          But all that happens is a bunch of code goes to the top of the page. Pretty new to adding touching the custom css/html. ![alt text](Screenshot 2020-05-28 at 18.29.30.png image url)

          1 Reply Last reply
          0
          • mariusjopenM
            mariusjopenM
            mariusjopen
            Global Moderator
            wrote on last edited by
            #5

            Dear @fernandol97
            you need to wrap them into <script> tags.
            Like in the example above.
            Best!
            Marius

            www.mariusjopen.world

            1 Reply Last reply
            0
            • arminunruhA
              arminunruhA
              arminunruh
              Global Moderator
              wrote on last edited by
              #6

              hey if anyone wants to do this: use this code

              <script>
              var colors = ["#fff", '#000', '#f0f', '#0ff', '#00f', '#ff0', '#0f0']
              window.laytheme.on("newpageshown", function(){
              	var ix = getRandomInt(0, colors.length);
              	var color = colors[ix];
              	jQuery('#grid, #custom-phone-grid, .cover-region-desktop, .cover-region-phone, #footer, #footer-custom-phone-grid').css('background-color', color);
              });
              
              // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
              // Returns a random integer between min (included) and max (excluded)
              function getRandomInt(min, max) {
              	min = Math.ceil(min);
              	max = Math.floor(max);
              	return Math.floor(Math.random() * (max - min)) + min;
              }
              </script>
              
              1 Reply Last reply
              0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              Online Users
              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