力“位置:绝对”相对于文档而不是父容器

发布于 2024-11-26 18:55:32 字数 113 浏览 1 评论 0原文

我试图将 div 插入正文的任何​​部分,并使其 position:absolute 相对于整个文档,而不是具有 position:relative 的父元素。

I am trying to insert a div into any part of the body and make its position: absolute relative to the whole document and not a parent element which has a position: relative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪我闹别瞎闹 2024-12-03 18:55:32

您正在寻找position:fixed

来自 MDN

固定定位与绝对定位类似,不同之处在于元素的包含块是视口。这通常用于创建即使在滚动页面后也保持在同一位置的浮动元素。

请注意它不起作用当...

(...) 它的祖先之一将 transformperspectivefilter 属性设置为 之外的其他属性无

You're looking for position: fixed.

From MDN:

Fixed positioning is similar to absolute positioning, with the exception that the element's containing block is the viewport. This is often used to create a floating element that stays in the same position even after scrolling the page.

Notice it doesn't work when...

(...) one of its ancestors has a transform, perspective, or filter property set to something other than none.

始于初秋 2024-12-03 18:55:32

您必须将 div 放置在 position:relative 元素之外并放入 body 中。

You will have to place the div outside of the position:relative element and into body.

伴梦长久 2024-12-03 18:55:32

我的解决方案是使用 jQuery 将 div 移出其父级:

<script>
    jQuery(document).ready(function(){
        jQuery('#loadingouter').appendTo("body");
    });
</script>

<div id="loadingouter"></div>

My solution was to use jQuery for moving the div outside its parent:

<script>
    jQuery(document).ready(function(){
        jQuery('#loadingouter').appendTo("body");
    });
</script>

<div id="loadingouter"></div>
慕烟庭风 2024-12-03 18:55:32

如果您不想将元​​素附加到 body,则可以使用以下解决方案。

我来到这个问题寻找一个无需将 div 附加到主体的解决方案,因为我有一个鼠标悬停脚本,我想在鼠标悬停在新元素和生成它的元素上时运行该脚本。只要您愿意使用 jQuery,并受到 @Liam William 的回答的启发:

var leftOffset = <<VALUE>>;
var topOffset = <<VALUE>>;
$(element).css("left", leftOffset - element.offset().left);
$(element).css("top", topOffset - element.offset().top);

此解决方案的工作原理是减去元素当前的左侧和顶部位置(相对于主体),从而将元素移动到 0, 0。无论您想要相对于主体的任何位置,都只需添加左侧和顶部偏移值即可。

If you don't want to attach the element to body, the following solution will work.

I came to this question looking for a solution that would work without attaching the div to the body, because I had a mouseover script that I wanted to run when the mouse was over both the new element and the element that spawned it. As long as you are willing to use jQuery, and inspired by @Liam William's answer:

var leftOffset = <<VALUE>>;
var topOffset = <<VALUE>>;
$(element).css("left", leftOffset - element.offset().left);
$(element).css("top", topOffset - element.offset().top);

This solution works by subtracting the element's current left and top position (relative to the body) so as to move the element to 0, 0. Placing the element wherever you want relative to the body is then as simple as adding a left and top offset value.

千里故人稀 2024-12-03 18:55:32

简单地使用 CSS 和 HTML 是不可能实现这一点的。

使用 Javascript/jQuery,您可能会将元素 jQuery.offset() 获取到 DOM,并将其与 jQuery.position() 进行比较,以计算它应该出现在页面上的位置。

This isn't possible with simply CSS and HTML.

Using Javascript/jQuery you could potentially get the elements jQuery.offset() to the DOM and compare it the jQuery.position() to calculate where it should appear on the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文