简体   繁体   English

超过图像和比例的SVG

[英]Svg above a image and scale

I have a page that haves a image of a bag, and above the bag image i putted a svg overlay, the problem is that the svg is not overllaping on the image and also is not being responsive. 我的页面上有一个包的图像,在包图像上方,我放了一个svg叠加层,问题是svg不会在图像上重叠,也没有响应。

Working example: https://jsbin.com/lilaxizizo/1/edit 工作示例: https//jsbin.com/lilaxizizo/1/edit

code: 码:

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

First, if you want the SVG to be responsive, you need to give it a viewBox . 首先,如果您想让SVG响应,则需要为其提供一个viewBox See this question for more details. 有关更多详细信息,请参见此问题。

How can I make an svg scale with its parent container? 如何使用其父容器制作svg秤?

Next, if you want the SVG to sit on top of the image, surround them both with a <div> with postion: relative . 接下来,如果您想让SVG位于图片上方,请用<div>postion: relative包围它们。 That way, if you absolutle position the SVG, it will be relative to the div and not the whole page. 这样,如果您绝对定位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> 

Add 'margin-top' to the svg element. 将'margin-top'添加到svg元素。 I've tested it and a value of '-305px' seems to do the trick. 我已经测试过,“-305px”的值似乎可以解决问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM