SVG 动画描边-Dasharray

我正在为这个 svg 笔画设置动画以改变 dasharray 偏移量。边框的两端可以动画,而不是左端是静态的,只有另一端移动和环绕吗?

我被迫在这里纠正更多内容,请告诉我是否清楚,或者我是否需要以更好的方式解释我需要的内容。谢谢。

<style>

    .wrapper{
        --height: 185px; 
        width: 300px;
        height: var(--height);
        background: rgba(255,255,0);
        margin: auto;
        position: relative;
    }
    
    .image,.image-border{
        position: absolute;
        top: 0;
        left: 0;
        border-radius: 12px !important;
    }
    
    .image{
        max-width: 96% !important;
        top: 1.3%;
        left: 1.8%;
    }
    
    .image-border{
        width: 100%;
        top: -2.3px;
    }
    
    .image-border path{
        fill: none;
        stroke: #824ad8;
        stroke-width: 2.3px;
        stroke-dasharray: 320;
        stroke-dashoffset: 320;
        transition: 1s;
    }
    
    .wrapper:hover .image-border path{
        stroke-dashoffset: 0;
        stroke-dasharray: 320;
        /*stroke-width: 2.3px;*/
    }
    
</style>

<div class="wrapper">
    
    <img class="image" src="https://wordpress-513216-1810865.cloudwaysapps.com/wp-content/uploads/2021/03/Screen-Shot-2021-02-12-at-10.48.49-AM.jpg" />
    
    <svg class="image-border" viewBox="0 0 103 65" version="1.1" xmlns="http://www.w3.org/2000/svg" style="stroke-linecap:round;">
        <path d="M1.146,4.244c-0,-1.71 1.388,-3.098 3.098,-3.098l93.804,-0c1.71,-0 3.098,1.388 3.098,3.098l-0,55.767c-0,1.71 -1.388,3.099 -3.098,3.099l-93.804,-0c-1.71,-0 -3.098,-1.389 -3.098,-3.099l-0,-55.767Z"/>
    </svg>
    
</div>

chenhongyi2009 回答:SVG 动画描边-Dasharray

您必须更改 stroke-dasharray 以使其比未悬停在长度为 0 的孔时小两倍,同时将其移动到其初始位置,以便你有一个完整的中风正确居中。

.image-border path{
    fill: none;
    stroke: #824ad8;
    stroke-width: 2.3px;
    stroke-dasharray: 320;
    stroke-dashoffset: 320;
    transition: 1s;
}
.wrapper:hover .image-border path{
    stroke-dasharray: 160 0;
    stroke-dashoffset: 0;
}

.wrapper{
    --height: 185px; 
    width: 300px;
    height: var(--height);
    background: rgba(255,255,0);
    margin: auto;
    position: relative;
}

.image,.image-border{
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 12px !important;
}

.image{
    max-width: 96% !important;
    top: 1.3%;
    left: 1.8%;
}

.image-border{
    width: 100%;
    top: -2.3px;
}
<div class="wrapper">
    
    <img class="image" src="https://wordpress-513216-1810865.cloudwaysapps.com/wp-content/uploads/2021/03/Screen-Shot-2021-02-12-at-10.48.49-AM.jpg" />
    
    <svg class="image-border" viewBox="0 0 103 65" version="1.1" xmlns="http://www.w3.org/2000/svg" style="stroke-linecap:round;">
        <path d="M1.146,4.244c-0,-1.71 1.388,-3.098 3.098,-3.098l93.804,-0c1.71,-0 3.098,1.388 3.098,3.098l-0,55.767c-0,1.71 -1.388,3.099 -3.098,3.099l-93.804,-0c-1.71,-0 -3.098,-1.389 -3.098,-3.099l-0,-55.767Z"/>
    </svg>
    
</div>

本文链接:https://www.f2er.com/773938.html

大家都在问