html - jQuery: best way to place dom element in the center of viewport -


i'm looking proper way of placing popup div-element in center of current view area.

for example: have div element {display:none; position:absolute} , few buttons, 1 on top of document, second in center , last one, somewhere in bottom. clicking on of button, div should appear in center of current viewing area

$(".btnclass").click(function(){     //some actions positioning here     $(div_id).show()   }) 

the following it. although there other ways (using css, margins, overflows, etc)...so may not answer question depending on consider "best" be.

$(".btn_class").click(function(){     var win = $(window),         winw = win.width(),         winh = win.height(),         scrolltop = win.scrolltop(),         scrollleft = win.scrollleft(),         container = $("#div_id").css({"display":"block","visibility":"hidden"}),         contw = container.width(),         conth = container.height(),         left = (winw-contw)/2+scrollleft,         top = (winh-conth)/2+scrolltop;      container.css({"left":left,"top":top,"visibility":"visible"}); }); 

you may have adjust scrollleft , scrolltop...i'm being distracted , can't think (sigh, wish had private office again).


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -