【问题标题】:Full width and height SVG全宽和全高 SVG
【发布时间】:2012-03-12 01:13:41
【问题描述】:

困境:制作一个全窗口的 svg 图像,不使用 SVG 标签,但不使用 SVG 标签。为什么没有svg标签?因为我打算在页面的生命周期中稍后(如果不经常)换掉 SVG,但我还没有找到一个简单的方法来做到这一点。

失败的尝试:

  <!-- for the record, my style are in a css file, 
       for example purposes they are style attrs-->

  <!-- Working in Webkit but not in firefox, in firefox blocks stretching 
       and places the svg in the middle of the tag-->
  <img src="my.svg" style="width:100%;height:100%;
       position:fixed;top:0;left:0;bottom:0;right:0;" />

  <!-- Both Webkit and Firefox block distortion, so the svg 
       appears centered in the div rather than conforming to the div-->
  <div style="width:100%;height:100%;position:fixed;top:0;
       left:0;bottom:0;right:0;background-size:cover;
       background-image:url(my.svg);" />

我也试过

 background-size:contain;
 background-size:cover;
 background-size:100% 100%;
 background-postion: center center;

但没有运气。

【问题讨论】:

标签: css svg


【解决方案1】:

这是一个 jQuery 解决方案。如您所见,我将它与没有 &lt;svg&gt; 的 SVG 一起使用

CSS

#bgImage{
    position:absolute;
    left:0;
    top:0;
}

html

<object width="10" height="10" id="bgImage" data="resources/runner.svg" type="image/svg+xml"></object>

JavaScript

//resize the background image
function resizeImage($selection){
    //get the ratio of the image
    var imageRatio = $selection.width() / $selection.height();

    //get the screen ratio
    var screenRatio = $(window).width() / $(window).height();

    //if the image is wider than the screen
    if(imageRatio > screenRatio){
        $selection.height($(window).height()); //set image height to screen height
        $selection.width($(window).height()*imageRatio); //set the correct width based on image ratio
    }

    //if the screen is wider than the image
    else{
        $selection.width($(window).width()); //set the image width to the screen width
        $selection.height($(window).width()/imageRatio); //set the correct image height based on the image ratio
    }
}

当你想调整图像大小时运行这个,通常在“onresize”和“onload”上

$(window).resize(function(){
    resizeImage($("#bgImage"));
}

【讨论】:

  • 修复?我对删除 jQuery 选择参数犹豫不决,因为我喜欢它的灵活性。我想只调用 resizeImage() 会更容易,但更受限制。
【解决方案2】:

我让它在 Firefox、Chrome 和 Safari 中使用

<img src="my.svg" style="width:100%;height:100%;position:fixed;top:0;left:0;bottom:0;right:0;" />

诀窍是确保我显示的 SVG 在根目录中设置了 preserveAspectRatio="none"。另外,我必须要么删除 SVG 中的 viewBox,要么确保它紧紧地裁剪了图像内容。

例如:

<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none" viewBox="0 0 5 3">
    <desc>Flag of Germany</desc>
    <rect id="black_stripe" width="5" height="3" y="0" x="0" fill="#000"/>
    <rect id="red_stripe" width="5" height="2" y="1" x="0" fill="#D00"/>
    <rect id="gold_stripe" width="5" height="1" y="2" x="0" fill="#FFCE00"/>
</svg>

希望您能够控制您尝试显示的 SVG 文件的内容。 :-)

【讨论】:

  • 这很完美。显然是 Adob​​e Illustrator 惹恼了我!
  • preserveAspectRatio="none" 提示真的救了我。谢谢!
  • 对于其他人,请注意显然 position:fixed 应该是 position: absolute 如果你希望它存在于父级中。
  • preserveAspectRatio="none" - 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
相关资源
最近更新 更多