Responsive SVGs on InternetExplorer with SVG injection

VickyB

To make SVGs responsive on our website with Internet Explorer 11 I am using the "Canvas Hack" by Nicholas Gallagher. This hack uses an extra canvas element to make use the SVG keeps the aspect ratio. The whole structure of the inline SVGs looks something like this.

HTML:

<div style="position:relative;width:100%;">
  <canvas width="256" height="256"></canvas>
  <svg viewBox="0 0 256 256" preserveAspectRatio="xMaxYMax meet">
    ...
  </svg>
</div>

CSS:

canvas {
    display: block;
    width: 100%;
    visibility: hidden;
}

svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
}

I am using SVGInject to make the SVGs inline, which means the SVGs are kept is separate files. Before SVG injection the HTML looks like this:

<div style="position:relative;width:100%;">
  <canvas width="256" height="256"></canvas>
  <img src="myimage.svg" onload="SVGInject(this)" />
</div>

This works well, but maintenance is very annoying, because for each SVG the values of width and height for the canvas must be manually set to match the SVG's aspect ratio. And since the SVGs are kept in separate files, I have to open the SVG every time to find out the aspect ratio.

So I was wondering, is this something that could automatically be done during injection?.

My ideas was to create a script that during injection somehow reads the SVG's aspect ratio from the viewBox attribute and then set the width and height for the canvas accordingly.

Waruyama

SVGInject provides the following hooks to the injection: beforeLoad, afterLoad, beforeInject and afterInject. In your case you can use afterInject to modify the SVG, its parent, siblings, etc..

With using the afterInject hook, you can not only set the width and height attributes of the <canvas> element, but you can even check if Internet Explorer is running and only insert the canvas in that case. This will make your HTML much cleaner.

Here is a script (using jQuery) that will add the canvas only on Internet Explorer. In the <head> add these lines (the one including svg-inject.js should alrady be in your code):

<script src="svg-inject.js"></script>
<script>
  SVGInject.setOptions({afterInject: function(img, svg) {
    if (/Trident|MSIE/.test(window.navigator.userAgent)) { // if Internet Explorer
      var $svg = $(svg);
      // Get width and height from viewBox attribute if available
      var viewBoxVals = ($svg.attr('viewBox') || '').split(/[\s,]+/);
      var width = parseInt(viewBoxVals[2]);
      var height = parseInt(viewBoxVals[3]);
      if (width > 0 && height > 0) {
        // Set position of parent div to relative
        $svg.parent().css({position: 'relative'});
        // Add canvas using width and height from viewBox
        $svg.before('<canvas height="' + height + '" width="' + width + '"' +
         'style="display: block; width: 100%; visibility: hidden;"></canvas>');
        // Set SVG attributes to make it fill space reserved by canvas.
        $svg.css({position: 'absolute', top: 0, left: 0});
      }
    }
  }})
</script>

After the SVG is injected, the script checks if Internet Explorer is running. If so, it extracts the width and height values from the viewBox attribute and inserts a canvas before the SVG. Also, the parent's and the SVG's attributes are set to make the SVG responsive.

SVGs can then be simply added like this:

<div style="width:100%;">
  <img src="myimage.svg" onload="SVGInject(this)" />
</div>

No need to add the canvas in your HTML, it will be automatically inserted on Internet Explorer (and only on Internet Explorer).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

responsive SVG using viewbox

分類Dev

Responsive SVG background / tailwindcss

分類Dev

Svg responsive width with adjustable height

分類Dev

Trying to make SVG height responsive

分類Dev

Responsive clip-path SVG

分類Dev

InternetExplorerを使用した動的SVGの生成

分類Dev

Angular ng-bind-html svg into svg InternetExplorerが機能しない

分類Dev

SVGインラインCSSがInternetExplorerに表示されない

分類Dev

FirefoxとInternetExplorerでSVGをシャープにする方法

分類Dev

InternetExplorerでのSVGコンテナの問題

分類Dev

Chrome72はcssbackground svgsの 'data:image / svg + xml; utf8'を壊しましたか?

分類Dev

svgマスクされた画像をInternetExplorerと互換性のあるものにする方法

分類Dev

SVGアイコンがInternetExplorer11に正しく配置されていません

分類Dev

InternetExplorerのsvgでAppendChildを使用すると間違ったことをしている

分類Dev

<img>タグのソースとしてのSVGがInternetExplorer11に表示されない

分類Dev

align two SVGs at the center of their container

分類Dev

Transform is not applied on Embedded SVGs Chrome

分類Dev

TranspillerとInternetExplorer 8

分類Dev

InternetExplorerのBlazor

分類Dev

Dojo、InternetExplorerでsetAttribute

分類Dev

php-svg-libをdompdfに追加して、pdfでsvgsを使用できるようにするにはどうすればよいですか?

分類Dev

Can I write an InternetExplorer.document to another InternetExplorer window?

分類Dev

Fancybox InternetExplorerの問題

分類Dev

InternetExplorerのLeafletJS0.8

分類Dev

InternetExplorerの高速化

分類Dev

InternetExplorerの互換性

分類Dev

how to stretch/resize svgs in uwp's xaml?

分類Dev

Large SVGs not rendering...my fault? browsers?

分類Dev

Responsive design

Related 関連記事

  1. 1

    responsive SVG using viewbox

  2. 2

    Responsive SVG background / tailwindcss

  3. 3

    Svg responsive width with adjustable height

  4. 4

    Trying to make SVG height responsive

  5. 5

    Responsive clip-path SVG

  6. 6

    InternetExplorerを使用した動的SVGの生成

  7. 7

    Angular ng-bind-html svg into svg InternetExplorerが機能しない

  8. 8

    SVGインラインCSSがInternetExplorerに表示されない

  9. 9

    FirefoxとInternetExplorerでSVGをシャープにする方法

  10. 10

    InternetExplorerでのSVGコンテナの問題

  11. 11

    Chrome72はcssbackground svgsの 'data:image / svg + xml; utf8'を壊しましたか?

  12. 12

    svgマスクされた画像をInternetExplorerと互換性のあるものにする方法

  13. 13

    SVGアイコンがInternetExplorer11に正しく配置されていません

  14. 14

    InternetExplorerのsvgでAppendChildを使用すると間違ったことをしている

  15. 15

    <img>タグのソースとしてのSVGがInternetExplorer11に表示されない

  16. 16

    align two SVGs at the center of their container

  17. 17

    Transform is not applied on Embedded SVGs Chrome

  18. 18

    TranspillerとInternetExplorer 8

  19. 19

    InternetExplorerのBlazor

  20. 20

    Dojo、InternetExplorerでsetAttribute

  21. 21

    php-svg-libをdompdfに追加して、pdfでsvgsを使用できるようにするにはどうすればよいですか?

  22. 22

    Can I write an InternetExplorer.document to another InternetExplorer window?

  23. 23

    Fancybox InternetExplorerの問題

  24. 24

    InternetExplorerのLeafletJS0.8

  25. 25

    InternetExplorerの高速化

  26. 26

    InternetExplorerの互換性

  27. 27

    how to stretch/resize svgs in uwp's xaml?

  28. 28

    Large SVGs not rendering...my fault? browsers?

  29. 29

    Responsive design

ホットタグ

アーカイブ