2016-10-04 60 views
0

我有一個網頁有一個袋子的圖像,並且在袋子圖像上面放置了一個svg覆蓋圖,問題在於svg沒有在圖像上覆蓋,也沒有響應。Svg以上的圖像和比例

工作實施例: https://jsbin.com/lilaxizizo/1/edit

代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <title>SVG </title> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

<!-- Optional theme --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 
<!-- Latest compiled and minified JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 

</head> 
<body> 
<div class="container"> 
<div id="wrapper"> 
<img class="img-responsive" src="duffell_bag.jpg" alt="Planets" usemap="#bag"> 


<svg style="position:absolute;z-index:10" height="300" width="500"> 
    <polygon points="103,79,119,74,136,68,173,67,202,70,262,78,316,89,380,97,393,121,402,157,407,184,418,216,424,249,419,261,404,274,359,288,332,294,271,284,195,266,142,255,83,238,59,223,64,177" style="fill-opacity:0.1;fill:lime;stroke:purple;stroke-width:1" /> 
    Sorry, your browser does not support inline SVG. 
</svg> 

</div> 
</div> 


</body> 

</html> 

回答

-1

添加 '邊距' 到SVG元素。我已經測試過它,並且'-305px'的值似乎有訣竅。

<svg style="margin-top:-305px;position:absolute;z-index:10" height="300" width="500"> 
+0

沒有響應,嘗試調整它,你給的解決方案是非常錯誤的,如果繼續調整窗口大小,是不是穩定 – Pedro

+0

我做了谷歌搜索只是在「縮放一個SVG',並且有無數的資源(大部分已經在stackoverflow上)都提到了viewBox。當你真正陷入困境時,Stackoverflow可以提供幫助。請在未來研究。 –

2

首先,如果你想要SVG響應,你需要給它一個viewBox。看到這個問題的更多細節。

How can I make an svg scale with its parent container?

接下來,如果您希望SVG坐在圖像的頂部,包圍他們都與一個postion: relative<div>。這樣,如果你絕對SVG的位置,它將相對於div而不是整個頁面。

div { 
 
    position: relative; 
 
} 
 

 
img, svg { 
 
    width: 500px; 
 
} 
 

 
svg { 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
}
<div> 
 
    
 
    <img class="img-responsive" src="http://i.imgur.com/icQzdFl.jpg"> 
 

 
    <svg height="300" width="500"> 
 
    <polygon points="103,79,119,74,136,68,173,67,202,70,262,78,316,89,380,97,393,121,402,157,407,184,418,216,424,249,419,261,404,274,359,288,332,294,271,284,195,266,142,255,83,238,59,223,64,177" style="fill-opacity:0.1;fill:lime;stroke:purple;stroke-width:1" /> 
 
    </svg> 
 

 
</div>

+0

這是固定大小;不響應。 – frezq