是否有可能使元素忽略位置:相对?

时间:2022-10-26 22:27:27

Just want to know if it is possible to make an element ignore any wrappers with position:relative.

只想知道是否有可能使一个元素忽略任何包含position:relative的包装器。

I'm creating elements with jQuery which is being placed with position absolute, and needs to work no matter how the user is placing their elements.

我正在使用jQuery创建元素,这些元素放置在绝对位置,无论用户如何放置元素,都需要工作。

I can't place them in the BODY-tag, because I need to place them inside a wrapper together with an inputfield.

我不能将它们放在BODY标签中,因为我需要将它们与输入字段一起放在包装器中。

Any ideas?

有任何想法吗?

UPDATE: Found a workaround. I was using jQuery .offset() which is getting position of the document. Edited it to .position() which is getting position of the element inside the parent. Now it works

更新:找到一个解决方法。我正在使用jQuery .offset()来获取文档的位置。编辑了.position(),它获取了父元素中元素的位置。现在它有效

2 个解决方案

#1


0  

Maybe I'm missing something, but you can monitor for a resize event and then re-position your element. http://api.jquery.com/resize/

也许我错过了一些东西,但你可以监控resize事件,然后重新定位你的元素。 http://api.jquery.com/resize/

#2


0  

A though would be to set the left and top attributes as if there were no position: relative-parents and add lets say class "body_pos":

虽然将左侧和顶部属性设置为没有位置:relative-parents和add让我们说类“body_pos”:

$(function() {
  $('.body_pos').each(function() {
    var offset = $(this).offset();
    $(this).css({
      left : '-='+offset.left,
      top : '-='+offset.top
    });
  });
});

Example: You've set left: 50px, the element is positioned at left: 100px in the document, then it will be positioned left: -50px.

示例:您已设置左:50px,元素位于左侧:文档中的100px,然后它将位于左侧:-50px。

It should grant desired effect as long as it's not positioned inside a margin: 0 auto box, and the window is resized I suppose.

只要它没有位于边距内:0自动框,它应该给予期望的效果,并且我认为窗口已调整大小。

#1


0  

Maybe I'm missing something, but you can monitor for a resize event and then re-position your element. http://api.jquery.com/resize/

也许我错过了一些东西,但你可以监控resize事件,然后重新定位你的元素。 http://api.jquery.com/resize/

#2


0  

A though would be to set the left and top attributes as if there were no position: relative-parents and add lets say class "body_pos":

虽然将左侧和顶部属性设置为没有位置:relative-parents和add让我们说类“body_pos”:

$(function() {
  $('.body_pos').each(function() {
    var offset = $(this).offset();
    $(this).css({
      left : '-='+offset.left,
      top : '-='+offset.top
    });
  });
});

Example: You've set left: 50px, the element is positioned at left: 100px in the document, then it will be positioned left: -50px.

示例:您已设置左:50px,元素位于左侧:文档中的100px,然后它将位于左侧:-50px。

It should grant desired effect as long as it's not positioned inside a margin: 0 auto box, and the window is resized I suppose.

只要它没有位于边距内:0自动框,它应该给予期望的效果,并且我认为窗口已调整大小。