简体   繁体   English

如何在不更改其子偏移的情况下更改父偏移值?

[英]How to change parent offset value without changing its child offset?

I have a problem with element positioning. 我对元素定位有疑问。 I have a div element which further contains the svg path element. 我有一个div元素,其中进一步包含svg path元素。 Mark-up is something like this : 标记是这样的:

 <div style="position:absolute;" class="svg">
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
   <path id="path203" d="M150 0 L75 200 L225 200 Z" />
  </svg>
 </div>

I want to set the border around the path element when user mouseover the path. 当用户将鼠标悬停在路径上时,我想在路径元素周围设置边框。 For this i have to access the height and weight of the path element and then set those value as an height and width of its parent div element. 为此,我必须访问path元素的高度和重量,然后将这些值设置为其父div元素的高度和宽度。 For this i used getBoundingClientRect(). 为此,我使用了getBoundingClientRect()。 Code : 代码:

 var box = document.getElementById("path203").getBoundingClientRect();
 $("#path203").parents("div.svg").css({ width: box.width + "px", height: box.height + "px" });

Here my problem is not fully solved by this i only get the border, but the position of div and its children path element is not the same both have different offset values. 在这里,我的问题并没有完全解决,因为我只得到了边框,但是div的位置及其子路径元素都不相同,两者具有不同的偏移值。 So for this i set the top and left for the parent div also : 所以为此我也为父div设置了顶部和左侧:

 var box = document.getElementById("path203").getBoundingClientRect();
 $("#path203").parents("div.svg").offset({ left: box.left + "px", top: box.top });

Now by this div got the correct position but its child path element move away from its position. 现在,此div位置正确,但是其子路径元素偏离其位置。 May be the reason is because the path element is the child of div element. 可能是因为path元素是div元素的子元素。 So when we move div , all of its child elements will also move simultaneously. 因此,当我们移动div时,其所有子元素也将同时移动。 How can i change the parent div offset value without changing its child element offset ? 如何在不更改其子元素偏移的情况下更改父div偏移值?

Is this what you're trying to achieve? 这是您要达到的目标吗?

<div style="position:absolute;" class="svg">
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="302" height="202">
   <path id="path203" d="M150 0 L75 200 L225 200 Z" onmouseover="showRect()" onmouseout="hideRect()" />
   <rect id="border203" fill="none" stroke="black" stroke-width="1"/>
   <script>
     function showRect() {
         var bbox = document.getElementById("path203").getBBox();
         var border = document.getElementById("border203");
         border.x.baseVal.value = bbox.x;
         border.y.baseVal.value = bbox.y;
         border.width.baseVal.value = bbox.width;
         border.height.baseVal.value = bbox.height;
     }
     function hideRect() {
         var border = document.getElementById("border203");
         border.width.baseVal.value = 0;
         border.height.baseVal.value = 0;
     }
   </script>
  </svg>
 </div>

You could take the svg outside of parent div and access the div directly via $('div.svg') . 您可以将svg带到父div之外,并通过$('div.svg')直接访问div。

Here is a fiddle: http://jsfiddle.net/FSQrz/ 这是一个小提琴: http : //jsfiddle.net/FSQrz/

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

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