-
AuthorPosts
-
January 15, 2016 at 3:55 pm #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
January 15, 2016 at 3:59 pm #566138Hi 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!
YigitJanuary 15, 2016 at 4:26 pm #566156That code i have doesn’t work. Have you other code to put in javascript to change background color randomly.
Thank you
January 15, 2016 at 4:38 pm #566164Hey!
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,
YigitJanuary 18, 2016 at 1:59 pm #567212Hello,
i put the above code in function.php and doesn’t work.
January 18, 2016 at 2:03 pm #567215Hi!
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!
YigitJanuary 18, 2016 at 2:44 pm #567230I 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, 9 months ago by Isakos-Soft.
-
AuthorPosts
- The topic ‘Change background color randomly’ is closed to new replies.