Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve element margin if it's 'auto'

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/

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; ?

Edit: 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.

Feel free to suggest something better.

like image 633
grisevg Avatar asked Nov 04 '22 15:11

grisevg


1 Answers

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();

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.

like image 102
VNO Avatar answered Nov 15 '22 06:11

VNO