目前,我在背景视频中播放了一些文字。文本颜色和两个文本块之间的空间是固定的。我想要实现的是在运行时随机化每个文本块的颜色和空间。我相信它可以使用javascript实现,但我不确定如何。这是我当前的HTML和CSS代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"/>
<link rel="stylesheet" href="StyleSheet.css"/>
</head>
<body>
<iframe width="100%" height="315" src="http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1"></iframe>
<div class="marquee">
<span>yay 1st comment</span>
<span> </span>
<span>lol</span>
<span> </span>
<span>hacking !!???</span>
<span> </span>
<span>this video is hacked!!!!!</span>
<span> </span>
<span>this is THE 4th dimension hahaha</span>
<span> </span>
<span>Let's use this feature more wisely now</span>
<span> </span>
<span>Okay</span>
<span> </span>
</div>
</body>
</html>
样式表
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
}
.marquee {
color: white;
position: absolute;
margin-top: 20px;
top: 0;
animation: marquee 20s linear infinite;
z-index: 1;
display: inline-block;
}
span:before{
content:" ";
display:inline-block;
width:32px;
}
/* Make it move */
@keyframes marquee {
0% { text-indent: 100em }
100% { text-indent: -100em }
}
我应该在javascript中做一些事情吗?
var comments = document.getElementsByTagName("span");
for (var i = 0; i < comments.length; i++) {
comments[i].setAttribute("color", getRandomColor())
}
但不知怎的,这不起作用。
答案 0 :(得分:1)
更改颜色使用
for (var i = 0; i < comments.length; i++) {
comments[i].style.color = getRandomColor();
}
修改强> 随机化空间使用
span.style.paddingRight = getRandomSpace() + "px";
Edit2:正如@dandavis在上面的评论中所指出的那样,“而不是随机颜色,预先烘烤5-10对良好的可读颜色并随机选择对“