エフェクト画像
アプリケーションシナリオ
あまり役に立たないです。インターネットでも同様の効果が見られました。理解できない40行または50行のコードを書いたので、簡単にしようとしました。
html
<h2 id="text-box"></h2>
<!--一行也是代码-->
css
h2 {
width: 800px;
line-height: 40px;
border-bottom: 1px solid;
margin: 200px auto;
font-size: 24px;
}
.animate {
display: inline-block;
padding: 0 5px;
vertical-align: 3px;
font-size: 20px;
font-weight: normal;
}
.animate.on {
animation: fade 1.5s infinite forwards;
}
@keyframes fade {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
js
let textBox = $('#text-box');
let index = 0;
let str = 'Welcome to my website!';
let len = str.length;
function input() {
textBox.html(str.substr(0, index) + '<span class="animate">|</span>');
setTimeout(function() {
index++;
if(index === len + 1) {
$('.animate').addClass('on');
return;
}
input();
}, Math.random() * 600)
console.log(index);
}
input();
実現原理
タイプライターのような欲求不満の感覚は、文字列の傍受と組み合わせたタイマーによって実現され、インデックスは再帰によって蓄積されます。これは、最初の1秒で1バイトをインターセプトし、2番目の秒で2バイトをインターセプトするのと同じです...タイマー時間は乱数であり、シミュレートされたタイピングの方が一時停止が優れています。再帰呼び出しに終了ループ条件を追加し、終了前にアニメーションを開始して、カーソルのビートをシミュレートします。