html - 响应式 SVG 矩形

标签 html css svg

found一个很好的 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>

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;
}

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

已阅读 tutorial在这件事上,我遵循了内联 svg 的所有步骤,但我想我遗漏了一些东西。 你能帮我让那个 SVG 矩形响应吗?

最佳答案

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

关于html - 响应式 SVG 矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49918288/

相关文章:

html - Bootstrap 最大列宽度

html - 动态 img(或视频)标签根本不加载资源,HTTP 请求是 "pending"

javascript - 使用 jQuery 添加/删除输入框 - 添加的输入框复制第一个输入框的值

html - 在不同类型的按钮之间切换

css - 垂直对齐 Div 框中图像下的文本?

html - CouchCMS - float 元素未正确 float

css - 无法从 SVG def 更改 SVG 中对象的颜色

html - <a> 链接在 Safari 中中断 SVG 路径转换

html - 将 SVG 居中放置在 DIV 中

javascript - 如何使用 ajax 发布到 razor 页面处理程序?