HTML 随机化内联文本块 space 和颜色
HTML randomize inline text block space and color
目前我有一些文字飞过背景视频。文本颜色和两个文本块之间的 space 是固定的。我想要实现的是在 运行 时间内为每个文本块随机化颜色和 space。我相信它可以使用 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())
}
但不知何故这不起作用。
改变颜色使用
for (var i = 0; i < comments.length; i++) {
comments[i].style.color = getRandomColor();
}
编辑:
随机化 space 使用
span.style.paddingRight = getRandomSpace() + "px";
Edit2: 正如@dandavis 在上面的评论中指出的那样,"instead of random colors, it would look a LOT nicer to pre-bake 5-10 pairs of good readable colors and randomly choose from the pairs"
目前我有一些文字飞过背景视频。文本颜色和两个文本块之间的 space 是固定的。我想要实现的是在 运行 时间内为每个文本块随机化颜色和 space。我相信它可以使用 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())
}
但不知何故这不起作用。
改变颜色使用
for (var i = 0; i < comments.length; i++) {
comments[i].style.color = getRandomColor();
}
编辑: 随机化 space 使用
span.style.paddingRight = getRandomSpace() + "px";
Edit2: 正如@dandavis 在上面的评论中指出的那样,"instead of random colors, it would look a LOT nicer to pre-bake 5-10 pairs of good readable colors and randomly choose from the pairs"