html - 如何在CSS中显示随机句子

标签 html css

我想在加载动画上使用一些句子。

“你减肥了吗?”, “数到10”, “为什么如此严肃?”, “不是你,是我。”

我有这个 css 代码来呈现加载:

.loading .spinner:Before{content:"Discovering new ways of making you wait..."; position:absolute; bottom:50px; text-align:center; left:0; right:0; font-size:23px}

怎样才能让句子随机出现?

非常感谢您的帮助:)

最佳答案

您可以将 content 属性设置为元素的 data-content 属性,然后使用 JavaScript 将数组中的值随机设置为该属性。

const possibleContent = ["Have you lost weight?", "Just count to 10", "Why so serious?", "It's not you. It's me."];

spinner.setAttribute('data-content', possibleContent[Math.floor(Math.random() * possibleContent.length)]);
.loading .spinner:Before {
  content: attr(data-content);
  position: absolute;
  bottom: 50px;
  text-align: center;
  left: 0;
  right: 0;
  font-size: 23px
}
<div class="loading"><div class="spinner" id="spinner"></div></div>

关于html - 如何在CSS中显示随机句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68764739/

相关文章:

html - 将子 div 标签水平放置,尽管父 div 标签宽度

javascript - 如何从js中的css转换返回值?

javascript - 使用 JavaScript 或 Jquery 链接到过滤后的 html

css - 如果字体超过给定大小,则更改 css 属性

CSS 样式定义列表 <dl>

css - Chrome 输入和文本区域填充很烂

javascript - 具有固定大小的空相对 div?

javascript - 如何将内容添加到模态对话框 (kbModal.js)

html - 箭头在 CSS 中无法正确显示

javascript - Html 获取innerHTML