java - 图像上方的 Svg 和比例

标签 java html svg jquery-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>

最佳答案

首先,如果您希望 SVG 具有响应能力,您需要给它一个 viewBox 。请参阅此问题了解更多详细信息。

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

接下来,如果您希望 SVG 位于图像顶部,请用 <div> 将它们包围起来。与 postion: relative 。这样,如果您对 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>

关于java - 图像上方的 Svg 和比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39859226/

相关文章:

javascript - 无法将 EventListener 添加到 javascript 中的对象

html - 网页固定宽度

javascript - 在 D3.JS 中创建元素 block 的最佳方法是什么

html - 使 IE 10 重复 svg 背景

jquery - Html5 拖放 svg 元素

java - 多线程环境中跨整个应用程序的变量的单个实例

Java 我 : easiest way to format strings?

java - Switch语句: Why can't I have the same variable name in different cases? Java

java - 在 Jackson(和 MrBean)的反序列化时使用多态性

javascript - 为什么javascript找不到定义在它下面的div?