Create a random combination with CSS classes using javascript or jquery

Asked

Viewed 334 times

1

Hey, guys, I was wondering if anyone knows a solution for how to make a combination of css classes using javascript or jquery?

What I would like is to have links and each of these links have several color classes, and after selecting some of the links generate a page with a bg of one color and the font was another, which is always random for each person who click, as this will be used as a theme for managed websites that I am building.

  • Create an array of classes and then use them randomly. But you could/should do this on the I think server. What language do you have on the server? you already have that list of classes?

  • There is no such list no, this is a project I would like to be implementing, I have all the languages on the server but the preference would be in js or jquery if possible

1 answer

3

You can draw the classes from an array. Or rather, two, one for BG and one for the source, so you get some contrast. Example:

var classesBg = ['fundoAzul', 'fundoVermelho', 'fundoVerde'];
var classesTexto = ['textoBranco', 'textoPreto', 'textoCinza'];
function sorteia(arr) {
    var rand = Math.floor(Math.random() * arr.length);
    return arr[rand];
}

document.getElementById('troca').addEventListener('click', function() {
    var classeBg = sorteia(classesBg);
    var classeTexto = sorteia(classesTexto);
    var elemento = document.getElementById('conteudo');
    elemento.className = classeBg + ' ' + classeTexto;
}); 
.fundoAzul {
  background-color: #0000ff;
}
.fundoVermelho {
  background-color: #ff0000;
}
.fundoVerde {
  background-color: #00ff00;
}
.textoBranco {
  color: #ffffff;
}
.textoPreto {
  color: #000000;
}
.textoCinza {
  color: #aaaaaa;
}
<p id="conteudo">Texto texto texto</p>
<button type="button" id="troca">trocar</button>

  • And how could I make for each of the links to generate a palette of different color, romantic type use red and pink colors, happy use yellow and blue tones?

  • Create different arrays for each palette.

  • using this code then causes you to create diverse themes within the colors I stipulate, and that that bg and that font color stay fixed on each theme for example?

  • 1

    Yeah, try playing a little game with that code.

  • VLW man, I’ve been behind this for another week and a half

  • 1

    Dude, the array here is of classes. What you linked is a script that generates random color code, it’s different. If you want to use this script, change the colors of the element directly instead of exchanging classes (which is what you had asked). For example: elemento.style.backgroundColor = randomColor() (include the script earlier as explained on their website).

  • How do I make him open the new page with the combination, because I put the Blank on the button but he opens the page with no class.

  • It’s more complicated than that, and you’re dodging your original question. You can save to a database and read from there, or you can save to the person’s machine (a cookie or localstorage). I suggest opening a separate question about that.

  • Do you know if there’s a way I can put an image and only appear in the "theme" selected? Because I took your code and tripled it making it have 3 types of theme

  • If you put the image in CSS, like background-image, it doesn’t solve?

  • So what I wanted was to have an image in Parallax, as I do Parallax in pure css. For images that won’t have Parallax I used bg even.

  • 1

    Dude, this is going beyond the scope of this question. Try to put a simple example of the problem and post as a separate question.

  • Ah blz, it’s just that since you passed the code I thought you knew some way, but I’m going to ask a question with this, vlw

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.