svg css实现霓虹灯

svg css文字霓虹灯


事情是酱紫的,因为两会马上来了,公司产品要搞免费试用活动。首页要求醒目抓眼,UI小姐姐废了九牛九虎之力让产品同意了一版霓虹灯效果图,要在首页中的部分文字大量使用霓虹效果。UI小姐姐给了gif图,但是测试中有拉伸和各种分别率问题,效果并不好,于是就有了这篇文章,用我们的大CSS来完成这个任务。
首先,要知道svg中有个重要的属性–stroke,翻译过来意为“描边”,这就是能完成霓虹效果的最重要角色。stroke有以下几种属性设置:

stroke-width //描边粗细
stroke-linecap //描边端点的表现方式 可用值有 butt round square inherit
stroke-linejoin //描边转角的表现方式 可用值有 miter round bevel inherit 
stroke-miterlimit //描边相交(锐角)的表现方式
stroke-dasharray //虚线描边 可用值 none dasharray inherit
stroke-dashoffset //虚线的起始偏移 可用值 percentage length inherit
stroke-opacity //透明度

内联SVG的强大之处在于,其本身也是个HTML元素,能被CSS属性控制,处理传统的高宽定位、边框背景色等,SVG自身的一些特殊属性也能被CSS支持,甚至在CSS3 animation动画中,因为有这些特性,接下来我们就可以实现一个简单的霓虹效果

<svg width="100%" height="100">
    <text text-anchor="middle" x="50%" y="50%" class="welcome">
        欢迎试用
    </text>
</svg> 
.welcome{
    font-size: 64px;
    font-weight: bold;
    text-transform: uppercase;
    fill: none;
    stroke: #3498db;
    stroke-width: 2px;
    stroke-dasharray: 90 310;
    animation: stroke 6s infinite linear;
}
@keyframes stroke {
  100% {
    stroke-dashoffset: -400;
  }
}

然后根据可以根据需求调整颜色和动画时间:

<svg width="100%" height="100">
    <text text-anchor="middle" x="50%" y="50%" class="welcome welcome-1">
        欢迎试用
    </text>
    <text text-anchor="middle" x="50%" y="50%" class="welcome welcome-2">
        欢迎试用
    </text>
    <text text-anchor="middle" x="50%" y="50%" class="welcome welcome-3">
        欢迎试用
    </text>
    <text text-anchor="middle" x="50%" y="50%" class="welcome welcome-4">
        欢迎试用
    </text>
</svg> 
.welcome-1{
    stroke: #3498db;
    text-shadow: 0 0 5px #3498db;
    animation-delay: -1.5s;
}
.welcome-2{
    stroke: #f39c12;
    text-shadow: 0 0 5px #f39c12;
    animation-delay: -3s;
}
.welcome-3{
    stroke: #e74c3c;
    text-shadow: 0 0 5px #e74c3c;
    animation-delay: -4.5s;
}
.welcome-4{
    stroke: #9b59b6;
    text-shadow: 0 0 5px #9b59b6;
    animation-delay: -6s;
}

效果如下图
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值