jQuery: positioning popup windows

Today I've changed my tweet buttons and I had to manually create the popup windows with jQuery. How to position such popups near their related links? The solution is pretty simple.

We have only to get the top and left offset of each link and use the returned values with the open() method:

$(function() {

	$('a.popup').click(function(e) {
    	e.preventDefault();
    	var $a = $(this);
    	var url = $a.attr('href');
    	var top = $a.offset().top;
    	var left = $a.offset().left;
    	window.open(url, '', 'width=500,height=400,top=' + top + ',left=' + left);
	});​

});

Really simple.

Back to top