使内联SVG在浏览器中响应而不使用javascript? [英] Making inline SVG responsive in browsers without using javascript?

查看:47
本文介绍了使内联SVG在浏览器中响应而不使用javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在评论类似这样的文章哪些功能确实不错,但可能会过时,我想知道在保持宽高比的同时使内嵌svg响应的简单方法是否取得了进展?

I've been reviewing articles like this one which are really good, but possibly dated, and I'm wondering if any progress has been made on a simple way to make inline svg responsive while maintaining the aspect ratio?

换句话说,如果在div容器中有svg,其纵横比为1:1,并且容器从400x400px缩小到200x200 px,则视图框的宽度和高度参数会加倍.

In other words if we have svg with a 1:1 aspect ratio inside a div container, and the container shrinks from 400x400px to 200x200 px, then the view box width and height parameters double.

推荐答案

我对想要的东西感到困惑.当您谈论视图框的宽度和高度参数加倍"时,我不知道您的意思.也许您对viewBox的工作方式感到困惑.

I am confused about what you want. I don't know what you mean, in your question, when you talk about "view box width and height parameters double". Perhaps you are confused about how viewBox works.

只要SVG具有viewBox,它就应该具有响应性.

As long as the SVG has a viewBox it should be responsive.

在下面的示例中,我将SVG放入了div容器中,并对容器的大小进行了动画处理以模拟页面大小的变化.

In the following example, I've put the SVG in a div container and animated the container size to simulate the page size changing.

#container {
  width: 200px;
  background: linen;
  animation: scale 1s alternate infinite;
}


@keyframes scale {
  from { width: 200px; }
  to   { width: 100px; }
}

<div id="container">

  <svg viewBox="0 0 100 100">
    <circle cx="50" cy="50" r="40" fill="gold"/>
    <rect x="10" y="50" width="20" height="50" fill="gold"/>
    <rect x="70" y="50" width="20" height="50" fill="gold"/>
    <circle cx="35" cy="45" r="5"/>
    <circle cx="65" cy="45" r="5"/>
  </svg>

</div>
    

但是也可以很容易地将其作为页面宽度的容器.尝试运行以下代码段.然后单击整页"并调整浏览器窗口的大小.:

But it could just as easily be a container that is the width of the page. Try running the following snippet. Then click "Fullpage" and resize the browser window.:

#container {
  width: 100%;
  background: linen;
}

<div id="container">

  <svg viewBox="0 0 100 100">
    <circle cx="50" cy="50" r="40" fill="gold"/>
    <rect x="10" y="50" width="20" height="50" fill="gold"/>
    <rect x="70" y="50" width="20" height="50" fill="gold"/>
    <circle cx="35" cy="45" r="5"/>
    <circle cx="65" cy="45" r="5"/>
  </svg>

</div>

这篇关于使内联SVG在浏览器中响应而不使用javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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