繁体   English   中英

如何在svg stroke-dasharray中使用calc()

[英]how to use calc() in svg stroke-dasharray

我正在执行此效果(请参见url),但是在我的项目中宽度和高度不是固定的。 https://tympanus.net/codrops/2014/02/26/creating-a-border-animation-effect-with-svg-and-css/

我不知道如何自动计算笔触数组。

谢谢!!

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

这是我的解决方案,但我没有使用calc 不用了 我使用的是路径而不是线条,并且使用过渡来使stroke-dashoffset动画从0变为线条的两倍。 stroke-dasharray设置为与路径一样长。

UPDATE

在第二个SVG中,我按预期使用线

 .box{width:30%; border:1px solid red; position:relative; display:inline-block;} .box p{position:absolute;width:100%;height:1em;text-align:center;margin:auto;top:0;bottom:0;} .box:hover .top, .box:hover .bottom{stroke-dashoffset:200} .box:hover .left, .box:hover .right{stroke-dashoffset:200} path,line{stroke:black; fill:none;stroke-linecap: round;} .top,.bottom{stroke-dasharray:100;stroke-dashoffset:0;} .left,.right{stroke-dasharray:50;stroke-dashoffset:0} path,line{transition: stroke-dashoffset 1.5s;} 
 <div class="box"><p>PATHS</p> <svg viewBox="-5 -5 110 60"> <path class="top" d="M100,0H0" /> <path class="left" d="M0,0V50"/> <path class="bottom" d="M0,50H100"/> <path class="right" d="M100,50V0"/> </svg> </div> <div class="box"><p>LINES</p> <svg viewBox="-5 -5 110 60"> <line class="top" x1="100" /> <line class="left" y2="50"/> <line class="bottom" y1="50" x2="100" y2="50"/> <line class="right" x1="100" y1="50" x2="100"/> </svg> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM