当前位置:Gxlcms > css > 如何使用纯CSS实现文字断开的动画效果(附源码)

如何使用纯CSS实现文字断开的动画效果(附源码)

时间:2021-07-01 10:21:17 帮助过:7人阅读

本篇文章给大家带来的内容是关于如何使用纯CSS实现文字断开的动画效果(附源码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

3894516576-5b738d082c3fc_articlex.gif

源代码下载

https://github.com/comehope/front-end-daily-challenges/tree/master/012-broken-text-effects

代码解读

定义 dom,只有一个元素,元素有一个 data-text 属性,属性值等于元素内的文本:

  1. <div class="text" data-text="BREAK">BREAK</div>

居中显示:

  1. html, body {
  2. height: 100%;
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. }

设置渐变背景色:

  1. body {
  2. background: linear-gradient(brown, sandybrown);
  3. }

设置文本的字体字号:

  1. .text {
  2. font-size: 5em;
  3. font-family: "arial black";
  4. }

利用伪元素增加文字:

  1. .text {
  2. position: relative;
  3. }
  4. .text::before,
  5. .text::after {
  6. content: attr(data-text);
  7. position: absolute;
  8. top: 0;
  9. left: 0;
  10. color: lightyellow;
  11. }

设置左侧文字的遮罩:

  1. .text::before {
  2. background-color: darkgreen;
  3. clip-path: polygon(0 0, 60% 0, 30% 100%, 0 100%);
  4. }

设置右侧文字的背景和遮罩:

  1. .text::after {
  2. background-color: darkblue;
  3. clip-path: polygon(60% 0, 100% 0, 100% 100%, 30% 100%);
  4. }

当鼠标划过时,遮罩的文字分别向两侧偏移:

  1. .text::before,
  2. .text::after {
  3. transition: 0.2s;
  4. }
  5. .text:hover::before {
  6. left: -0.15em;
  7. }
  8. .text:hover::after {
  9. left: 0.15em;
  10. }

隐藏辅助元素,包括原始文字和伪元素的背景色:

  1. .text {
  2. color: transparent;
  3. }
  4. .text::before {
  5. /*background-color: darkgreen;*/
  6. }
  7. .text::after {
  8. /*background-color: darkblue;*/
  9. }

两侧文字增加歪斜效果:

  1. .text:hover::before {
  2. transform: rotate(-5deg);
  3. }
  4. .text:hover::after {
  5. transform: rotate(5deg);
  6. }

微调文字的高度:

  1. .text:hover::before {
  2. top: -0.05em;
  3. }
  4. .text:hover::after {
  5. top: 0.05em;
  6. }

大功告成!

相关推荐:

如何使用CSS实现渐变色动画边框的效果(附代码)

如何使用CSS和混色模式实现loader动画效果(附代码)

以上就是如何使用纯CSS实现文字断开的动画效果(附源码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行