Вы никогда не сбрасываете colortag1
или colortag2
, как заметил Сантия go Касас Рей. Это будет легче заметить с более организованным кодом. Особенно беспорядок может возникнуть, если возиться с Math.random (). Я использую rando. js, чтобы сделать мою случайность более простой и читаемой. Вы можете получить случайную шестнадцатеричную строку с помощью rando. js вот так:
function randomHex(){
var hex = "#";
for(var i = 0; i < 3; i++) hex += ("0" + rando(255).toString(16)).slice(-2);
return hex;
}
console.log(randomHex());
<script src="https://randojs.com/1.0.0.js"></script>
Это может быть даже однострочный, если вы хотите:
console.log( "#" + ("0" + rando(255).toString(16)).slice(-2) + ("0" + rando(255).toString(16)).slice(-2) + ("0" + rando(255).toString(16)).slice(-2) );
<script src="https://randojs.com/1.0.0.js"></script>
Если мы применим это к вашему js, получится:
var css = document.querySelector("h3");
var color1 = document.querySelector(".color1");
var color2 = document.querySelector(".color2");
var body = document.getElementById('gradient')
var random1 = document.getElementById("random1");
function randomHex() {
var hex = "#";
for (var i = 0; i < 3; i++) hex += ("0" + rando(255).toString(16)).slice(-2);
return hex;
}
function changeToColors(hex1, hex2){
body.style.background = "linear-gradient(to right," + hex1 + "," + hex2 + ")";
css.textContent = body.style.background;
}
function changeToInputColors() {
changeToColors(color1.value, color2.value);
}
function changeToRandomColors() {
changeToColors(randomHex(), randomHex());
}
changeToInputColors();
color1.addEventListener("input", changeToInputColors);
color2.addEventListener("input", changeToInputColors);
random1.addEventListener("click", changeToRandomColors);
body {
font: 'Raleway', sans-serif;
color: rgba(0, 0, 0, .5);
text-align: center;
text-transform: uppercase;
letter-spacing: .5em;
top: 15%;
background: linear-gradient(to right, red, yellow);
/* Standard syntax */
}
h1 {
font: 600 3.5em 'Raleway', sans-serif;
color: rgba(0, 0, 0, .5);
text-align: center;
text-transform: uppercase;
letter-spacing: .5em;
width: 100%;
}
h3 {
font: 900 1em 'Raleway', sans-serif;
color: rgba(0, 0, 0, .5);
text-align: center;
text-transform: none;
letter-spacing: 0.01em;
}
button {
color: rgba(0, 0, 0, .5);
font: 800 1em 'Raleway', sans-serif;
background-color: transparent;
border: none;
overflow: hidden;
outline: none;
cursor:pointer;
text-decoration:underline;
}
<!DOCTYPE html>
<html>
<head>
<title>Gradient background</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://randojs.com/1.0.0.js"></script>
</head>
<body id="gradient">
<h1>Backround Generator</h1>
<input class="color1" type="color" name="color1" value="#FF0000">
<input class="color2" type="color" name="color2" value="#FFFF00">
<h2>Current CSS Background</h2>
<button id="random1">Random Colors</button>
<h3></h3>
</body>
</html>
Если вы хотите использовать этот код, просто не забудьте включить эту строку в тег заголовка вашего html документа:
<script src="https://randojs.com/1.0.0.js"></script>