[英]SVG background-image not rotating at center
I'm displaying spinner via background-image
property.我正在通过background-image
属性显示微调器。
As you can see in the example, spinner is not rotating from it's center.正如您在示例中所见,微调器并未从其中心旋转。
I tried with transform-origin
property but it's not working.我尝试使用transform-origin
属性,但它不起作用。
body { background: #5daf47; } .spinner { height: 150px; width: 150px; transform-origin: center center; background-size: 150px; background-image: url('https://dl.dropboxusercontent.com/u/3057457/spinner.svg'); }
<div class="spinner"></div>
Here is a content of my svg:这是我的 svg 的内容:
<svg x="0px" y="0px" viewBox="0 0 150 150" xmlns="http://www.w3.org/2000/svg">
<style>
svg {
width: 150px;
animation: loading 3s linear infinite;
}
circle {
animation: loading-circle 2s linear infinite;
stroke: white;
fill: transparent;
-webkit-transform-origin: center center;
-moz-transform-origin: center center;
-ms-transform-origin: center center;
-o-transform-origin: center center;
transform-origin: center center;
}
@keyframes loading {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
@keyframes loading-circle {
0% {
stroke-dashoffset: 0
}
100% {
stroke-dashoffset: -600;
}
}
</style>
<circle cx="75" cy="75" r="60" stroke-width="4" stroke-dashoffset="0" stroke-dasharray="300" stroke-miterlimit="10" stroke-linecap="round"></circle>
</svg>
How can I make this spinner to rotate from center?我怎样才能让这个微调器从中心旋转?
Thanks for help.感谢帮助。
I finally managed to figure it out.我终于设法弄明白了。 The problem was that I didn't set transform-origin
style to <svg>
tag like so:问题是我没有像这样将transform-origin
样式设置为<svg>
标签:
<svg x="0px" y="0px" viewBox="0 0 150 150" xmlns="http://www.w3.org/2000/svg">
<style>
svg {
width: 150px;
animation: loading 3s linear infinite;
/* INSERTED CODE BELOW IS THE SOLUTION */
-webkit-transform-origin: center center;
-moz-transform-origin: center center;
-ms-transform-origin: center center;
-o-transform-origin: center center;
transform-origin: center center;
}
circle {
animation: loading-circle 2s linear infinite;
stroke: white;
fill: transparent;
-webkit-transform-origin: center center;
-moz-transform-origin: center center;
-ms-transform-origin: center center;
-o-transform-origin: center center;
transform-origin: center center;
}
@keyframes loading {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
@keyframes loading-circle {
0% {
stroke-dashoffset: 0
}
100% {
stroke-dashoffset: -600;
}
}
</style>
<circle cx="75" cy="75" r="60" stroke-width="4" stroke-dashoffset="0" stroke-dasharray="300" stroke-miterlimit="10" stroke-linecap="round"></circle>
</svg>
Final solution:最终解决方案:
html,body { background: #5daf47; } .spinner { height: 150px; width: 150px; transform-origin: center center; background-size: 150px; background-image: url('https://dl.dropboxusercontent.com/u/3057457/spinner2.svg'); }
<div class="spinner"></div>
Thanks everybody for help.谢谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.