Selecting a random css class from a list

I created a random background code here.

3 different ways.

How do I know which is the better of the 3 codes I made?

The first 2 codes use an array, code 3 uses the least amount of code.

To see that it works, just click run in the jsfiddle.

Code 1
https://jsfiddle.net/a6hm3s5d/1/

(function randomBackground() {
  const colors = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
  const random = colors[Math.floor(Math.random() * colors.length)];
  document.querySelector('body').classList.add('play' + random);
}());

Code 2
https://jsfiddle.net/r2c5Lf4v/1/

(function randomBackground() {
  const colors = ['play1', 'play2', 'play3', 'play4', 
  'play5', 'play6', 'play7', 'play8', 'play9'];
  const random = Math.floor(Math.random() * colors.length);
  document.querySelector('body').classList.add(colors[random]);
}());

Code 3
https://jsfiddle.net/zhtkyjep/

(function randomBackground() {
    const random = Math.floor(Math.random() * 8 + 1);
    document.querySelector('body').classList.add('play' + random);
}());

css

.play1 {
  --color-a: blue;
  --color-b: black;
  --color-c: red;
  --color-d: black;
}

.play2 {
  --color-a: purple;
  --color-b: black;
  --color-c: purple;
  --color-d: black;
}

.play3 {
  --color-a: green;
  --color-b: black;
  --color-c: green;
  --color-d: black;
}

.play4 {
  --color-a: orange;
  --color-b: black;
  --color-c: orange;
  --color-d: black;
}

.play5 {
  --color-a: yellow;
  --color-b: black;
  --color-c: yellow;
  --color-d: black;
}

.play6 {
  --color-a: blue;
  --color-b: black;
  --color-c: orange;
  --color-d: black;
}

.play7 {
  --color-a: red;
  --color-b: black;
  --color-c: green;
  --color-d: black;
}

.play8 {
  --color-a: white;
  --color-b: black;
  --color-c: white;
  --color-d: black;
}

.play9 {
  --color-a: red;
  --color-b: black;
  --color-c: red;
  --color-d: black;
}

Naming variables appropriately,

In codes 1 & 2, colors can be changed to classNames for better readability.

Code 1
https://jsfiddle.net/9znq65vL/

(function randomBackground() {
    const classNames = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
    const random = classNames[Math.floor(Math.random() * classNames.length)];
    document.querySelector("body").classList.add("play" + random);
}());

Code 2
https://jsfiddle.net/9znq65vL/1/

(function randomBackground() {
  const classNames = ["play1", "play2", "play3", "play4",
    "play5", "play6", "play7", "play8", "play9"
  ];
  const random = Math.floor(Math.random() * classNames.length);
  document.querySelector("body").classList.add(classNames[random]);
}());

When it comes to Code 3, this one uses hard-coded values, and so this one should probably not be used.

https://jsfiddle.net/zhtkyjep/

(function randomBackground() {
    const random = Math.floor(Math.random() * 8 + 1);
    document.querySelector('body').classList.add('play' + random);
}());

The same can be said about the first snippet as well, I think. What if one day you’ll decide to add more classes which names don’t start with “play”?

2 Likes

I was told different things about how this should be written.

Here is my last updated code, how I have it written now. https://jsfiddle.net/r9bygt3d/

(function randomBackground() {
  const classNames = [
    "bg1",
    "bg2",
    "bg3",
    "bg4",
    "bg5",
    "bg6",
    "bg7",
    "bg8",
    "bg9",
    "bg10"
  ];

  const random = Math.floor(Math.random() * classNames.length);
  document.querySelector("body").classList.add(classNames[random]);
}());

Here is another way I was able to write it. https://jsfiddle.net/0hcgkurm/

:root {
  --bg-color: linear-gradient();
}

body {
  background: var(--bg-color, white);
  background-repeat: no-repeat;
  min-height: 100vh;
  overflow: hidden;
}
(function randomBackground() {

  const linearGradients = [
    "linear-gradient(to right, #c6ffdd, #fbd786, #f7797d)",
    "linear-gradient(to right, #6a3093, #a044ff)",
    "linear-gradient(to right, #a8ff78, #78ffd6)",
    "linear-gradient(to right, #6d6027, #d3cbb8)",
    "linear-gradient(to right, #4da0b0, #d39d38)",
    "linear-gradient(to right, #74ebd5, #acb6e5)",
    "linear-gradient(to right, #12c2e9, #c471ed, #f64f59)",
    "linear-gradient(to right, #4b79a1, #283e51)",
    "linear-gradient(to right, #0099f7, #f11712)"
  ];

  const randomColor =
    linearGradients[Math.floor(Math.random() * linearGradients.length)];
  document.querySelector("body").style.setProperty("--bg-color", randomColor);
}());

or maybe this way: https://jsfiddle.net/f7qopgwL/

:root {
  --color-a: linear-gradient(120deg, #155799, #159957);
  --color-b: linear-gradient(0deg, #522db8 0%, #1c7ce0 100%);
  --color-c: linear-gradient(45deg, #102eff, #d2379b);
}
(function randomBackground() {
  const varNames = [
    "color-a",
    "color-b",
    "color-c"
  ];

  const random = Math.floor(Math.random() * varNames.length);
  document.body.style.backgroundImage = 'var(--' + varNames[random] + ')';
}());

Where possible keep the CSS out of the JS.

i.e. Don’t do this unless there is no choice:


 const linearGradients = [
    "linear-gradient(to right, #c6ffdd, #fbd786, #f7797d)",
    "linear-gradient(to right, #6a3093, #a044ff)",
    "linear-gradient(to right, #a8ff78, #78ffd6)",
    "linear-gradient(to right, #6d6027, #d3cbb8)",
    "linear-gradient(to right, #4da0b0, #d39d38)",
    "linear-gradient(to right, #74ebd5, #acb6e5)",
    "linear-gradient(to right, #12c2e9, #c471ed, #f64f59)",
    "linear-gradient(to right, #4b79a1, #283e51)",
    "linear-gradient(to right, #0099f7, #f11712)"
  ];

Edit: Link fixed

3 Likes

The link you provided doesn’t take me to a post.

There might be something broken in sitepoint.

Someone may have to get that sorted out.

1 Like

Might have been a glitch in the matrix - it worked for me…

I’m on Chrome and left clicking on the link with my mouse gives me:

Oops! That page doesn’t exist or is private.

Clicking the link also doesn’t work in Firefox, but right or middle clicking to open it in a new tab does work. I’ve no idea why. sdesole

This is the link: Changing color of navigation bar after 100vh - #9 by Paul_Wilkins If it doesn’t work by clicking, you can copy and paste it.

2 Likes

Right clicking, then “open tab in new link” worked for me.

2 Likes

I was just told that this one, in their opinion would be the cleanest and the most easiest to maintain.

https://jsfiddle.net/r9bygt3d/

Javascript

(function randomBackground() {
  const classNames = [
    "bg1",
    "bg2",
    "bg3",
    "bg4",
    "bg5",
    "bg6",
    "bg7",
    "bg8",
    "bg9",
    "bg10"
  ];

  const random = Math.floor(Math.random() * classNames.length);
  document.querySelector("body").classList.add(classNames[random]);
}());

CSS

.bg1 {
  --color: linear-gradient(76.9deg, #e8bbf2, #8787fd 51.66%, #84caff 109.18%);
}

.bg2 {
  --color: linear-gradient(45deg, #102eff, #d2379b);
}

.bg3 {
  --color: radial-gradient(60% 60% at 50% 50%, rgb(40, 0, 115), rgb(0, 0, 0));
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  background-image: var(--color);
  background-repeat: no-repeat;
}
1 Like

Is there no point to using custom properties here?

What is your opinion on this?

I was told it’s not needed here.

My understanding was, using custom properties is good for, to reduce the size of the code.

Instead of using background-image:

--color: is used instead.

https://jsfiddle.net/j3ypzwhL/

.bg1 {
  --color: linear-gradient(76.9deg, #e8bbf2, #8787fd 51.66%, #84caff 109.18%);
}

.bg2 {
  --color: linear-gradient(45deg, #102eff, #d2379b);
}

.bg3 {
  --color: radial-gradient(60% 60% at 50% 50%, rgb(40, 0, 115), rgb(0, 0, 0));
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  background-image: var(--color);
  background-repeat: no-repeat;
}

And this would be better?

https://jsfiddle.net/eghwx7aj/

body {
  background-repeat: no-repeat;
}

.bg1 {
  background-image: linear-gradient(76.9deg, #e8bbf2, #8787fd 51.66%, #84caff 109.18%);
}

.bg2 {
  background-image: linear-gradient(45deg, #102eff, #d2379b);
}

.bg3 {
  background-image: linear-gradient(to right, #6a3093, #a044ff);
}

It makes more sense without the custom property because the css custom property isn’t really doing much in that example.

It saves a few characters but unlike your other example you are just redefining the same custom properly over and over again.

1 Like

How was that link issue fixed because it happened to me also?

Is there a proper way to do it that I am unaware of?

Another way is to construct the className string instead of using an array of classNames

document.querySelector("body").classList.add('bg' + Math.floor(Math.random() * classNames.length));

Are custom properties able to be removed from this code?

https://jsfiddle.net/1gkutazq/4/

.vc1 {
  --image-a: radial-gradient(60% 60% at 50% 50%, rgb(255, 255, 0), rgb(0, 0, 0));
}

.vc2 {
  --image-a: radial-gradient(60% 60% at 50% 50%, rgb(40, 0, 115), rgb(0, 0, 0));
}

.vc3 {
  --image-a: linear-gradient(120deg, #155799, #159957);
}

.vc4 {
  --image-a: linear-gradient(120deg, #155799, #159957);
}


.curtain {
  flex: 1 0 0;
  margin: auto;
  max-width: 640px;
  position: relative;
  background-image: var(--image-a);
  overflow: hidden;
}
(function randomVideoCover() {
  const classNames = [
    "vc1",
    "vc2",
    "vc3",
    "vc4",
    "vc5",
    "vc7",
    "vc8",
    "vc9",
    "vc10"
  ];

  const random = Math.floor(Math.random() * classNames.length);
  document.querySelector("body").classList.add(classNames[random]);
}());

Based on his answer to this post (which is essentially exactly the same question), what do you think the correct answer is?

1 Like

I tried, but I think other things need to be changed in the code.

What’s wrong with this line for that purpose?

1 Like