当前位置: 首页 > 科技观察

使用Background实现花哨的文字效果

时间:2023-03-13 01:21:46 科技观察

大家好,我是Coco。本文将讲解如何使用背景系列属性巧妙地实现一些花哨的文字效果。通过这篇文章,你将能够学到:使用background-size和background-position实现炫酷的文字下划线效果使用background-size、background-position和background-clip实现文字逐渐出现的效果写这篇文章的原因文章是这样说的,有一天,我被这样一个标题——10MasterfullyDesignedWebsites[1]所吸引,其中列出了10个极具创意的网站。RedBullRacing[2]网站之一是介绍F1RedBull车队的主页。有这样一个非常有趣的Hover动画效果:这段文字的hover效果看似简单,但实际上仅仅依靠CSS来完全实现它是非常复杂的。比较困难的部分之一是——如何制作效果,逐渐将效果应用于整个文本的部分,而不是将整个效果一次应用于整个文本。利用背景实现文字的下划线效果这时我想起了这篇文章——CSS文字装饰text-decoration&text-emphasis[3]中介绍了一种利用背景来模拟下划线的效果。看个简单的DEMO,使用background模拟文字的下划线效果:

Loremipsumdolorsitametconsecteturadipisicingelit.Mollitianostrumplaceatconsequaturdeseruntvelitducimuspossimuscommoditemporibusdebitisquam,molestiaelaboriosamsitrepellendussedsapientequidemquodaccusantiumvero.

p{??width:600px;font-size:24px;color:#666;}a{background:linear-gradient(90deg,#0cc,#0cc);background-size:100%3px;background-repeat:no-repeat;background-position:100%100%;color:#0cc;}用背景模拟文字下划线效果,效果图如下:或者,用背景模拟虚线下划线:a{background:linear-gradient(90deg,#0cc50%,transparent50%,transparent1px);background-size:10px2px;background-repeat:repeat-x;background-position:100%100%;}CodePenDemo--使用背景模拟下划线和点下划线[4]当然这是最基本的,小聪明利用背景的各种属性,我们实现了各种有趣的效果。巧妙改变background-size和background-position实现文字悬停效果这里,通过巧妙改变background-size和background-position属性,我们可以实现一些非常有趣的文字悬停效果。先看这样一个demo,核心代码作用于被

标签包裹的标签:

Loremipsumdolorsitametconsecteturadipisicingelit.Mollitianostrumpplaceatconsequaturdeseruntvelitducimuspossimuscommoditemporibusdebitisquam,molestiaelaboriosamsitrepellendussedsapientequidemquodaccusantium:

gradient0deg(9ff3c41,#fc0,#0ebeff);背景尺寸:03px;背景重复:无重复;背景位置:0100%;过渡:1sall;颜色:#0cc;}a:悬停{背景-尺寸:100%3px;color:#000;}虽然我们设置了background:linear-gradient(90deg,#ff3c41,#fc0,#0ebeff),但是它默认的background-size:03px开头,也就是看不到下划线一开始。悬停时,更改background-size:100%3px。这时候会出现从03px到100%3px的变换,这是一种从头开始拉伸的效果。看最后的效果:由于设置的background-position为0100%,如果设置的background-position为100%100%,我们可以得到一个相反的效果://其他不变,只改变background-position,变了从0100%到100%100%a{...background-position:100%100%;...}然后看效果,可以对比上面的动画看看具体的区别在哪里:CodePenDemo--背景下划线动画[5]OK,如果我们用背景实现两条重叠的下划线,再利用上面两个不同的background-position值,就可以得到更有趣的下划线悬停效果。CSS代码显示,背景模拟的两条下划线的background-position的值不同:a{background:linear-gradient(90deg,#0cc,#0cc),linear-gradient(90deg,#ff3c41,#fc0,#8500d8);背景大小:100%3px,03px;背景重复:无重复;背景位置:100%100%,0100%;过渡:0.5sall;颜色:#0cc;}a:hover{background-size:03px,100%3px;color:#000;}可以得到这样的效果,其实每次悬停,都有两条下划线移动:CodePenDemo--backgroundunderlineanimation[6]viabackground-size和background-position配合background-clip实现文字淡入。上述大段用--下划线包围。回到本文开头提到的Gif效果,我们能否在一段文字中实现文字的渐变效果呢?上面的技巧用到了背景,那么背景背景色能不能改变文字的颜色呢?答案是“是”,仅在背景剪辑的帮助下。molestiaelaboriosamsitrepellendussedsapientequidemquodaccusantiumvero。

p{??color:#666;cursor:pointer;}a{background:linear-gradient(90deg,#fc0,#fc0);background-size:0100px;background-repeat:no-repeat;background-位置:0100%;背景剪辑:文本;过渡:。6salllinear;}p:hovera{background-size:100%100%;color:transparent;}看效果,通过background-clip:textmask裁剪,我们将background:linear-gradient(90deg,#fc0,#fc0)给文本赋予背景色,同时使用color:transparent让文本显示背景色的颜色值:CodePenDemo--background-size,background-position和background-clip实现文字的渐变显示[7]当然,稍微变形一下上面的代码,我们可以演化出几种不同的效果。实现整个文本的淡入——从透明到显现。首先是从透明到有色逐渐显现。这里我们只需要保持颜色透明即可(以下效果使用按钮触发效果):Button

Loremipsumdolorsitametconsecteturadipisicingelit.Mollitianostrumplaceatconsequaturdeseruntvelitducimuspossimuscommoditemporibusdebitisquam,molestiaelaboriosamsitrepellendussedsapientequidemquod.a{background:linear-gradient(90deg,#fc0,#fc0);background-size:0100px;background-repeat:no-repeat;background-position:0100%;color:transparent;background-clip:text;}.button:hover~pa{transition:.8salllinear;background-size:0100px,100%100%;}效果如下:实现整个文字的淡入淡出——从一种颜色到另一种颜色也可以实现从一种颜色到另一种颜色的文字,只需添加一层额外的背景图像渐变。按钮

Loremipsumdolorsitametconsecteturadipisicingelit.Mollitianostrumplaceatconsequaturdeseruntvelitducimuspossimuscommoditemporibusdebitisquam,molestiaelaboriosamsitrepellendussedsapientequidemquodaccusantiumvero。

{background:linear-gradient#9),#99,90lineardeg-gradient(90deg,#fc0,#fc0);背景大小:100%100%,0100px;背景重复:无重复;背景位置:100%100%,0100%;颜色:透明;背景剪辑:text;}.button:hover~pa{transition:.8salllinear;background-size:0100px,100%100%;}这里需要说明一下,虽然设置了color:transparent,但是文字默认还是有颜色的,默认文字颜色由第一层渐变背景给定: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-cliptextfade-ineffect[8]标题图片效果的简单模拟这里我们简单的用这个技巧来模拟文章开头列出的Gif的效果:原作者对这个效果的技巧就是:将每个单词作为一个对象,包裹一个特殊的类使用animation-delay逐渐给每个单词赋予动画这里我们将整个文本统一处理,简单还原:Button

Loremipsumdolorsitametconsecteturadipisicingelit.Mollitianostrumplaceatconsequaturdeseruntvelitducimuspossimuscommoditemporibusdebitisquam,molestiaelaboriosamsitrepellendussedsapientequidemquodaccusantiumvero.

/**动画核心background、line-height、opacity**/a{background:linear-gradient(90deg,#ff5722,#ff5722),linear-gradient(90deg,#aaa,#aaa);background-size:100%100%,0100px;background-repeat:no-repeat;background-position:100%100%,0100%;cursor:pointer;颜色:透明;背景剪辑:文本;行高:3;不透明度:0;}.button:hover~pa{transition:1.2sbackground.3sease-out,.8sline-heightease-out,.6sopacityease-in;background-size:0100px,100%100%;color:transparent;line-height:1;opacity:1;}/**简单控制半透明黑色蒙版的外观**/a::before{content:"";position:fixed;background:rgba(0,0,0,.8);top:0;left:0;right:0;bottom:0;z-index:-1;transition:.3salllinear;opacity:0;}.button:hover~pa::before{opacity:1;}效果如下:可以看出效果不如word-by-wordcontrol因为它是对整个文本进行整体控制,但是优点是代码量非常少,对于一些简单的卡片悬停场景来说已经足够了。background-image、background-clip实现文字淡入效果[9]完美还原题图效果当然题图效果使用纯CSS是没有问题的。只是统一处理起来并不容易。在这里,我们需要对每个单词进行微调,并为每个单词使用伪元素来实现额外的动画效果。简单结构如下:Button

Loremipsumdoloramet//...类似的结构

可以看到,每个单词都被包裹起来,并且加入了data-text,方便伪元素获取当前单词。接下来设置动画,为每个单词添加相应增加的animation-delay,以实现每个单词动画的不同。核心伪代码如下:p{position:relative;width:500px;overflow:hidden;}pspan{position:relative;display:inline-block;opacity:0;transform:translateY(15px)translateZ(0);transition-属性:变换,不透明度;过渡持续时间:.3s,.2s;}.button:hover~pspan{不透明度:1;颜色:#ddd;变换:translateY(0)translateZ(0);过渡持续时间:1s,.2s;}pspan:after,pspan:before{position:absolute;content:attr(data-text);top:0;left:0;z-index:1;transform:translateZ(0);}pspan:在{color:#e62541;transition-property:opacity;transition-duration:.1s;}.button:hover~pspan:after{opacity:0;transition-property:opacity;transition-duration:.4s;}@for$ifrom1to21{pspan:nth-child(#{$i}){转换延迟:#{$i*0.04}s;&::after{转换延迟:#{$i*0.04+0.2}s;}其实动画本身并不太复杂,主要有两点:hover状态和非hover状态下的transition-duration不一样,因为在取消hover过程的过程中,动画消失的时间ing过程通常要求更短;SASS循环@for$ifrom1to21{}递增地向每个跨度及其伪元素添加一个递增的转换延迟。最终可以得到如下结果:完整代码,大家可以戳——CSS灵感——利用animation-delay实现文字淡入效果[10]最后,本文到此结束,希望这将对您有所帮助:)参考资料[1]10个精心设计的网站:https://medium.com/@MonteVerdeVT/10-masterfully-designed-websites-f999112e2fa9[2]RedBullRacing:https://thenewmobileworkforce。imm-g-prod.com/back-at-hq[3]CSS文字修饰text-decoration&text-emphasis:https://github.com/chokcoco/iCSS/issues/103[4]CodePenDemo--使用背景模拟下划线和虚线下划线:https://codepen.io/Chokcoco/pen/YzNQKwm[5]CodePenDemo--背景下划线动画:https://codepen.io/Chokcoco/pen/QWdgLwp[6]CodePenDemo--背景下划线动画:https://codepen.io/Chokcoco/pen/MWJogjQ[7]CodePenDemo--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灵感——使用一个imation-delay实现文字淡入效果:https://csscoco.com/inspiration/#/./animation/animation-delay-control-text-effect