简体   繁体   中英

Changing the size of the SVG image using JavaScript?

I have a .svg file that has the following code:

<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. The .svg is being used like this:

<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.

How about putting an <img> tag inside the div, and setting the width and the height on that?

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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