gpt4 book ai didi

html - "stroke"css 属性在我的 svg 中不起作用

转载 作者:行者123 更新时间:2023-12-04 08:06:06 33 4
gpt4 key购买 nike

实际上,我是使用 css 为 svg 设置动画的新手,我想为我的 svg 设置动画,例如检查标记动画,但我发现 youtube 中的许多人使用“stroke”css 属性,但它不会对我的 svg 进行任何更改,但是当我将颜色填充到我的 svg 时它仍然很好
这是代码

* {
padding: 0;
margin: 0;
}

.container {
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}

.container svg {
width: 50%;
}

#circle {
fill: red;
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: animating 2s ease-out;
}

@keyframes animating{
to {
stroke-dashoffset: 0;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" data-name="Layer 1" viewBox="0 0 90 112.5" x="0px" y="0px">
<path d="M45,77A32,32,0,1,1,77,45,32.036,32.036,0,0,1,45,77Zm0-62A30,30,0,1,0,75,45,30.034,30.034,0,0,0,45,15Z" id="circle"/>
<path d="M40.5,53.5a1,1,0,0,1-.707-.293l-6-6a1,1,0,0,1,1.414-1.414L40.5,51.086,53.793,37.793a1,1,0,0,1,1.414,1.414l-14,14A1,1,0,0,1,40.5,53.5Z" class="mark"/>
</svg>
</div>
</body>
</html>

最佳答案

你的圆不是一个真正的圆,这个形状是一个空心圆和stroke你看到的其实是fill ,不能使用 stroke-dashoffset 进行动画处理.

<path d="M45,77A32,32,0,1,1,77,45,32.036,32.036,0,0,1,45,77Zm0-62A30,30,0,1,0,75,45,30.034,30.034,0,0,0,45,15Z" id="circle"/>
您应该使用真实的 circle形状,然后可以为其设置动画 stroke-dashoffset像这样:
<circle r="25" cx="45" cy="45" id="circle"/>

* {
padding: 0;
margin: 0;
}

.container {
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}

.container svg {
width: 50%;
}

#circle {
fill: none;
stroke: red;
stroke-width: 2;
stroke-dasharray: 500;
stroke-dashoffset: 500;
animation: animating 3s ease-out both;
}

@keyframes animating{
to {
stroke-dashoffset: 0;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<svg xmlns="http://www.w3.org/2000/svg" data-name="Layer 1" viewBox="0 0 90 112.5" x="0px" y="0px">
<!--<path d="M45,77A32,32,0,1,1,77,45,32.036,32.036,0,0,1,45,77Zm0-62A30,30,0,1,0,75,45,30.034,30.034,0,0,0,45,15Z" id="circle"/>-->
<!-- Use a real circle -->
<circle r="25" cx="45" cy="45" id="circle"/>
<path d="M40.5,53.5a1,1,0,0,1-.707-.293l-6-6a1,1,0,0,1,1.414-1.414L40.5,51.086,53.793,37.793a1,1,0,0,1,1.414,1.414l-14,14A1,1,0,0,1,40.5,53.5Z" class="mark"/>
</svg>
</div>
</body>
</html>

关于html - "stroke"css 属性在我的 svg 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66219281/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com