首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >全宽和全高SVG

全宽和全高SVG
EN

Stack Overflow用户
提问于 2012-02-22 01:44:16
回答 2查看 59K关注 0票数 14

难题:制作一个全窗口svg图像,它填充了宽高比失真,而不使用SVG标签。为什么没有svg标签?因为我打算在页面生命周期中稍后(如果不是频繁的话)换出SVG,而我还没有找到一种简单的方法来做到这一点。

失败的尝试:

代码语言:javascript
复制
  <!-- 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);" />

我也试过

代码语言:javascript
复制
 background-size:contain;
 background-size:cover;
 background-size:100% 100%;
 background-postion: center center;

但没那么走运。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-22 02:26:28

我使用以下命令在Firefox、Chrome和Safari中运行

代码语言:javascript
复制
<img src="my.svg" style="width:100%;height:100%;position:fixed;top:0;left:0;bottom:0;right:0;" />

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

例如:

代码语言:javascript
复制
<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文件的内容。:-)

票数 57
EN

Stack Overflow用户

发布于 2014-06-21 03:07:59

这是一个jQuery解决方案。正如您所看到的,我将它与不带<svg>的SVG一起使用

css

代码语言:javascript
复制
#bgImage{
    position:absolute;
    left:0;
    top:0;
}

这个html

代码语言:javascript
复制
<object width="10" height="10" id="bgImage" data="resources/runner.svg" type="image/svg+xml"></object>

javascript

代码语言: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”上。

代码语言:javascript
复制
$(window).resize(function(){
    resizeImage($("#bgImage"));
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9382434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档