gpt4 book ai didi

javascript - HTML 随机化内联文本 block 空间和颜色

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:07 25 4
gpt4 key购买 nike

目前我有一些文字在背景视频上飞舞。文本颜色和两个文本 block 之间的间距是固定的。我想要实现的是在运行时随机化每个文本 block 的颜色和空间。我相信它可以使用 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();
}

编辑:随机化空间使用

span.style.paddingRight = getRandomSpace() + "px";

Edit2:正如@dandavis 在上面的评论中指出的那样,“预烘焙 5-10 对可读性好的颜色并从中随机选择,而不是随机颜色,看起来会好很多对"

关于javascript - HTML 随机化内联文本 block 空间和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31582236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com