自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

妙用 Background 實(shí)現(xiàn)花式文字效果

開發(fā) 前端
本文將講解如何利用 background 系列屬性,巧妙的實(shí)現(xiàn)一些花式的文字效果。通過 background-size 與 background-position 實(shí)現(xiàn)酷炫的文字下劃線效果。

[[426213]]

Hello 大家好,我是 Coco。本文將講解如何利用 background 系列屬性,巧妙的實(shí)現(xiàn)一些花式的文字效果。通過本文,你將可以學(xué)到:

  • 通過 background-size 與 background-position 實(shí)現(xiàn)酷炫的文字下劃線效果
  • 通過 background-size 與 background-position 以及 background-clip 實(shí)現(xiàn)文字逐個(gè)漸現(xiàn)的效果

起因

寫本文的動(dòng)機(jī)是在于,某天,被這樣一個(gè)標(biāo)題所吸引 -- 10 Masterfully Designed Websites[1],其中列舉了 10 個(gè)極具創(chuàng)意的 Web 網(wǎng)站。

其中一個(gè) Red Bull Racing[2] 網(wǎng)站,是介紹關(guān)于 F1 紅牛車隊(duì)的主頁。其中有這樣一個(gè)非常有意思的 Hover 動(dòng)畫效果:

圖片

這個(gè)文字的 hover 出現(xiàn)效果,看似簡單,其實(shí)想要完全實(shí)現(xiàn)它,僅僅依靠 CSS 是非常復(fù)雜的,其中一個(gè)比較難的地方在于 -- 如何讓一個(gè)效果,逐漸作用給整段文字中的部分,而不是一次將整個(gè)效果賦予整段文本。

利用 background 實(shí)現(xiàn)文字的下劃線效果

到這里,我想起了之前在這篇文章中 -- CSS 文字裝飾 text-decoration & text-emphasis[3],我介紹的一種 使用 background 模擬下劃線的效果。

看個(gè)簡單的 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]

當(dāng)然這個(gè)是最基礎(chǔ)的,巧妙的運(yùn)用 background 的各種屬性,我們實(shí)現(xiàn)各種有意思的效果。

巧妙改變 background-size 與 background-position 實(shí)現(xiàn)文字 hover 動(dòng)效

這里,通過巧妙改變 background-size 與 background-position 屬性,我們可以實(shí)現(xiàn)一些非常有意思的文字 hover 效果。

先看這樣一個(gè) Demo,核心代碼作用于被<p>標(biāo)簽包裹的 <a>標(biāo)簽之上:

  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; 

我們雖然,設(shè)定了 background: linear-gradient(90deg, #ff3c41, #fc0, #0ebeff),但是一開始默認(rèn)它的 background-size: 0 3px,也就是一開始是看不到下劃線的,當(dāng) hover 的時(shí)候,改變 background-size: 100% 3px,這個(gè)時(shí)候,就會(huì)有一個(gè) 0 3px 到 100% 3px 的變換,也就是一個(gè)從無到有的伸展效果。

看看最后的效果:

圖片

由于設(shè)定的 background-position 是 0 100%,如果,設(shè)定的 background-position 是 100% 100%,我們可以得到一個(gè)反向的效果:

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

再看看效果,你可以對(duì)比著上面的動(dòng)圖看看具體的差異點(diǎn)在哪:

圖片

CodePen Demo -- background underline animation[5]

OK,如果我們使用 background 實(shí)現(xiàn)兩條重疊的下劃線,再利用上述的兩個(gè)不同的 background-position 值,我們就可以得到一個(gè)更有意思的下劃線 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; 

可以得到這樣一種效果,其實(shí)每次 hover, 都有兩條下劃線在移動(dòng):

CodePen Demo -- background underline animation[6]

通過 background-size 與 background-position 配合 background-clip 實(shí)現(xiàn)文字的漸現(xiàn)

上述一大段都在圍繞 -- 下劃線。

回歸到本文一開始提到的 Gif 效果,我們能否實(shí)現(xiàn)在一段文字中,實(shí)現(xiàn)文字的漸現(xiàn)效果呢?

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

我們稍微改造下代碼,實(shí)現(xiàn)利用 background-clip 實(shí)現(xiàn) hover 的時(shí)候部分文字逐漸改變顏色:

  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) 背景色作用給了文字,同時(shí)利用 color: transparent 讓文字展示出背景色的色值:

圖片

CodePen Demo -- background-size 與 background-position 以及 background-clip 實(shí)現(xiàn)文字逐個(gè)漸現(xiàn)[7]

當(dāng)然,稍微對(duì)上述代碼變形,我們就可以演化出幾種不同的效果。

實(shí)現(xiàn)整段文字的漸現(xiàn) - 從透明到出現(xiàn)

第一種就是從透明到有顏色,逐漸展現(xiàn),這里我們只需要讓 color 一直是 transparent 即可(下述效果借助了一個(gè)按鈕去觸發(fā)效果):

  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%; 

效果如下:

圖片

實(shí)現(xiàn)整段文字的漸現(xiàn) - 從一種顏色到另外一種顏色

還可以實(shí)現(xiàn)文字從一種顏色到另外一種顏色的逐個(gè)轉(zhuǎn)變,只需要添加多一層 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%; 

這里需要解釋一下,雖然設(shè)置了 color: transparent,但是文字默認(rèn)還是有顏色的,默認(rèn)的文字顏色,是由第一層漸變賦予的 background: linear-gradient(90deg, #999, #999), linear-gradient(90deg, #fc0, #fc0),也就是這一層:linear-gradient(90deg, #999, #999)。

當(dāng) hover 觸發(fā)時(shí),linear-gradient(90deg, #999, #999) 這一層漸變逐漸消失,而另外一層 linear-gradient(90deg, #fc0, #fc0)` 逐漸出現(xiàn),借此實(shí)現(xiàn)上述效果。

CodePen -- background-clip 文字漸現(xiàn)效果[8]

簡單模擬題圖效果

這里,我們簡單利用這個(gè)技巧模擬一下文章一開始列出的 Gif 的效果:

圖片

這個(gè)效果原作者的技巧是:

  1. 將每個(gè)單詞作為一個(gè)對(duì)象,包裹一個(gè)特殊的 class
  2. 利用 animation-delay 將動(dòng)畫逐漸賦予每個(gè)單詞

這里,我們將整段文本統(tǒng)一處理,簡單還原:

  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. /** 動(dòng)畫核心 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. / ** 簡單控制半透明黑色遮罩出現(xiàn) **/ 
  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; 

 效果如下:

圖片

可以看到,由于是整體控制整段文本,效果上沒有逐個(gè)單詞控制的好,但是優(yōu)點(diǎn)是代碼量非常少。對(duì)于一些簡單卡片類的 hover 場景,足以。

background-image、background-clip 實(shí)現(xiàn)文字漸現(xiàn)效果[9]

完美還原題圖效果

當(dāng)然,題圖效果使用純 CSS 也是不在話下的。只不過就不是簡單能夠統(tǒng)一處理的了。

這里,我們需要對(duì)每一個(gè)單詞進(jìn)行精細(xì)化的處理,并且使用每個(gè)單詞的偽元素進(jìn)行額外的動(dòng)畫。

簡單的結(jié)構(gòu)如下:

  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.     // ... 類似結(jié)構(gòu)      
  10. </p> 

可以看到,每個(gè)單詞都被 包裹,并且添加了 data-text,方便偽元素拿到當(dāng)前單詞。

接下來,就是設(shè)定動(dòng)畫,并且通過順序給每個(gè) 添加相應(yīng)遞增的 animation-delay 以實(shí)現(xiàn)沒個(gè)單詞動(dòng)畫的差異性。核心的偽代碼如下:

  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.     } 

其實(shí)動(dòng)畫本身不太復(fù)雜,主要講兩點(diǎn):

hover 狀態(tài)下和非 hover 狀態(tài)下的 transition-duration 是不一樣的,是因?yàn)槿∠?hover 過程中,動(dòng)畫消失過程的時(shí)間通常是要求更短的;

借助了 SASS 的循環(huán) @for $i from 1 to 21 {} 遞增給每個(gè) span 和它的偽元素添加了遞增的 transition-delay。

最終,我們可以得到如下的結(jié)果:

完整的代碼,你可以猛戳 -- CSS 靈感 - 利用 animation-delay 實(shí)現(xiàn)文字漸現(xiàn)效果[10]

最后

好了,本文到此結(jié)束,希望對(duì)你有幫助 :)

參考資料

[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 實(shí)現(xiàn)文字逐個(gè)漸現(xiàn):

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

[8]CodePen -- background-clip 文字漸現(xiàn)效果:

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

[9]background-image、background-clip 實(shí)現(xiàn)文字漸現(xiàn)效果:

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

[10]CSS 靈感 - 利用 animation-delay 實(shí)現(xiàn)文字漸現(xiàn)效果:

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

 

責(zé)任編輯:姜華 來源: iCSS前端趣聞
相關(guān)推薦

2023-05-18 09:25:20

background花式文字效果

2010-09-13 14:09:35

CSS文字

2022-07-19 06:20:47

CSSbackground

2023-12-04 08:06:41

CSS文字效果

2021-09-28 08:26:06

CSS 技巧文字鏤空波浪

2024-01-30 09:21:29

CSS文字效果文字裝飾

2022-03-31 07:46:17

CSS動(dòng)畫技巧

2021-09-30 08:25:28

CSS 技巧酷炫線條光影

2023-05-26 07:08:05

CSS模糊實(shí)現(xiàn)文字

2021-08-30 06:20:39

CSS 技巧3D 效果

2023-12-25 12:57:00

樹形結(jié)構(gòu)CSScounters

2009-11-10 15:07:11

VB.NET窗體

2021-11-09 08:30:48

CSS 技巧巧用濾鏡

2024-05-11 08:25:43

自定義分隔線背景效果

2024-03-20 09:40:27

動(dòng)畫技巧CSS逐幀動(dòng)畫

2022-04-13 07:52:19

VLAN 技術(shù)物理局域網(wǎng)虛擬局域網(wǎng)

2021-02-07 23:04:31

5G過年紅包

2009-07-30 09:42:29

CSS實(shí)現(xiàn)文字旋轉(zhuǎn)

2011-05-30 08:58:59

Android focusable 屬性

2011-07-08 10:15:15

IPhone 動(dòng)畫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)