简体   繁体   English

使用JavaScript更改SVG图像的大小?

[英]Changing the size of the SVG image using JavaScript?

I have a .svg file that has the following code: 我有一个.svg文件,其中包含以下代码:

<svg version="1.1" x="0" y="0" width="256" height="256" viewBox="0 0 335 394">

plus a ton more code (namespaces etc) that is unnecessary to this situation, but the problem I have is that I need to somehow alter that width and height (from 256px to arbitrary size) using JavaScript. 再加上很多这种情况不需要的代码(命名空间等),但是我的问题是我需要以某种方式使用JavaScript更改宽度和高度(从256px更改为任意大小)。 The .svg is being used like this: .svg的用法如下:

<html>
<body>
<div style="background: url(example.svg);"></div>
</body>
</html>

I would really need to accomplish this somehow. 我真的需要以某种方式完成此操作。 Any ideas? 有任何想法吗?

Update: I need this because I want a user to be able to set the base size for my user interface via a JavaScript control. 更新:我需要这样做,因为我希望用户能够通过JavaScript控件设置用户界面的基本大小。

How about putting an <img> tag inside the div, and setting the width and the height on that? 如何在div内放置<img>标签,并在其上设置宽度和高度呢?

<html>
  <body>
    <div>
      <img src="example.svg" width="10px" height="10px" />
    </div>
  </body>
</html>

This will allow your browser to automatically scale the image based on the given dimensions. 这将使您的浏览器可以根据给定的尺寸自动缩放图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM