如何将img放在svg标签前面

时间:2014-08-13 01:59:48

标签: javascript jquery html svg snap.svg

我正在使用snap.svg 我有index.html

<!Doctype>
<html>
<head>
    <title>MAP_TEST</title>
    <meta charset="utf-8">
    <script type = "text/javascript" src = "JS/jquery.js"></script>
    <script type = "text/javascript" src = "JS/init.js"></script>
    <script type = "text/javascript" src = "JS/snap.svg.js"></script>
</head>
<body>
    <div class="comm_cont">
        <div id = "svgborder">
            <svg id = 'svgmain'></svg>
        </div>
    </div>
</body>
</html>

和init.js

$( document ).ready(function() {
    var s = Snap("#svgmain");
    var g = s.group();
    Snap.load("SVGFILES/3k1e-test.svg",function(lf)
    {
        g.append(lf);
        //trying to load picture... Scale button in future
        $('<img />', {
                src: 'PNG/plus.png',
                width: '30px',
                height: '30px',
                id: 'buttoninrk'
        }).appendTo($('.comm_cont'));
        //this button must be on picture
        //but in front of the picture svg element
        //And i can't click the button
    });
});

我使用了 #svgborder #buttoninkr 的z索引,但它对我没有帮助。 如何将按钮放在svg元素前面?

#buttoninkr, #svgborder
{
    position: absolute;
}
#svgborder
{
    border:5px solid black;
    z-index: 0;
    margin-left:auto;
    border-radius: 5px;
    display: inline-block;
}
#buttoninkr
{
    z-index: 1;
}

添加了带z-indexes的css代码。 有一个原因我不使用svg按钮而不是jquery图像按钮。

好的,你可以在plus.png前面看到#svgmain  http://jsfiddle.net/3wcq9aad/1/

有什么想法吗?

解决

 #svgborders
  {
      position: absolute;
      background-color: #535364;
      border:5px solid black;
      z-index: 0;
      margin-left:auto;
      border-radius: 5px;
      display: inline-block;
  }
 #buttoninrk, #buttondekr, #home_btn
 {
     position: inherit;
     top:0;
     margin:10px;
     z-index: 1;
 }
 #buttoninrk
 {
     right:0px;
 }
 #buttondekr
 {
     right:60px
 }

2 个答案:

答案 0 :(得分:0)

编辑:这不是div的位置产生差异,而是简单地添加宽度和高度。因此,只要在CSS中添加svgborder的宽度和高度,原始HTML就可以正常工作:

http://jsfiddle.net/3wcq9aad/4/

(请注意,有时,文档中元素的位置会对z-index的工作方式产生影响。)


如果你把svgborder div放在svg之前,那么z-index会起作用,但是你需要知道你的SVG的宽度和高度,并在svgborder div上设置它。

<body>
    <div class="comm_cont">
        <div id="svgborder"></div>
        <svg id='svgmain'></svg>
    </div>
</body>

#svgborder
{
    z-index: 2;
    width:330px;
    height:150px;
    ...
}

小提琴:http://jsfiddle.net/3wcq9aad/3/

答案 1 :(得分:0)

svg不支持z-index 改为使用元素位置: $(&#39;元素&#39;)。css(&#39;位置&#39;,&#39;绝对&#39;);

Is there a way in jQuery to bring a div to front?

相关问题