妙用 Background 实现花式文字效果

开发 前端
本文将讲解如何利用 background 系列属性,巧妙的实现一些花式的文字效果。通过 background-size 与 background-position 实现酷炫的文字下划线效果。

[[426213]]

Hello 大家好,我是 Coco。本文将讲解如何利用 background 系列属性,巧妙的实现一些花式的文字效果。通过本文,你将可以学到:

  • 通过 background-size 与 background-position 实现酷炫的文字下划线效果
  • 通过 background-size 与 background-position 以及 background-clip 实现文字逐个渐现的效果

起因

写本文的动机是在于,某天,被这样一个标题所吸引 -- 10 Masterfully Designed Websites[1],其中列举了 10 个极具创意的 Web 网站。

其中一个 Red Bull Racing[2] 网站,是介绍关于 F1 红牛车队的主页。其中有这样一个非常有意思的 Hover 动画效果:

图片

这个文字的 hover 出现效果,看似简单,其实想要完全实现它,仅仅依靠 CSS 是非常复杂的,其中一个比较难的地方在于 -- 如何让一个效果,逐渐作用给整段文字中的部分,而不是一次将整个效果赋予整段文本。

利用 background 实现文字的下划线效果

到这里,我想起了之前在这篇文章中 -- CSS 文字装饰 text-decoration & text-emphasis[3],我介绍的一种 使用 background 模拟下划线的效果。

看个简单的 DEMO,使用 background 模拟文字的下划线效果:

  1. <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. <a>Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam</a>, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</p> 
  1. p { 
  2.     width: 600px; 
  3.     font-size: 24px; 
  4.     color: #666; 
  5. a { 
  6.     background: linear-gradient(90deg, #0cc, #0cc); 
  7.     background-size: 100% 3px; 
  8.     background-repeat: no-repeat; 
  9.     background-position: 100% 100%; 
  10.     color: #0cc; 

使用 background 模拟文字的下划线效果,效果图如下:

又或者,使用 background 模拟虚线下划线:

  1. a { 
  2.     background: linear-gradient(90deg, #0cc 50%, transparent 50%, transparent 1px); 
  3.     background-size: 10px 2px; 
  4.     background-repeat: repeat-x; 
  5.     background-position: 100% 100%; 

CodePen Demo -- 使用 background 模拟下划线与虚线下划线[4]

当然这个是最基础的,巧妙的运用 background 的各种属性,我们实现各种有意思的效果。

巧妙改变 background-size 与 background-position 实现文字 hover 动效

这里,通过巧妙改变 background-size 与 background-position 属性,我们可以实现一些非常有意思的文字 hover 效果。

先看这样一个 Demo,核心代码作用于被<p>标签包裹的 <a>标签之上:

  1. <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. <a>Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam</a>, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</p> 
  1. a { 
  2.     background: linear-gradient(90deg, #ff3c41, #fc0, #0ebeff); 
  3.     background-size: 0 3px; 
  4.     background-repeat: no-repeat; 
  5.     background-position: 0 100%; 
  6.     transition: 1s all
  7.     color: #0cc; 
  8.  
  9. a:hover { 
  10.     background-size: 100% 3px; 
  11.     color: #000; 

我们虽然,设定了 background: linear-gradient(90deg, #ff3c41, #fc0, #0ebeff),但是一开始默认它的 background-size: 0 3px,也就是一开始是看不到下划线的,当 hover 的时候,改变 background-size: 100% 3px,这个时候,就会有一个 0 3px 到 100% 3px 的变换,也就是一个从无到有的伸展效果。

看看最后的效果:

图片

由于设定的 background-position 是 0 100%,如果,设定的 background-position 是 100% 100%,我们可以得到一个反向的效果:

  1. // 其他都保持一致,只改变 background-position,从 0 100% 改为 100% 100% 
  2. a { 
  3.     ... 
  4.     background-position: 100% 100%; 
  5.     ... 

再看看效果,你可以对比着上面的动图看看具体的差异点在哪:

图片

CodePen Demo -- background underline animation[5]

OK,如果我们使用 background 实现两条重叠的下划线,再利用上述的两个不同的 background-position 值,我们就可以得到一个更有意思的下划线 hover 效果。

CSS 代码示意,注意看两条使用 background 模拟的下划线的 background-position 的值是不一样的:

  1. a { 
  2.     background:  
  3.         linear-gradient(90deg, #0cc, #0cc), 
  4.         linear-gradient(90deg, #ff3c41, #fc0, #8500d8); 
  5.     background-size: 100% 3px, 0 3px; 
  6.     background-repeat: no-repeat; 
  7.     background-position: 100% 100%, 0 100%; 
  8.     transition: 0.5s all
  9.     color: #0cc; 
  10. a:hover { 
  11.     background-size: 0 3px, 100% 3px; 
  12.     color: #000; 

可以得到这样一种效果,其实每次 hover, 都有两条下划线在移动:

CodePen Demo -- background underline animation[6]

通过 background-size 与 background-position 配合 background-clip 实现文字的渐现

上述一大段都在围绕 -- 下划线。

回归到本文一开始提到的 Gif 效果,我们能否实现在一段文字中,实现文字的渐现效果呢?

上述技巧利用的是 background,那么 background 背景色能否改变文字的颜色的?答案是可以的,只需要借助 background-clip。

我们稍微改造下代码,实现利用 background-clip 实现 hover 的时候部分文字逐渐改变颜色:

  1. <p> 
  2. Lorem ipsum dolor sit amet consectetur adipisicing elit.  
  3. <a>Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, </a> 
  4. molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero. 
  5. </p> 
  1. p { 
  2.     color: #666; 
  3.     cursor: pointer; 
  4.  
  5. a {     
  6.     background: linear-gradient(90deg, #fc0, #fc0); 
  7.     background-size: 0 100px; 
  8.     background-repeat: no-repeat; 
  9.     background-position: 0 100%; 
  10.     background-clip: text; 
  11.     transition: .6s all linear; 
  12.  
  13. p:hover a { 
  14.     background-size: 100% 100%; 
  15.     color: transparent; 

看看效果,通过 background-clip: text 的遮罩裁剪,我们将 background: linear-gradient(90deg, #fc0, #fc0) 背景色作用给了文字,同时利用 color: transparent 让文字展示出背景色的色值:

图片

CodePen Demo -- background-size 与 background-position 以及 background-clip 实现文字逐个渐现[7]

当然,稍微对上述代码变形,我们就可以演化出几种不同的效果。

实现整段文字的渐现 - 从透明到出现

第一种就是从透明到有颜色,逐渐展现,这里我们只需要让 color 一直是 transparent 即可(下述效果借助了一个按钮去触发效果):

  1. <div class="button">Button</div> 
  2. <p><a>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</a></p> 
  1. a {     
  2.     background: linear-gradient(90deg, #fc0, #fc0); 
  3.     background-size: 0 100px; 
  4.     background-repeat: no-repeat; 
  5.     background-position: 0 100%; 
  6.     color: transparent; 
  7.     background-clip: text; 
  8. .button:hover ~ p a { 
  9.     transition: .8s all linear; 
  10.     background-size: 0 100px, 100% 100%; 

效果如下:

图片

实现整段文字的渐现 - 从一种颜色到另外一种颜色

还可以实现文字从一种颜色到另外一种颜色的逐个转变,只需要添加多一层 background-image 渐变。

  1. <div class="button">Button</div> 
  2. <p><a>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</a></p> 
  1. a {     
  2.     background:  
  3.         linear-gradient(90deg, #999, #999), 
  4.         linear-gradient(90deg, #fc0, #fc0); 
  5.     background-size: 100% 100%, 0 100px; 
  6.     background-repeat: no-repeat; 
  7.     background-position: 100% 100%, 0 100%; 
  8.     color: transparent; 
  9.     background-clip: text; 
  10. .button:hover ~ p a { 
  11.     transition: .8s all linear; 
  12.     background-size: 0 100px, 100% 100%; 

这里需要解释一下,虽然设置了 color: transparent,但是文字默认还是有颜色的,默认的文字颜色,是由第一层渐变赋予的 background: linear-gradient(90deg, #999, #999), linear-gradient(90deg, #fc0, #fc0),也就是这一层:linear-gradient(90deg, #999, #999)。

当 hover 触发时,linear-gradient(90deg, #999, #999) 这一层渐变逐渐消失,而另外一层 linear-gradient(90deg, #fc0, #fc0)` 逐渐出现,借此实现上述效果。

CodePen -- background-clip 文字渐现效果[8]

简单模拟题图效果

这里,我们简单利用这个技巧模拟一下文章一开始列出的 Gif 的效果:

图片

这个效果原作者的技巧是:

  1. 将每个单词作为一个对象,包裹一个特殊的 class
  2. 利用 animation-delay 将动画逐渐赋予每个单词

这里,我们将整段文本统一处理,简单还原:

  1. <div class="button">Button</div> 
  2. <p><a>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</a></p> 
  1. /** 动画核心 background、line-height、opacity **/ 
  2. a {     
  3.     background:  
  4.         linear-gradient(90deg, #ff5722, #ff5722), 
  5.         linear-gradient(90deg, #aaa, #aaa); 
  6.     background-size: 100% 100%, 0 100px; 
  7.     background-repeat: no-repeat; 
  8.     background-position: 100% 100%, 0 100%; 
  9.     cursor: pointer; 
  10.     color: transparent; 
  11.     background-clip: text; 
  12.     line-height: 3; 
  13.     opacity: 0; 
  14. .button:hover ~ p a { 
  15.     transition: 1.2s background .3s ease-out, .8s line-height ease-out, .6s opacity ease-in
  16.     background-size: 0 100px, 100% 100%; 
  17.     color: transparent; 
  18.     line-height: 1; 
  19.     opacity: 1; 
  20. / ** 简单控制半透明黑色遮罩出现 **/ 
  21. a::before { 
  22.     content: ""
  23.     position: fixed; 
  24.     background: rgba(0, 0, 0, .8); 
  25.     top: 0; 
  26.     left: 0; 
  27.     right: 0; 
  28.     bottom: 0; 
  29.     z-index: -1; 
  30.     transition: .3s all linear; 
  31.     opacity: 0; 
  32. .button:hover ~ p a::before { 
  33.     opacity: 1; 

 效果如下:

图片

可以看到,由于是整体控制整段文本,效果上没有逐个单词控制的好,但是优点是代码量非常少。对于一些简单卡片类的 hover 场景,足以。

background-image、background-clip 实现文字渐现效果[9]

完美还原题图效果

当然,题图效果使用纯 CSS 也是不在话下的。只不过就不是简单能够统一处理的了。

这里,我们需要对每一个单词进行精细化的处理,并且使用每个单词的伪元素进行额外的动画。

简单的结构如下:

  1. <div class="button">Button</div> 
  2. <div class="g-wrap"></div> 
  3. <p> 
  4.     <span data-text="Lorem">Lorem</span>  
  5.     <span data-text="ipsum">ipsum</span>  
  6.     <span data-text="dolor">dolor</span>  
  7.     <span data-text="sit">sit</span>  
  8.     <span data-text="amet">amet</span>  
  9.     // ... 类似结构      
  10. </p> 

可以看到,每个单词都被 包裹,并且添加了 data-text,方便伪元素拿到当前单词。

接下来,就是设定动画,并且通过顺序给每个 添加相应递增的 animation-delay 以实现没个单词动画的差异性。核心的伪代码如下:

  1. p { 
  2.     position: relative
  3.     width: 500px; 
  4.     overflow: hidden; 
  5.  
  6. p span { 
  7.     position: relative
  8.     display: inline-block; 
  9.     opacity: 0; 
  10.     transform: translateY(15px) translateZ(0); 
  11.     transition-property: transform, opacity; 
  12.     transition-duration: .3s, .2s; 
  13.  
  14. .button:hover ~ p span { 
  15.     opacity: 1; 
  16.     color: #ddd; 
  17.     transform: translateY(0) translateZ(0); 
  18.     transition-duration: 1s, .2s; 
  19.  
  20. p span:after
  21. p span:before { 
  22.     position: absolute
  23.     content: attr(data-text); 
  24.     top: 0; 
  25.     left: 0; 
  26.     z-index: 1; 
  27.     transform: translateZ(0); 
  28.  
  29. p span:after { 
  30.     color: #e62541; 
  31.     transition-property: opacity; 
  32.     transition-duration: .1s; 
  33.  
  34. .button:hover ~ p span:after { 
  35.     opacity: 0; 
  36.     transition-property: opacity; 
  37.     transition-duration: .4s; 
  38.  
  39. @for $i from 1 to 21 { 
  40.     p span:nth-child(#{$i}) { 
  41.         transition-delay: #{$i * 0.04}s; 
  42.          
  43.         &::after { 
  44.             transition-delay: #{$i * 0.04 + 0.2}s; 
  45.         } 
  46.     } 

其实动画本身不太复杂,主要讲两点:

hover 状态下和非 hover 状态下的 transition-duration 是不一样的,是因为取消 hover 过程中,动画消失过程的时间通常是要求更短的;

借助了 SASS 的循环 @for $i from 1 to 21 {} 递增给每个 span 和它的伪元素添加了递增的 transition-delay。

最终,我们可以得到如下的结果:

完整的代码,你可以猛戳 -- CSS 灵感 - 利用 animation-delay 实现文字渐现效果[10]

最后

好了,本文到此结束,希望对你有帮助 :)

参考资料

[1]10 Masterfully Designed Websites:

https://medium.com/@MonteVerdeVT/10-masterfully-designed-websites-f999112e2fa9

[2]Red Bull Racing:

https://thenewmobileworkforce.imm-g-prod.com/back-at-hq

[3]CSS 文字装饰 text-decoration & text-emphasis:

https://github.com/chokcoco/iCSS/issues/103

[4]CodePen Demo -- 使用 background 模拟下划线与虚线下划线:

https://codepen.io/Chokcoco/pen/YzNQKwm

[5]CodePen Demo -- background underline animation:

https://codepen.io/Chokcoco/pen/QWdgLwp

[6]CodePen Demo -- background underline animation:

https://codepen.io/Chokcoco/pen/MWJogjQ

[7]CodePen Demo -- background-size 与 background-position 以及 background-clip 实现文字逐个渐现:

https://codepen.io/Chokcoco/pen/qBjmvdq?editors=1100

[8]CodePen -- background-clip 文字渐现效果:

https://codepen.io/Chokcoco/pen/XWgpyqz

[9]background-image、background-clip 实现文字渐现效果:

https://codepen.io/Chokcoco/pen/abwWMJm

[10]CSS 灵感 - 利用 animation-delay 实现文字渐现效果:

https://csscoco.com/inspiration/#/./animation/animation-delay-control-text-effect

 

责任编辑:姜华 来源: iCSS前端趣闻
相关推荐

2023-05-18 09:25:20

background花式文字效果

2010-09-13 14:09:35

CSS文字

2022-07-19 06:20:47

CSSbackground

2021-09-28 08:26:06

CSS 技巧文字镂空波浪

2023-12-04 08:06:41

CSS文字效果

2024-01-30 09:21:29

CSS文字效果文字装饰

2022-03-31 07:46:17

CSS动画技巧

2021-09-30 08:25:28

CSS 技巧酷炫线条光影

2023-05-26 07:08:05

CSS模糊实现文字

2021-08-30 06:20:39

CSS 技巧3D 效果

2021-11-09 08:30:48

CSS 技巧巧用滤镜

2023-12-25 12:57:00

树形结构CSScounters

2024-03-20 09:40:27

动画技巧CSS逐帧动画

2009-11-10 15:07:11

VB.NET窗体

2011-07-08 10:15:15

IPhone 动画

2022-04-13 07:52:19

VLAN 技术物理局域网虚拟局域网

2009-07-30 09:42:29

CSS实现文字旋转

2015-07-23 15:15:06

动态弹出

2011-05-04 09:05:39

Flash

2021-02-07 23:04:31

5G过年红包
点赞
收藏

51CTO技术栈公众号