简体   繁体   English

SVG图像在iOS8上无法调整大小

[英]SVG image does not resize on iOS8

I have SVG picture with PNG fallback: 我有带有PNG后备的SVG图片:

 <svg width=64 height=64>
  <image xlink:href="gender-male.svg"
   src="gender-male.png" width="100%" height="100%"> </image>
 </svg>

When I set different size via CSS for the outer SVG, it resizes the inner image correctly on all browsers: 当我通过CSS为外部SVG设置不同的大小时,它将在所有浏览器上正确调整内部图像的大小:

 svg.small {
   width: 50px;
   height: 50px;
 }

But on iOS8, the inner image does not scale and remains in original size: 但是在iOS8上,内部图片无法缩放,并保持原始大小:

iOS8上的SVG

Demo: jsfiddle 演示: jsfiddle

I've tried various changes in SVG header: 我已经尝试了SVG标头中的各种更改:

<svg preserveAspectRatio="xMidYMin"
     x="0px" y="0px" width="283px" height="283px" 
     viewBox="0 0 283.5 283.5" 
     enable-background="new 0 0 283.5 283.5" xml:space="preserve">

The other image: 另一张图片:

<svg preserveAspectRatio="xMidYMin meet"
     x="0px" y="0px"
     viewBox="0 0 283.5 283.5" 
     enable-background="new 0 0 283.5 283.5" xml:space="preserve">

And also various CSS methods ( max-width , absolute position , etc.) for the image but nothing seems to work. 还有图像的各种CSS方法( 最大宽度绝对位置等),但似乎没有任何作用。 Even setting the size in JS does not work. 即使在JS设置大小也不起作用。

Any other ideas? 还有其他想法吗?

The Solution is actually pretty easy, but you must know what is the problem. 解决方案实际上很简单,但是您必须知道问题出在哪里。

The problem is, that the outer SVG element is resized based on its CSS properties, but the inner IMAGE is rendered based on the HTML properties width and height . 问题在于,外部SVG元素是根据其CSS属性调整大小的,而内部IMAGE是根据HTML属性widthheight呈现的。

To make it render correctly, you simply need to change HTML size attributes every time the actual size of the element changes: 为了使其能够正确呈现,您只需在元素的实际大小每次更改时就更改HTML大小属性:

    if ((-1 < navigator.userAgent.indexOf('iPhone')
      || -1 < navigator.userAgent.indexOf('iPad'))
      && window.navigator.userAgent.match(new RegExp('OS ([1-8])_'))) {
     //iOS8 and older cannot correctly resize SVG graphic inside element resized by CSS

        $(window).on('resize.svg-size', function() {
            $('svg').each(function() {
                var me = $(this);

                //if height is set to auto, you may need to fix it first
                me.css('height', me.width() * me.data('ratio'));

                //set HTML attributes to current size defined in CSS
                me.attr('width', me.width());
                me.attr('height', me.height());

                //if there is also another container, fix it as well
                me.parent().height(me.parent().width() * me.data('ratio'));
            });
        });
        //call the handler when the page is loaded
        $(function() { $(window).triggerHandler('resize.svg-size'); });
    } //Fix for gender select size on iOS8 and older

HTML (outer DIV is optional): HTML(外部DIV是可选的):

<div class="image-container">    
  <svg width=64 height=64 data-ratio=1><!-- square image -->
      <image xlink:href="gender-male.svg"
             src="gender-male.png" width="100%" height="100%"> </image>
  </svg>
</div>
<div class="image-container">    
  <svg width=100 height=75 data-ratio="0.75"><!-- 4:3 image -->
      <image xlink:href="gender-female.svg"
             src="gender-female.png" width="100%" height="100%"> </image>
  </svg>
</div>

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

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