我正在尝试制作一个每400毫秒更换颜色的渐变。我使用了别人的随机颜色更改器,并将其添加到h1标签中,以便进行更改。当我没有使用渐变时,它可以正常工作,但是一旦我尝试添加渐变,它就完全无法工作。这是我的代码没有渐变:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Word Animation</title>
<style>
body {
background-color: black;
}
h1 {
transition:300ms;
text-align: center;
vertical-align: middle;
font-size: 200px;
font-family: Impact, Arial,serif;
}
</style>
</head>
<body>
<h1 id="h1">RANDOM TEXT</h1>
<script>
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function title() {
document.getElementById("h1").style.color = getRandomColor();
setTimeout(title, 300);
}
title();
</script>
</body>
</html>
这个可以实现,但是它带有渐变效果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Word Animation</title>
<style>
body {
background-color: black;
}
h1 {
transition: 300ms;
text-align: center;
vertical-align: middle;
font-size: 200px;
font-family: Impact, Arial, serif;
background: -webkit-linear-gradient(blue, red);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
</style>
</head>
<body>
<h1 id="h1">RANDOM TEXT</h1>
<script>
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function title() {
document.getElementById("h1").style.background = "-webkit-linear-gradient(", getRandomColor(), ",", getRandomColor();
setTimeout(title, 300);
}
title();
</script>
</body>
</html>
请帮忙,谢谢!