Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertical align div within a div using jquery?

Anyone have any experience with jquery vAligh plugins or similar?

I tried to align for the following but it fails.. I was using a simple valign plugin (I will put the plugin at the end, its a jquery extension), if anyone can help it would be really helpful...

var overlayLayer = $("<div id='office-location'></div>").addClass('modal-overlay');
$('body').append(overlayLayer);

$('<div id="content-for-overlay" style="background-color: white;"></div>').appendTo(overlayLayer);

this.render({ to: "content-for-overlay", partial: "office-location-modal" }); // this just copies html into the layer

$('#content-for-overlay').vAlign(); THIS USES a plugin called valign but it doesn't align

$("body").css("overflow", "hidden");
$('#office-location').css("opacity", 0.8).fadeIn(150);
$('#content-for-overlay').css("opacity", 1);

heres the FN extension..

(function($) {
$.fn.vAlign = function() {
    return this.each(function(i) {
        var h = $(this).height();
        var oh = $(this).outerHeight();
        var mt = (h + (oh - h)) / 2;
        $(this).css("margin-top", "-" + mt + "px");
        $(this).css("top", "50%");
        $(this).css("position", "absolute");
    });
};

})(jQuery);

like image 946
mark smith Avatar asked Jun 08 '09 16:06

mark smith


1 Answers

You could try using a CSS technique to vertically centre items, as shown here. I believe the method is cross-browser, though unfortunately it adds an extra div to your markup.

It'd be fairly easy to apply this css to the markup using jQuery, using $().css()

like image 176
Donald Harvey Avatar answered Oct 13 '22 00:10

Donald Harvey