检索元素margin,如果它'auto' [英] Retrieve element margin if it's 'auto'
问题描述
我试着定位新的dom元素与旧的一样,然后隐藏旧的,所以新的将替换它: http://jsfiddle.net/jC33F/5/
Hi I'm trying position new dom element same as old one, and then hide old one so new would replace it: http://jsfiddle.net/jC33F/5/
虽然如果原始元素有margin:auto jQuery无法检索。
有一种方法能够确定元素是否具有margin:auto; ?
Though if original element has margin:auto jQuery can't retrieve it. Is there a way to be able to determine if element has margin:auto; ?
编辑:
感谢@Vibhu我想出了这个 http://jsfiddle.net/jC33F/43/ 看起来可怕:D我不确定它会一直工作。
Thanks to @Vibhu I came up with this http://jsfiddle.net/jC33F/43/ looks horrible :D And I'm not sure it will always work.
推荐答案
这可能很疯狂,但是像这样的东西, margin:0 auto
(似乎找不到任何其他方式):
This might be crazy, but what about something like this for checking it an element has margin: 0 auto
(cannot seem to find any other way):
var margin = $("#parent").css("margin");
if($('#parent').position().left*2 == ($('#parent').parent().innerWidth() - $('#parent').outerWidth())) {
margin = "0px auto";
}
$("#positionable").css({
position: $("#parent").css("position"),
top: $("#parent").css("top"),
left: $("#parent").css("left"),
margin: margin
});
$("#parent").hide();
此代码基本上检查#parent的左值是否等于2个边之间的一半的容器。在这种情况下为我工作,在其他情况下可能会失败。
This code basically checks if the left value of #parent is equal to half the space between the 2 edges of its container. Worked for me in this case, might fail in other cases.
这篇关于检索元素margin,如果它'auto'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!