Siegfried Puchbauer
2006-Feb-03 14:10 UTC
Contribute: Center extension to Prototype''s Position object
If anyone is intrested, i was hardly missing such a feature in prototype (for displaying well formatted error dialogs and things like that): Position.center = function(element){ var options = Object.extend({ zIndex: 999, update: false }, arguments[1] || {}); element = $(element) if(!element._centered){ Element.setStyle(element, {position: ''absolute'', zIndex: options.zIndex }); element._centered = true; } var dims = Element.getDimensions(element); Position.prepare(); var winWidth = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0; var winHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0; var offLeft = (Position.deltaX + Math.floor((winWidth-dims.width )/2)); var offTop = (Position.deltaY + Math.floor((winHeight-dims.height )/2)); element.style.top = ((offTop != null && offTop > 0) ? offTop : ''0'') + ''px''; element.style.left = ((offLeft != null && offLeft > 0) ? offLeft : ''0'') + ''px''; if(options.update){ Event.observe(window, ''resize'', function(evt){ Position.center(element); }, false); Event.observe(window, ''scroll'', function(evt){ Position.center(element); }, false); } } You could call Position.absolutize first to keep the dimensions of a relative positioned element. Tested in IE6, Mozilla 1.7 and FF 1.5. Any suggestions on enhacements or bugfixes are appreciated. It would be great too if anyone could tell me if it works in safari, konqueror or opera. brgds sigi -- Mit freundlichen Grüßen Siegfried Puchbauer _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs