Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #566131

    Hello,

    how to change background color randomly in layout boxed style. I want to set some hex color and then change randomly when user do reload site.

    I want to put javascript code to do that. The code is.

    Where can i put that code or i need to use something esle? Which is class name of background layout boxed style in enfold theme?

    Thank you

    #566138

    Hi Isakos-Soft!

    Please add following code to Functions.php file in Appearance > Editor

    function add_custom_code(){
    ?>
    <script>
    function get_random_color() { //function name
    var color = '#'; // hexadecimal starting symbol
    var letters = ['000000','FF0000','00FF00','0000FF','FFFF00','00FFFF','FF00FF','C0C0C0']; //Set your colors here
    color += letters[Math.floor(Math.random() * letters.length)];
    document.getElementByClassName('html.html_boxed').style.background = color; // Setting the random color element.
    }
    </script>
    <?php
    }
    add_action('wp_footer', 'add_custom_code');

    Cheers!
    Yigit

    #566156

    That code i have doesn’t work. Have you other code to put in javascript to change background color randomly.

    Thank you

    #566164

    Hey!

    Please try using following code instead

    function add_custom_code(){
    ?>
    <script>
    jQuery(document).ready(function(){
      var colors = ["#000000","#FF0000","#00FF00","#0000FF","#FFFF00","#00FFFF","#FF00FF","#C0C0C0"];                
      var rand = Math.floor(Math.random()*colors.length);           
      jQuery('html.html_boxed').css("background", colors[rand]);
    });
    </script>
    <?php
    }
    add_action('wp_footer', 'add_custom_code');

    Regards,
    Yigit

    #567212

    Hello,

    i put the above code in function.php and doesn’t work.

    #567215

    Hi!

    It does change the background image each time you refresh your page. I tried on my local installation and it worked fine. If that is what you would like to achieve, please create a temporary admin login and post it here privately. If you would like something more advanced, please hire a freelance developer

    Cheers!
    Yigit

    #567230

    I try again the adove code and now that work it.

    Sorry, I don’t know why the WordPress doesn’t it recognise before.

    The code worded perfectly.

    Thank you very much for your time.

    • This reply was modified 8 years, 11 months ago by Isakos-Soft.
Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Change background color randomly’ is closed to new replies.