如何使文本显示速度变慢
How to make text display slower?
如何使网页上的任何文本(例如h1
标记)display
变慢,就像CSS3
标记-webkit-transition-duration
/transition-duration
一样?
我没有任何代码可以提供,因为我只是好奇,而且,我在互联网上没有发现任何有用的东西(不能保证我在每一页都搜索过)或"相关"问题。
它需要JavaScript
吗?(如果我不够清楚,我可以举一个例子)。
这完全取决于你想做什么。如果你只想一次fade-in
文本(或其他动画),css3
可能足以处理这个问题。
如果您试图逐个字母地显示文本(例如,在RPG中可以找到),那么您需要代码来迭代字符串,并一次只写一个字母。
以下是javascript
中的一个例子,直接取自对类似问题的回答:
var showText = function (target, message, index, interval) {
if (index < message.length) {
$(target).append(message[index++]);
setTimeout(function () { showText(target, message, index, interval); }, interval);
}
}
从本质上讲,代码查看传递到其中的数据的每个索引(例如,t
,然后是h
,然后是e
),并一次一个字母地将其写入目标。
有关其他详细信息,请参阅链接的答案。
这里有一个关于CSS3动画的好链接。此外,如果你不介意使用插件的话,jQuery为许多动画提供了一些很棒的功能,这些功能超出了CSS
本身的功能。
顺便说一句,当你尝试执行这样的动画时,你应该记住浏览器的兼容性。较旧的浏览器可能会对这类东西有不稳定或不支持,所以你应该有一个后备方案。
编辑:
很抱歉添加另一个链接,但这里有一个很好的实现,可以一次显示一个单词。这个代码直接取自链接的答案,并添加了我的解释:
var textToDisplay = "Even more awesome text inside here, but displayed one word at a time",
$output = $("p");
$("button").click(function() {
var displayInt;
textToDisplay = textToDisplay.split(' '); //split the text variable into an array
$output.empty(); //clear out the $output variable
displayInt = setInterval(function() {
var word = textToDisplay.shift(); //removes the first word ("Even") and sets the word variable to that value
if (word == null) { return clearInterval(displayInt); } //if we're out of words to append
$output.append(word + ' '); //else, add the word and then a space (.split(' ') will not carry over the spaces)
}, 300); //setInterval is delayed 300ms, so a word will be added every 300ms
});
注意这个方法和我之前添加的方法都使用jQuery
($
符号是jQuery
前缀)。如果你点击第二个答案的链接,有一个片段可以让你试用这个代码。
使用CSS3动画可以轻松做到这一点:
h1{
animation:fadeIn 5s;
}
@keyframes fadeIn{
0%{opacity:0;}
80%{opacity:0;}
100%{opacity:1;}
}
<h1>I fade In</h1>
此片段将等待4秒,然后在h1中淡出。
代码说明:
h1{
/*Since we want the h1 to fade in, and stay there, we cannot use animation-delay.*/
animation:fadeIn 5s;
/*Instead, we can add the delay by setting the animation to longer*/
}
@keyframes fadeIn{/*Then, don't actually animate anything for, in this case, 4 seconds*/
0%{opacity:0;}
80%{opacity:0;}
100%{opacity:1;}
}
如果你想一次淡出一个字母,你必须使用JavaScript。
使用javascript与CSS3动画相结合,您可以实现JS Fiddle,其中您用span
包裹每个字母,并根据字母的位置向其添加延迟值的CSS动画,因此您可以逐字母淡入文本:
var $test = document.getElementById('test').innerHTML, $html = '', $i;
for ($i = 0; $i < $test.length; $i++) {
$html += '<span style="animation: foo ' + $i + 's">' + ($test[$i]) + '</span>';
}
document.getElementById('test').innerHTML = $html;
@keyframes foo {
0%, 10% {
opacity: 0;
}
15%, 100% {
opacity: 1;
}
}
<h1 id="test">I am Sample Text</h1>
因此,您可以设置不同属性的动画,而不仅仅是淡入,例如逐个字母平滑地更改颜色JS Fiddle 2
var $test = document.getElementById('test').innerHTML, $html = '', $i;
for($i=0; $i<$test.length; $i++){
$html += '<span style="animation: foo ' +$i+'s">' + ($test[$i]) + '</span>';
}
document.getElementById('test').innerHTML = $html;
document.getElementById('test').style.color = 'green';
@keyframes foo{
0%,10%{color:orange;}
15%,100%{color:green;}
}
<h1 id="test">I am Sample Text</h1>
- 从桌面读取python文件时高亮显示代码
- 如何在HTML / JavaScript中加快本地文件系统中大图像的显示速度
- 这个预加载器工作吗?它不用于显示状态,只是加快了以后可能的图像加载速度
- 当 ajax 请求速度时不显示动画.js
- 如何在互联网速度慢时显示基本的html页面
- 对于连接速度较慢的用户来说,加载图像显示得太晚
- 如何使文本显示速度变慢
- 以非常高的速度显示流数据
- AngularJS动画(显示/隐藏)速度
- 如何从excel文件中输入,并在速度计上显示输出
- Javascript显示更多/显示更少的速度效果
- 我可以以60fps的速度运行angular's digest loop来显示快速计时器吗?
- 在Chrome浏览器中,组合框显示速度太慢,数据量大
- 填写表单的脚本执行速度比onchange()显示表单选项的速度快
- 我怎样才能显示我的进步呢?下载速度
- 图像库显示图像的速度太慢
- 第一个幻灯片图像的显示速度比其他幻灯片图像慢
- 如何计算时间和显示在H-M与12小时时钟,基于距离和速度使用javascript
- Angular js的渲染速度[slow]阻止加载器显示
- 动画旋转器在IE隐藏/显示后以相同的速度旋转