html - 如何从这个动画 SVG 中删除多余的空间(填充)?

标签 html css animation svg

这是一个微调器图标,我被困在删除微调器图标周围额外填充的部分。

<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; background: none; display: block; shape-rendering: auto; animation-play-state: running; animation-delay: 0s;" width="36px" height="36px" viewBox="0 0 100 100"
  preserveAspectRatio="xMidYMid">
<g transform="rotate(0 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.9166666666666666s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(30 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.8333333333333334s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(60 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.75s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(90 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.6666666666666666s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(120 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5833333333333334s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(150 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(180 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.4166666666666667s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(210 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.3333333333333333s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(240 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.25s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(270 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.16666666666666666s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(300 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.08333333333333333s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(330 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="0s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g>

最佳答案

您需要将 viewBox 属性更改为 viewBox="17 17 66 66" 我如何知道 viewBox 的新值?

我已将 SVG 元素内的所有内容包装在一个组 #G 中。

在 JavaScript 中,我使用 getBBox() 方法获取组的边界框,并使用边界框的属性来定义 的新值SVG 元素的 viewBox

vievBox = "x y width height"

console.log(G.getBBox())
svg{border:solid}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; background: none; display: block; shape-rendering: auto; animation-play-state: running; animation-delay: 0s;" width="36px" height="36px" viewBox="17 17 66 66"
  preserveAspectRatio="xMidYMid">
  <g id="G">
<g transform="rotate(0 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.9166666666666666s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(30 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.8333333333333334s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(60 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.75s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(90 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.6666666666666666s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(120 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5833333333333334s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(150 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.5s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(180 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.4166666666666667s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(210 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.3333333333333333s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(240 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.25s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(270 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.16666666666666666s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(300 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="-0.08333333333333333s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g><g transform="rotate(330 50 50)" style="animation-play-state: running; animation-delay: 0s;">
  <rect x="47.5" y="17" rx="2.34" ry="2.34" width="5" height="18" fill="#606366" style="animation-play-state: running; animation-delay: 0s;">
    <animate attributeName="opacity" values="1;0" keyTimes="0;1" dur="1s" begin="0s" repeatCount="indefinite" style="animation-play-state: running; animation-delay: 0s;"></animate>
  </rect>
</g>
</g>
</svg>

关于html - 如何从这个动画 SVG 中删除多余的空间(填充)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62630948/

相关文章:

iphone - 表格 View 变成水平 View

html - 在 div 中将内容居中。 margin 0,自动不起作用。也不显示 :inline

html - 三个<li>,左中右

css - 在图像悬停时更改颜色文本(文本位于图像 ex : img > div: p) 下的 div 中

html - 带有 CSS 下拉菜单的 header 中的 iframing - 在 iframe 之外获取下拉菜单的好解决方案?

javascript - Highcharts 条形图不会动画

javascript - 通过 Java JAX-RS REST 服务发送带有 JS 的 HTML

html - 使用 text-align center 使电子邮件表单居中 :

css - 使用 css 使用屏幕空间

swift : Animate SKSpriteNode Gradient