2012-02-21 71 views
8

困境:製作一個完整的窗口svg圖像,填充WITH aspect變形,而不使用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; 

,但沒有運氣。

+0

可能重複[如何縮放與標記嵌入的頑固svg?](http://stackoverflow.com/questions/644896/how-do-i-scale-a-stubborn-svg-嵌入的對象標籤) – givanse 2014-05-20 23:01:32

+1

我的帖子在哪裏是''標籤? – Fresheyeball 2014-05-21 03:57:36

回答

46

我得到這個在火狐,Chrome和Safari使用

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

訣竅是讓工作確保我顯示SVG有preserveAspectRatio =「無」根設置。另外,我不得不刪除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文件的內容控制。 :-)

+0

這工作完美。顯然,它是Adobe Illustrator,搞亂了我! – Fresheyeball 2012-02-22 07:19:27

+4

'preserveAspectRatio =「none」'提示真的救了我。謝謝! – inorganik 2012-12-19 20:42:59

+1

對於其他人,請注意明顯的位置:固定應該是position:absolute,如果您希望它住在父級內。 – 2014-02-24 04:17:38

2

這裏是一個jQuery解決方案。正如你所看到的,我使用它與SVG無<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")); 
} 
+0

刪除內聯js,我會upvote you – Fresheyeball 2014-06-20 21:44:32

+0

修復?我很猶豫要刪除jQuery選擇參數,因爲我喜歡這種靈活性。我想只是調用resizeImage()會更容易,但更多的限制。 – Adam 2014-06-24 02:55:37

+0

@Fresheyeball upvote? – Adam 2014-07-29 23:42:50