samedi 10 décembre 2022

how to make ::selection text background color random from selected colors

I want to make it so that every time someone highlights text on my sight, the highlight color is different. I have predefined colors already chosen and I will also give you the code I currently have.

currently with my code it just runs through each one in a sequence. I need to figure out how to make it so that it will give you a random color from my array.

<script> 
window.colors = ['#F78DA7','#CF2E2E','#FF6900','#FCB900','#7BDCB5','#00D084','#8ED1FC','#0693E3','#9B51E0']
window.order = 0

document.addEventListener("mousedown", function() {
document.head.insertAdjacentHTML('beforeend', '<style>::selection { background-color:' + window.colors[window.order] + '} ::-moz-selection { background-color:' + window.colors[window.order] + '} ::-o-selection { background-color:' + window.colors[window.order] + '} ::-ms-selection { background-color:' + window.colors[window.order] + '} ::-webkit-selection { background-color:' + window.colors[window.order] + '}</style>');
window.order = window.order + 1
if (window.order > window.colors.length - 1){
window.order = 0
}
});
 </script>



Aucun commentaire:

Enregistrer un commentaire