响应式 SVG 矩形

pol*_*llx 3 html css svg

找到了一个非常好的 svg 按钮动画示例,但按钮的大小是静态的 - 320px x 60px。我想让按钮响应 - 它是 svg 矩形,但我很难实现这一点。

HTML:

<div class="svg-wrapper">
  <svg class="svg-responsive" viewBox="0 0 320 60" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
    <rect class="shape" width="100%" height="100%"/>
  </svg>
  <div class="text">HOVER</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.svg-wrapper {
  border: 1px solid red;
  position: relative;

  height: 0;
  padding-top: 16.6%;
}
svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.shape {
  width: 100%;
  height: 100%;
  fill: salmon;
  stroke-dasharray: 140 540;
  stroke-dashoffset: -474;
  stroke-width: 8px;
  stroke: #19f6e8;
}

.text {
  color: #fff;
  font-family: "Roboto Condensed";
  font-size: 22px;
  letter-spacing: 8px;
  line-height: 32px;
  position: relative;
  top: -48px;
}

@keyframes draw {
  0% {
    stroke-dasharray: 140 540;
    stroke-dashoffset: -474;
    stroke-width: 8px;
  }
  100% {
    stroke-dasharray: 760;
    stroke-dashoffset: 0;
    stroke-width: 2px;
  }
}

.svg-wrapper:hover .shape {
  -webkit-animation: 0.5s draw linear forwards;
  animation: 0.5s draw linear forwards;
}
Run Code Online (Sandbox Code Playgroud)

https://codepen.io/anon/pen/dePNLG

已经阅读了有关此事的教程,并且我遵循了内联 svg 的所有步骤,但我想我错过了一些东西。你能帮我让 SVG 矩形响应吗?

ibr*_*cin 6

将preserveAspectRatio =“none”属性添加到您的svg