【发布时间】:2019-04-03 06:05:04
【问题描述】:
我正在做这个效果(见网址),但在我的项目中,宽度和高度不固定。 https://tympanus.net/codrops/2014/02/26/creating-a-border-animation-effect-with-svg-and-css/
我不知道如何自动计算stroke-dasharray。
谢谢!!
.box-wrap{height: 200px;margin-top: 100px;text-align: center;}
.box{background: #ffffff; width: 30%; margin-right: 10px;height: 200px;position: relative;}
.box svg { position: absolute; top: 0; left: 0; }
.box svg line { stroke-width: 3; stroke: #000000; fill: none; transition: all .8s ease-in-out; }
.box-wrap .even-news-content:hover svg line{transition-delay: 0.1s;}
.box svg line.top, .box svg line.bottom { stroke-dasharray: calc(100% + 30px) calc(100% - 60px);}
.box svg line.left,.box svg line.right {stroke-dasharray: calc(100% + 30px) calc(100% - 60px); }
.box:hover svg line.top { transform: translateX(-200%); }
.box:hover svg line.bottom {transform: translateX(200%);}
.box:hover svg line.left {transform: translateY(200%);}
.box:hover svg line.right {transform: translateY(-200%); }
<link href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class=" d-flex justify-content-around box-wrap">
<div class="box">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<line class="top" x1="0" y1="0" x2="300%" y2="0"/>
<line class="left" x1="0" y1="100%" x2="0" y2="-200%"/>
<line class="bottom" x1="100%" y1="100%" x2="-200%" y2="100%"/>
<line class="right" x1="100%" y1="0" x2="100%" y2="300%"/>
</svg>
</div>
<div class="box">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<line class="top" x1="0" y1="0" x2="300%" y2="0"/>
<line class="left" x1="0" y1="100%" x2="0" y2="-200%"/>
<line class="bottom" x1="100%" y1="100%" x2="-200%" y2="100%"/>
<line class="right" x1="100%" y1="0" x2="100%" y2="300%"/>
</svg>
</div>
<div class="box">
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<line class="top" x1="0" y1="0" x2="300%" y2="0"/>
<line class="left" x1="0" y1="100%" x2="0" y2="-200%"/>
<line class="bottom" x1="100%" y1="100%" x2="-200%" y2="100%"/>
<line class="right" x1="100%" y1="0" x2="100%" y2="300%"/>
</svg>
</div>
</div>
【问题讨论】:
-
您是否有示例说明这些值的依据?我的意思是
60px和30px至少来自哪里,这就是我认为你想要计算的? -
你肯定需要知道盒子的宽度和高度来计算
stroke-dasharray? -
@ksav 可以理解但还是要知道有没有固定数量