使SVG路径占据屏幕的整个宽度和高度 [英] Make SVG path take full width and height of screen

查看:61
本文介绍了使SVG路径占据屏幕的整个宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内联SVG路径,该路径可以对破折号进行动画处理,使其看起来像蛇游戏.现在,我的问题是如何使该路径占据屏幕的整个宽度和高度,并在屏幕变小或变大时做出响应.

I have an inline SVG path that animates a dash to look like the snake game. Now my question is how to get this path to take up the full width and height of the screen and act responsive if the screen goes smaller or bigger.

HTML

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100%" height="100%" viewBox="0 0 659 522" enable-background="new 0 0 659 522" xml:space="preserve">
    <path class="path" width="100%" height="100%" fill="none" stroke="#00ff00" stroke-width="5" stroke-miterlimit="10" d="M656.5,2.5v517H2.5V2.5H656.5z"
    stroke-dasharray="2042 300"
    stroke-dashoffset="2342" />
</svg>

CSS

.path {
    animation: dash 10s linear infinite;
}

@-webkit-keyframes dash {
    to {
        stroke-dashoffset: 0;
    }
}

在这里摆弄我的代码.

http://jsfiddle.net/c3ar6e5o/

推荐答案

您必须告诉SVG不要使用SVG代码中的 preserveAspectRatio ="none" 来保持其宽高比.只是CSS.

You have to tell the SVG not to maintain its aspect ratio with preserveAspectRatio="none" in the SVG code..then it's just CSS.

JSfiddle演示

html,
body {
  margin: 0;
  height: 100%;
}
svg {
  display: block;
}
.path {
  animation: dash 10s linear infinite;
}
@-webkit-keyframes dash {
  to {
    stroke-dashoffset: 0;
  }
}

<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100%" height="100%" viewBox="0 0 659 522" enable-background="new 0 0 659 522" xml:space="preserve" preserveAspectRatio="none">
  <path class="path" width="100%" height="100%" fill="none" stroke="#00ff00" stroke-width="5" stroke-miterlimit="10" d="M656.5,2.5v517H2.5V2.5H656.5z" stroke-dasharray="2042 300" stroke-dashoffset="2342" />
</svg>

这篇关于使SVG路径占据屏幕的整个宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆