2012-02-21 1 views
8

Sự tiến thoái lưỡng nan: làm cho một hình ảnh svg đầy đủ cửa sổ lấp đầy với méo khía cạnh, KHÔNG sử dụng thẻ SVG. Tại sao không có thẻ svg? Bởi vì tôi có ý định trao đổi SVG sau này (nếu không thường xuyên) trong cuộc đời của trang, và tôi đã không tìm thấy một cách dễ dàng để làm điều đó.Chiều rộng và chiều cao đầy đủ SVG

Những nỗ lực thất bại:

<!-- 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);" /> 

Tôi cũng đã cố gắng

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

nhưng không có may mắn.

+0

bản sao có thể có của [Làm cách nào để chia tỷ lệ một svg cứng đầu được nhúng với thẻ ?] (Http://stackoverflow.com/questions/644896/how-do-i-scale-a-stubborn-svg- nhúng-với-the-object-tag) – givanse

+1

Trong bài đăng của tôi là thẻ '' ở đâu? – Fresheyeball

Trả lời

46

Tôi có này để làm việc trong Firefox, Chrome, và Safari sử dụng

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

Bí quyết là để đảm bảo SVG tôi đã hiển thị có preserveAspectRatio = "none" đặt trong thư mục gốc. Ngoài ra, tôi phải xóa hộp xem trong SVG hoặc đảm bảo rằng nó đã cắt chặt nội dung hình ảnh.

Ví dụ:

<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> 

Hy vọng rằng bạn có thể kiểm soát nội dung của các tập tin SVG bạn đang cố gắng để hiển thị. :-)

+0

Điều này làm việc hoàn hảo. Rõ ràng đó là Adobe Illustrator đã gây rối với tôi! – Fresheyeball

+4

Mẹo 'preserveAspectRatio =" none "' thực sự đã lưu tôi. Cảm ơn! – inorganik

+1

Đối với những người khác, lưu ý rằng rõ ràng vị trí: cố định nên được vị trí: tuyệt đối nếu bạn muốn điều này để sống bên trong một phụ huynh. –

2

Đây là giải pháp jQuery. Như bạn thấy, tôi đang sử dụng nó với một SVG mà không <svg>

Các css

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

Các html

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

Các 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 
    } 
} 

Run này bất cứ khi nào bạn muốn đổi kích thước hình ảnh, thường là "onresize" và "onload"

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

xóa js nội tuyến và tôi sẽ upvote bạn – Fresheyeball

+0

Khắc phục? Tôi do dự để loại bỏ các tham số lựa chọn jQuery, vì tôi thích sự linh hoạt này. Tôi cho rằng chỉ cần gọi resizeImage() sẽ dễ dàng hơn, nhưng bị hạn chế hơn. – Adam

+0

@Fresheyeball upvote? – Adam