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. Change background colour upon refresh

Change background colour upon refresh

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