css svg伸展动画,利用SVG和CSS3来实现一个炫酷的边框动画

今天我们来探索一下Carl Philipe Brenner的网站上一个微妙而有趣的动画效果。当鼠标经过网格元素时,会有一个微妙的动画发生——网格元素变得透明,每条边有个顺时针的动画,创造了非常好的效果。这种效果是通过JS给span标签的宽或者高做了动画。我们待会会用SVG和CSS渐变来完成。注意,这个技术还是实验性的。

首先,让我们来看看基本的概念,然后我们会朝着这个方向努力。

请注意,我们将在SVG上使用CSS过渡,可能无法得到所有浏览器的支持。

乍眼一看可能不明白这个效果是怎么完成的。我们先仔细看看上面的边就会发现,白色的边的宽度不断从右边往左边延伸,而一条稍微延时的边紧跟着一起移动。每条边都有这样的做法。看起来就像上面的边经过拐角移动到了左边,并以此类推。

不用SVG也能完成这样的效果,甚至只用伪元素。但是我们想探索一下怎样用CSS而不是JavaScript来控制SVG。

现在,来思考一下要怎么创建出这样的效果。我们可以改变矩形的troke-dashoffset或者直接画线。我们尝试不用JavaScript的解决方案。我们发现,CSS过渡stroke-dashoffset 和 stroke-dasharray的值会触发很多BUG。所以我们要尝试不同的解决方案,利用线条和它们的动画,这在CSS里很容易理解和实现。这也给我们更多机会去探索不同的动画效果。

我们将要使用的线的特别之处是,它们在这个动画里将有三种状态。它们是方盒边长的三倍,其中中间一截是与边等长的间隙。我们将通过设置stroke-dashoffset的值来实现与方盒的边等长。现在,这个动画实现的关键在于线的位置转换:

855b121ef878472a48eb94b73af01eb3.gif

SVG与方盒大小一致,就不会看到超出虚线的部分。

我们先完成第一条线:

CSS Code复制内容到剪贴板

这个div长宽20px,和SVG一样。把SVG位置设为absolute,线宽度为10,stroke-dasharray为200。

CSS Code复制内容到剪贴板

div {

width:200px;

height:200px;

position:relative;

overflow:hidden;

background:#ddd;

}

svg {

position:absolute;

top: 0;

left: 0;

}

svg line {

stroke-width: 10;

stroke:#000;

fill:none;

stroke-dasharray: 200;

-webkit-transition: -webkit-transform .6s ease-out;

transition: transform .6s ease-out;

}

div:hover svg line {

-webkit-transform: translateX(-400px);

transform: translateX(-400px);

}

当数鼠标悬浮在div上时,线也有一个过渡。我们要把线移动到它的三分之二处,所以要在x轴上移动到-400px处,你可以看看这个效果。因为translation不能在这里用百分比做单位,所以用px。

接着添加其余三条线,gif效果:

6440500f912391e27794dede9c0539e0.gif

我们需要实现以下效果:线的第一部分移出方盒后,旁边的线的最后一部分移动进来,,这将传进直线在转角处转弯的假象,

来看看坐标系的定义:

904377806cbcc6de8cd3c6e2f1c88ba7.png

左边的线的坐标是(0,200) 到 (0,-400),底部的是(200,200) 到 (-400,200),右边的是right one (200,0) 到 (200,600):

给每条线加上不同的hover效果:

CSS Code复制内容到剪贴板

div:hover svg line.top{

-webkit-transform: translateX(-400px);

transform: translateX(-400px);

}

div:hover svg line.bottombottom{

-webkit-transform: translateX(400px);

transform: translateX(400px);

}

div:hover svg line.left{

-webkit-transform: translateY(400px);

transform: translateY(400px);

}

div:hover svg line.rightright{

-webkit-transform: translateY(-400px);

transform: translateY(-400px);

}

现在方盒大小改为300 x 460,再给它添加一些内容:

CSS Code复制内容到剪贴板

D

2012

Broccoli, Asparagus, Curry

为了实现Carl Philipe Brenner的网站上的效果,我们还要添加颜色过渡效果、盒子阴影等:

CSS Code复制内容到剪贴板

.box {

width:300px;

height:460px;

position:relative;

background: rgba(255,255,255,1);

display:inline-block;

margin: 010px;

cursor:pointer;

color:#2c3e50;

box-shadow:inset0 0 03px#2c3e50;

-webkit-transition:background0.4s 0.5s;

transition:background0.4s 0.5s;

}

.box:hover {

background: rgba(255,255,255,0);

-webkit-transition-delay: 0s;

transition-delay: 0s;

}

给文字加上样式:

CSS Code复制内容到剪贴板

.box h3 {

font-family:"Ruthie",cursive;

font-size:180px;

line-height:370px;

margin: 0;

font-weight: 400;

width: 100%;

}

.box span {

display:block;

font-weight: 400;

text-transform:uppercase;

letter-spacing:1px;

font-size:13px;

padding:5px;

}

.box h3,

.box span {

-webkit-transition:color0.4s 0.5s;

transition:color0.4s 0.5s;

}

.box:hover h3,

.box:hover span {

color:#fff;

-webkit-transition-delay: 0s;

transition-delay: 0s;

}

给SVG和线条添加样式:

CSS Code复制内容到剪贴板

.box svg {

position:absolute;

top: 0;

left: 0;

}

.box svg line {

stroke-width: 3;

stroke:#ecf0f1;

fill:none;

-webkit-transition:all.8s ease-in-out;

transition:all.8s ease-in-out;

}

给线的过渡加上延时:

CSS Code复制内容到剪贴板

.box:hover svg line {

-webkit-transition-delay: 0.1s;

transition-delay: 0.1s;

}

之前我们定义的stroke-dasharray只有一个值,但是现在要因尺寸变化而修改

CSS Code复制内容到剪贴板

.box svg line.top,

.box svg line.bottombottom{

stroke-dasharray: 330 240;

}

.box svg line.left,

.box svg line.rightright{

stroke-dasharray: 490 400;

}

如果你尝试这些值,你就能看到这些线条不同的显示效果。

最后,我们要个hover过渡设置相应的值。因为现在元素是300px宽,所以水平线条改为900px,竖线同理改变:

CSS Code复制内容到剪贴板

.box:hover svg line.top{

-webkit-transform: translateX(-600px);

transform: translateX(-600px);

}

.box:hover svg line.bottombottom{

-webkit-transform: translateX(600px);

transform: translateX(600px);

}

.box:hover svg line.left{

-webkit-transform: translateY(920px);

transform: translateY(920px);

}

.box:hover svg line.rightright{

-webkit-transform: translateY(-920px);

transform: translateY(-920px);

}

大功告成。希望能通过这些效果激发你的创意,实现更多的效果~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
js代码 [removed] ;(function() { 'use strict'; var c = document.getElementById('c'); var ctx = c.getContext('2d'); var w = c.width = window.innerWidth; var h = c.height = window.innerHeight; var cx = w / 2; var cy = h / 2; var fl = 1000; function prj(obj) { var cz = obj.z fl; if(cz === 0) return; var scl = fl / cz; obj.p.x = cx obj.x * scl; obj.p.y = cy obj.y * scl; obj.s = scl; } var P = function(x, y, z) { this.x = x; this.y = y; this.z = z; this.s = 1; this.cl = 0; this.p = { x: 0, y: 0 }; }; P.prototype = { constructor: P, update: function() { this.z -= 30; }, render: function(ctx) { if(this.z <= -fl) return; ctx.save(); ctx.translate(this.p.x, this.p.y); ctx.scale(this.s, this.s); ctx.fillStyle = 'hsla(' this.cl ', 100%, 50%, 0.5)'; ctx.beginPath(); ctx.arc(0, 0, 2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } }; var M = function(x, y, z) { this.list = []; this.max = 100; this.x = x; this.y = y; this.z = z; this.s = 1; this.p = { x: 0, y: 0 }; this.ax = Math.random() * (Math.PI * 2); this.ay = Math.random() * (Math.PI * 2); this.rx = Math.random() * 100; this.ry = Math.random() * 100; this.cl = Math.random() * 360; this.cls = Math.random(); }; M.prototype = { constructor: M, update: function() { this.cl = this.cls; this.ax = Math.random() * 0.1 - 0.02; this.ay = Math.random() * 0.1 - 0.02; this.x = Math.cos(this.ax) * 100; this.y = Math.sin(this.ay) * 100; this.z = 10; if(this.z > fl) this.z = fl; if(this.list.length < this.max) { if(Math.random() * 100 < 50) { var pp = new P(this.x, this.y, this.z); pp.cl = this.cl; this.list.push(pp); } } else { var pp = this.list.shift(); pp.x = this.x; pp.y = this.y; pp.z = this.z; pp.cl = this.cl; this.list.push(pp); } }, render: function(ctx) { if(this.z <= -fl) return; ctx.save(); ctx.translate(this.p.x, this.p.y); ctx.fillStyle = 'green'; ctx.beginPath(); ctx.arc(0, 0, 2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } }; function update(mv, list) { for(var i = 0; i < list.length; i ) { var p = list[i]; p.update(); prj(p); p.render(ctx); } for(var i = list.length-1; i >= 0; i--) { var p = list[i]; if(p.z <= -fl) continue; if(i === list.length - 1) { ctx.lineWidth = Math.random(); ctx.strokeStyle = 'hsl(' mv.cl ', 100%, 50%)'; ctx.beginPath(); ctx.moveTo(p.p.x, p.p.y); } else { ctx.lineTo(p.p.x, p.p.y); } } ctx.stroke(); } var ms = []; for(var i = 0; i < 10; i ) { ms.push(new M( Math.random() * 400 - 200, Math.random() * 400 - 200, Math.random() * 400 - 200)); } requestAnimationFrame(function loop() { requestAnimationFrame(loop); ctx.clearRect(0, 0, w, h); for(var i = 0; i < ms.length; i ) { var m = ms[i]; m.update(); prj(m); update(m, m.list); } }); })(); [removed] 这是一款基于HTML5 Canvas绘制的炫酷3D线条延伸动画特效,多彩颜色变幻,非常漂亮!
实现SVG CSS动画的流光线效果,可以结合使用stroke-dasharray和stroke-dashoffset这两个属性。首先,为折线元素添加动画效果的class,设置stroke-dasharray的值为折线的总长度,然后通过改变stroke-dashoffset的值来实现流动效果。具体的步骤如下: 1. 在SVG中创建折线元素,并为其设置class属性以便添加动画效果。 2. 使用CSS样式为折线元素设置stroke颜色、宽度等属性,并添加animation属性来指定动画的名称、持续时间和循环方式。 3. 使用stroke-dasharray属性设置折线的总长度,可以根据实际需求调整该值。 4. 使用stroke-dashoffset属性设置折线的偏移量,初始值为折线的总长度,这样折线会完全隐藏起来。 5. 创建一个@keyframes规则,定义动画的关键帧。在关键帧中使用stroke-dashoffset属性来逐渐改变折线的偏移量,从而实现流动效果。 6. 将@keyframes规则与折线元素的动画属性关联起来,使动画生效。 下面是一个示例的代码片段,演示了如何实现SVG CSS动画的流光线效果: ```html <svg> <polyline class="flowing-line" points="50,50 200,50 200,200 50,200" /> </svg> <style> .flowing-line { stroke: #E5DA40; fill: transparent; stroke-width: 2; stroke-linecap: round; animation: flow 3s linear infinite; } @keyframes flow { 0% { stroke-dashoffset: 0; } 100% { stroke-dashoffset: -800; } } </style> ``` 希望这个回答能够帮助到您!如果您有其他相关问题,请随时提出。 相关问题: 1. 如何改变流光线的颜色和宽度? 2. 怎样调整流光线的速度和流动方向? 3. 能否在同一个SVG中同时应用多个流光线效果?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值