2012-02-21 22 views
8

दुविधा: एक पूर्ण विंडो 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

संभव डुप्लिकेट http://stackoverflow.com/questions/644896/how-do-i-scale-a-stubborn-svg- एम्बेडेड-द-ऑब्जेक्ट-टैग) – givanse

+1

मेरी पोस्ट में '' टैग है? – Fresheyeball

उत्तर

46

मैं इस बनाने के लिए

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

का उपयोग कर चाल था फायरफॉक्स, क्रोम, और सफारी में काम मिल गया यकीन एसवीजी मैं प्रदर्शित किया गया था preserveAspectRatio = "कोई नहीं" रूट में सेट किया था। इसके अलावा, मुझे या तो एसवीजी में व्यूबॉक्स को हटाना पड़ा था, या यह सुनिश्चित करना था कि यह छवि सामग्री को कसकर फसल कर देगा।

उदाहरण के लिए:

<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

यह सही काम किया। जाहिर है यह एडोब इलस्ट्रेटर था जो मेरे साथ गड़बड़ कर रहा था! – Fresheyeball

+4

'preserveAspectRatio =" none "' tip ने वास्तव में मुझे बचाया। धन्यवाद! – inorganik

+1

दूसरों के लिए, ध्यान दें कि स्पष्ट रूप से स्थिति: निश्चित स्थिति होना चाहिए: पूर्ण अगर आप इसे माता-पिता के अंदर रहना चाहते हैं। –

2

यहां एक jQuery समाधान है। आप देख सकते हैं, मैं इसे उपयोग कर रहा हूँ एक एसवीजी के साथ बिना <svg>

सीएसएस

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

एचटीएमएल

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

जावास्क्रिप्ट

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

भागो इस जब भी आप छवि का आकार बदलना चाहते हैं, आमतौर पर "ऑन्रेसइज़" और "ऑनलोड"

पर
$(window).resize(function(){ 
    resizeImage($("#bgImage")); 
} 
की [मैं एक जिद्दी svg टैग के साथ एम्बेडेड कैसे पैमाने पर करते हैं?] (
  0

इनलाइन जेएस को हटा दें और मैं आपको 20 jun. 142014-06-20 21:44:32

  0

हटा दूंगा? मुझे jQuery चयन पैरामीटर को हटाने में संकोच नहीं है, क्योंकि मुझे इसकी लचीलापन पसंद है। मुझे लगता है कि बस resizeImage() को कॉल करना आसान होगा, लेकिन अधिक बाध्य होगा। 24 jun. 142014-06-24 02:55:37

  0

@Fresheyeball upvote? 29 jul. 142014-07-29 23:42:50

  0

काफी मेला, किया 30 jul. 142014-07-30 04:08:01


+0

इनलाइन जेएस को हटा दें और मैं आपको – Fresheyeball

+0

हटा दूंगा? मुझे jQuery चयन पैरामीटर को हटाने में संकोच नहीं है, क्योंकि मुझे इसकी लचीलापन पसंद है। मुझे लगता है कि बस resizeImage() को कॉल करना आसान होगा, लेकिन अधिक बाध्य होगा। – Adam

+0

@Fresheyeball upvote? – Adam

संबंधित मुद्दे