CSSめっちゃ便利ですよね。
最近はアニメーションを付けれちゃう!
今回は、テレビが故障したときのようなエフェクト、グリッチエフェクトをテキストに付けてみます。
CSSだけでテキストにグリッチエフェクトを付ける
では早速テキストにグリッチエフェクトをtけるための準備をしていきます。
グリッチエフェクトを付けるテキストを用意
テキストを用意する際に、「data-text」で同じテキストを入力しておきます。
<div class="glitch-effect" data-text="340.29m/s">340.29m/s</div>
グリッチエフェクトを付けるSCSSを用意
CSSアニメーションを使用し、書いていきます。
SCSSを使用していない方は、下のCSSをご確認ください。
// アニメーション設定
@keyframes glitch {
$steps: 20;
@for $i from 0 through $steps {
#{percentage($i*(1/$steps))} {
clip: rect(random(100) + px, 9999px, random(100) + px, 0);
}
}
}
@keyframes glitch-2 {
$steps: 20;
@for $i from 0 through $steps {
#{percentage($i*(1/$steps))} {
clip: rect(random(100) + px, 9999px, random(100) + px, 0);
}
}
}
.glitch-effect {
color: white;
font-size: 100px;
position: relative;
width: 400px;
margin: 0 auto;
}
.glitch-effect:after {
content: attr(data-text);
position: absolute;
left: 2px;
text-shadow: -1px 0 red;
top: 0;
color: white;
background: black;
overflow: hidden;
clip: rect(0, 900px, 0, 0);
animation: glitch 2s infinite linear alternate-reverse;
}
.glitch-effect:before {
content: attr(data-text);
position: absolute;
left: -2px;
text-shadow: 1px 0 blue;
top: 0;
color: white;
background: black;
overflow: hidden;
clip: rect(0, 900px, 0, 0);
animation: glitch-2 3s infinite linear alternate-reverse;
}
<CSSの場合>
body {
background: black;
}
@-webkit-keyframes glitch {
0% {
clip: rect(17px, 9999px, 62px, 0);
}
5% {
clip: rect(43px, 9999px, 46px, 0);
}
10% {
clip: rect(87px, 9999px, 1px, 0);
}
15% {
clip: rect(11px, 9999px, 13px, 0);
}
20% {
clip: rect(9px, 9999px, 87px, 0);
}
25% {
clip: rect(8px, 9999px, 66px, 0);
}
30% {
clip: rect(87px, 9999px, 1px, 0);
}
35% {
clip: rect(3px, 9999px, 37px, 0);
}
40% {
clip: rect(54px, 9999px, 8px, 0);
}
45% {
clip: rect(35px, 9999px, 49px, 0);
}
50% {
clip: rect(38px, 9999px, 94px, 0);
}
55% {
clip: rect(66px, 9999px, 56px, 0);
}
60% {
clip: rect(10px, 9999px, 70px, 0);
}
65% {
clip: rect(100px, 9999px, 46px, 0);
}
70% {
clip: rect(24px, 9999px, 70px, 0);
}
75% {
clip: rect(47px, 9999px, 68px, 0);
}
80% {
clip: rect(67px, 9999px, 97px, 0);
}
85% {
clip: rect(5px, 9999px, 13px, 0);
}
90% {
clip: rect(84px, 9999px, 20px, 0);
}
95% {
clip: rect(53px, 9999px, 39px, 0);
}
100% {
clip: rect(57px, 9999px, 79px, 0);
}
}
@keyframes glitch {
0% {
clip: rect(17px, 9999px, 62px, 0);
}
5% {
clip: rect(43px, 9999px, 46px, 0);
}
10% {
clip: rect(87px, 9999px, 1px, 0);
}
15% {
clip: rect(11px, 9999px, 13px, 0);
}
20% {
clip: rect(9px, 9999px, 87px, 0);
}
25% {
clip: rect(8px, 9999px, 66px, 0);
}
30% {
clip: rect(87px, 9999px, 1px, 0);
}
35% {
clip: rect(3px, 9999px, 37px, 0);
}
40% {
clip: rect(54px, 9999px, 8px, 0);
}
45% {
clip: rect(35px, 9999px, 49px, 0);
}
50% {
clip: rect(38px, 9999px, 94px, 0);
}
55% {
clip: rect(66px, 9999px, 56px, 0);
}
60% {
clip: rect(10px, 9999px, 70px, 0);
}
65% {
clip: rect(100px, 9999px, 46px, 0);
}
70% {
clip: rect(24px, 9999px, 70px, 0);
}
75% {
clip: rect(47px, 9999px, 68px, 0);
}
80% {
clip: rect(67px, 9999px, 97px, 0);
}
85% {
clip: rect(5px, 9999px, 13px, 0);
}
90% {
clip: rect(84px, 9999px, 20px, 0);
}
95% {
clip: rect(53px, 9999px, 39px, 0);
}
100% {
clip: rect(57px, 9999px, 79px, 0);
}
}
@-webkit-keyframes glitch-2 {
0% {
clip: rect(19px, 9999px, 94px, 0);
}
5% {
clip: rect(81px, 9999px, 97px, 0);
}
10% {
clip: rect(58px, 9999px, 48px, 0);
}
15% {
clip: rect(16px, 9999px, 47px, 0);
}
20% {
clip: rect(54px, 9999px, 27px, 0);
}
25% {
clip: rect(93px, 9999px, 1px, 0);
}
30% {
clip: rect(55px, 9999px, 19px, 0);
}
35% {
clip: rect(34px, 9999px, 38px, 0);
}
40% {
clip: rect(82px, 9999px, 79px, 0);
}
45% {
clip: rect(45px, 9999px, 44px, 0);
}
50% {
clip: rect(67px, 9999px, 59px, 0);
}
55% {
clip: rect(83px, 9999px, 6px, 0);
}
60% {
clip: rect(95px, 9999px, 70px, 0);
}
65% {
clip: rect(74px, 9999px, 12px, 0);
}
70% {
clip: rect(21px, 9999px, 2px, 0);
}
75% {
clip: rect(1px, 9999px, 23px, 0);
}
80% {
clip: rect(11px, 9999px, 91px, 0);
}
85% {
clip: rect(81px, 9999px, 10px, 0);
}
90% {
clip: rect(22px, 9999px, 98px, 0);
}
95% {
clip: rect(68px, 9999px, 59px, 0);
}
100% {
clip: rect(79px, 9999px, 12px, 0);
}
}
@keyframes glitch-2 {
0% {
clip: rect(19px, 9999px, 94px, 0);
}
5% {
clip: rect(81px, 9999px, 97px, 0);
}
10% {
clip: rect(58px, 9999px, 48px, 0);
}
15% {
clip: rect(16px, 9999px, 47px, 0);
}
20% {
clip: rect(54px, 9999px, 27px, 0);
}
25% {
clip: rect(93px, 9999px, 1px, 0);
}
30% {
clip: rect(55px, 9999px, 19px, 0);
}
35% {
clip: rect(34px, 9999px, 38px, 0);
}
40% {
clip: rect(82px, 9999px, 79px, 0);
}
45% {
clip: rect(45px, 9999px, 44px, 0);
}
50% {
clip: rect(67px, 9999px, 59px, 0);
}
55% {
clip: rect(83px, 9999px, 6px, 0);
}
60% {
clip: rect(95px, 9999px, 70px, 0);
}
65% {
clip: rect(74px, 9999px, 12px, 0);
}
70% {
clip: rect(21px, 9999px, 2px, 0);
}
75% {
clip: rect(1px, 9999px, 23px, 0);
}
80% {
clip: rect(11px, 9999px, 91px, 0);
}
85% {
clip: rect(81px, 9999px, 10px, 0);
}
90% {
clip: rect(22px, 9999px, 98px, 0);
}
95% {
clip: rect(68px, 9999px, 59px, 0);
}
100% {
clip: rect(79px, 9999px, 12px, 0);
}
}
.glitch-effect {
color: white;
font-size: 100px;
position: relative;
width: 400px;
margin: 0 auto;
}
.glitch-effect:after {
content: attr(data-text);
position: absolute;
left: 2px;
text-shadow: -1px 0 red;
top: 0;
color: white;
background: black;
overflow: hidden;
clip: rect(0, 900px, 0, 0);
animation: glitch 2s infinite linear alternate-reverse;
}
.glitch-effect:before {
content: attr(data-text);
position: absolute;
left: -2px;
text-shadow: 1px 0 blue;
top: 0;
color: white;
background: black;
overflow: hidden;
clip: rect(0, 900px, 0, 0);
animation: glitch-2 3s infinite linear alternate-reverse;
}
/*# sourceMappingURL=common.css.map */
こんな感じで、壊れたテレビのようなグリッチエフェクトを簡単に実装できます!
試してみてね\(^o^)/
ちなみに、「340.29m/s」ってなんぞやって思った方はこちら。
ABOUT ME