Random background color / custom JS

Posted in: Newspaper
Post count: 5

Hi there,

I’m trying to get a random background color on every page refresh for the top-menu element in my Newspaper 9.2 theme. I assume the best way to do this is to insert some custom Javascript, so I looked at the JS files that are included in Newspaper as an example and came up with this:


var tdRotatingColors = {};

( function() {
"use strict";
tdRotatingColors = {
arrayColors: ["#46e6b4", "#29b0cf", "#fa656e", "#feb92b"]};
})();
//create the animation
render: function render (color) {
for (var cnt = 0; cnt < arrayColors.items.length; cnt++) {
var random = Math.floor(Math.random() * colors.length);
var selectedColor = colors[random];

//call the animationDisplay function
tdRotatingColors.changeColor(
'<div class="td-header-top-menu-full" style="background-color:' + tdRotatingColors.arrayColors[0] + '"></div>'
);
arrayColors.items.splice(random, 1); // remove the item from the "array"
}
}

Unfortunately this code does not work. I do see that it appears as the first script called in all of the JS near the footer, but it does not change the color of the element. Is this the best way to do this? What am I doing wrong?

Thanks for your help!

Post count: 35449

Hi,

Please check this documentation about How to Randomly Change Background Color in WordPress
-> https://www.wpbeginner.com/wp-themes/how-to-randomly-change-background-color-in-wordpress/

Thank you!

Viewing 2 posts - 1 through 2 (of 2 total)
The forum ‘Newspaper’ is closed to new topics and replies.