缩放svg背景图像以适合容器

时间:2015-11-18 13:22:31

标签: javascript jquery html css svg

我有一个具有一定高度和宽度的简单div容器。我需要一张svg图片作为我的div ::after的背景图片。

这些是我的::after

的样式
    &:after{
        content:'';
        background-image: url('../images/svg/triangle_blue.svg');
        background-repeat: no-repeat;
        background-size: auto auto;
        position:absolute;
        right: 0;
        top: 0;
        transform: translateX(100%);
        display:inline-block;
        width: 50px;
        height: 100%;
    }

这就是我的svg的头像:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewbox="0 0 50 100" width="100%" height="100%" preserveAspectRatio="none">

现在我希望svg始终缩放比例以适合::after容器。 我错过了什么?

1 个答案:

答案 0 :(得分:1)

找到一个有效的解决方案。这是我的 <!DOCTYPE html> <html lang="nl"> <head> <meta charset="utf-8"> <title>Lingo spel</title> <meta name="Lingo" content="Making an lingo project for school"> <meta name="Author" content="Ronald Julian Dewindt"> <meta name="Made Date" content="18-11-2015"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script> <link href='https://fonts.googleapis.com/css?family=Oswald|Montserrat|Raleway|Poiret+One|Indie+Flower|Dosis' rel='stylesheet' type='text/css'> <link rel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <link rel="stylesheet" type="text/css" href="style.css"> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="border col-xs-12 col-sm-12 col-md-12 col-lg-12"> <div class="row"> <!--<div class="col-xs-12"> <img src="lingo_logo.jpg"> </div>--> <div class="col-xs-12"> <label>Typ een eu land hier in:</label> <input class="form-control input" type="text" id="woord"> <button class="btn btn-primary" type="button" onclick="lingo()">Check</button> <p style="padding-left:10px; display:inline-block;"><b>punten:</b></p><p style="display:inline-block;" id="punten">0</p> <p style="padding-left:20px; display:inline-block;"><b>Kansen:</b></p><p style="display:inline-block;" id="kans">30</p> </div> <div class="col-xs-5"> <h4>Woorden Geprobeerd</h4> <p class="try" id="try"></p> </div> <div class=" col-xs-3"> <h4>Correcte woord</h4> <p class="word" id="hiddenWord"></p> </div> <script type="text/javascript"> var euLanden = new Array('Nederland','Belgie','Bulgarije','Cyprus','Denemarken','Duitsland','Estland','Finland','Frankrijk','Griekeland','Ierland','Italie','Luxemburg','Malta','Oostenrijk','Polen','Spanje','Zweden','Hongarije','Kroatie','Letland','Litouwen','Portugal','Roemenie','Slovenie','Tsjechie'); var random = euLanden[Math.floor(Math.random() * euLanden.length)]; var woordenTry= new Array(); var tryOutput = ""; var punten= 0; var kans= 30; document.getElementById("hiddenWord").innerHTML="?".repeat(random.length); function lingo(countryName, textInput) { textInput = document.getElementById("woord").value; textInput = textInput.toLowerCase(); countryName = random.toLowerCase(); var output = ""; for(var i=0; i< countryName.length; i++) { if(textInput.indexOf(countryName[i])!== -1) { output += countryName[i]; } else { output += "?"; } } woordenTry.push(output); for(w=0; w<woordenTry.length; w++) { document.getElementById("hiddenWord").innerHTML=output; tryOutput+= woordenTry[w] + "<br>"; document.getElementById("try").innerHTML=tryOutput; } if(textInput != countryName) { kans--; } if(kans == 0) { alert("U heeft verloren, Probeer opnieuw"); location.reload(); } if(textInput === countryName) { random = euLanden[Math.floor(Math.random() * euLanden.length)]; random.length alert("Je hebt het woord gevonden"); punten++; alert("Zoek het nieuw woord op!"); document.getElementById("hiddenWord").innerHTML="?".repeat(countryName.length); } document.getElementById("punten").innerHTML=punten; document.getElementById("kans").innerHTML=kans; } </script> </div> </div> </div> </body> </html> 代码:

svg

的CSS:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="Ebene_1" x="0px" y="0px" viewBox="0 0 50 100" width="100%" height="100%" enable-background="new 0 0 50 100" xml:space="preserve" preserveAspectRatio="none"><g><polygon fill="#009EE0" points="0 100 50 50 0 0 "/></g></svg>

似乎唯一的区别是

  

的xml:空间=&#34;保留&#34;

background-size: auto auto;

中的