您可以重置 CSS 元素以引用相对元素中的绝对零吗

发布于 2024-10-04 05:04:40 字数 423 浏览 4 评论 0原文

这是一个相当随机的问题。我使用 CSS3Pie 来处理 IE 中的 CSS3 兼容性,但遇到了障碍。在很多情况下,对于 PIE,您需要使用position:relative 才能使其在 IE 中正常工作。

当我在包含包装器上使用它时,这成为一个问题,因为它随后会在布局内部的零引用中移动。我们用于在网站上绘制地图的软件使用浏览器左上角的绝对位置作为计算定位的参考点。理想情况下,我们的软件会更加灵活,不依赖于这样的绝对位置,但这可能不会很快改变。

那么回到我的问题,是否有可能以某种方式将元素从布局流中取出并引用回原始的 0,0 左上角位置,同时包含在相对定位的对象中?我认为也许 z-index 可能有效,但它不会使元素引用回绝对值 0,0。

最简单的解决方法是不对我的包装器应用 PIE,但这也会从我的布局包装器中删除圆角和阴影。这不是世界末日,但理想情况下我想找到一个解决方案。

This is a rather random problem. I'm using CSS3Pie to handle CSS3 compatibility in IE, but I've run into a snag. With PIE in a lot of cases you need to use position:relative for it to work properly in IE.

This becomes an issue when I use it on containing wrappers, as it then moves in the zero reference inside of the layout. The software we use for mapping on our sites uses an absolute position of the top left corner of the browser as its point of reference for calculating positioning. Ideally our software would be more flexible and not reliant on an absolute position like that, but that's probably not going to change anytime soon.

So getting to my question, is it possible to somehow take an element out of the flow of the layout and refer back to the original 0,0 top left position while being contained within a relatively positioned object? I thought maybe z-index might work, but it doesn't make the element refer back to absolute 0,0.

The most simple workaround is to not apply PIE to my wrappers, but then that also removes rounded corners and dropshadows from my layout wrapper. That isn't the end of the world, but ideally I'd like to come up with a solution.

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

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

发布评论

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

评论(3

☆獨立☆ 2024-10-11 05:04:41

好问题:我认为如果没有 js,这可能是不可能的。

这是一个 jQuery 解决方法: http://jsfiddle.net/SebastianPataneMasuelli/hSh25/

它采用左侧和相对定位元素的顶部值,并从绝对值中减去它们,实际上将绝对值返回到原点。

因此,鉴于

<div id="relative">
relative
  <div id="absolute">
     absolute;
 </div>

此计算如何在添加定位 div 的左侧值和顶部值之前返回到 0。

var relativeLeft = $('#relative').css('left');
var absoluteLeft =  $('#absolute').css('left');
var returnToOriginLeft = (parseInt(absoluteLeft) - parseInt(relativeLeft)) + 'px';
var relativeTop = $('#relative').css('top');
var absoluteTop =  $('#absolute').css('top');
var returnToOriginTop = (parseInt(absoluteTop) - parseInt(relativeTop)) + 'px';
$('#absolute').css('left', returnToOriginLeft);
$('#absolute').css('top', returnToOriginTop);

你必须在你的实际站点中获得更具体的信息(我想那里有一堆相对定位的 div)。请告诉我它是否是 CSS3PIE 的可行解决方案,因为我计划很快在项目中使用它。

注意: JavaScript 不是我的强项,我怀疑有一种更优雅的方式来编写上面的语句。

Great Question: i think it might be impossible without js.

this is a jQuery workaround: http://jsfiddle.net/SebastianPataneMasuelli/hSh25/

it takes the left and top values of the relatively positioned element and subtracts them from the absolute one, in effect returing the absolute one to the origin point.

so, given

<div id="relative">
relative
  <div id="absolute">
     absolute;
 </div>

this calculates how to get back to 0 before adding left and top values for the positioned div.

var relativeLeft = $('#relative').css('left');
var absoluteLeft =  $('#absolute').css('left');
var returnToOriginLeft = (parseInt(absoluteLeft) - parseInt(relativeLeft)) + 'px';
var relativeTop = $('#relative').css('top');
var absoluteTop =  $('#absolute').css('top');
var returnToOriginTop = (parseInt(absoluteTop) - parseInt(relativeTop)) + 'px';
$('#absolute').css('left', returnToOriginLeft);
$('#absolute').css('top', returnToOriginTop);

you'll have to get more specific in your actual site (i imagine there's a bunch of relatively positioned divs there).Please let me know if its a workable solution for CSS3PIE, as i plan to use it myself in a project soon.

note: Javascript is not my strong point, i suspect that there is a more elegant way of writing the statement above.

落墨 2024-10-11 05:04:41
$(document).ready(function() {
    var el = $('#absolute'); // element we want to work with
    el.clone().appendTo('body'); // clone and append to body
    el.remove(); // remove element
});

这是对 Wilson Page 代码的一点改进。到目前为止仅在 localhost 上进行了测试,但它确实成功了。在启用 IE6/7/8 + CSS3PIE 的情况下进行测试。

想要将其放在公共网络服务器上,但事实证明他们没有启用 *.htc 支持,并且当与 PIE.php (提供的脚本)一起使用时PIE)——它不起作用。

稍后当我回到办公室并开始推动系统管理员在服务器上启用 *.htc 支持时,我将进行更多测试。 呵呵

而且,我全面测试了Sebastian Patane Masue的脚本,它成功了! +1@塞巴斯蒂安

PS“完全”是指启用 CSS3PIE 并将属性添加到实际样式中。

编辑:刚刚再尝试一次,结果非常烦人。它在本地主机上完美运行,但在远程查看时却遇到了很大的问题。不知道是什么原因造成的...

$(document).ready(function() {
    var el = $('#absolute'); // element we want to work with
    el.clone().appendTo('body'); // clone and append to body
    el.remove(); // remove element
});

This is a little improvement of Wilson Page's code. Tested so far only on localhost, but it did the trick. Tested with IE6/7/8 + CSS3PIE enabled.

Wanted to put this up on public webserver, but it turns out they haven't enabled *.htc support, and when using it with PIE.php (the script provided with PIE) — it didn't work.

Will do some more testing later on when I get back to office and start pushing system admins to enable *.htc support on server. hehe

And, I fully tested Sebastian Patane Masue's script, it did the job! +1 @ Sebastian.

P.S. With fully I mean with CSS3PIE enabled and properties added to actual style.

EDIT: Just gave this a one more try, and the results are pretty annoying. It works on localhost flawlessly, but is pretty bugged when viewing it remotely. Have no idea what is causing that...

岁月蹉跎了容颜 2024-10-11 05:04:40

我认为 JS/jQuery 是必需的:

尝试类似的方法:

var el = $('#element');

$('body').append(el);
el.remove();

希望这是一个可以接受的修复,

W.

I think JS/jQuery is required for this:

try something like:

var el = $('#element');

$('body').append(el);
el.remove();

Hope this is an acceptable fix,

W.

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